1
1
.
.
2
2
.
.
1
1
S
S
e
e
r
r
v
v
e
e
r
r
I
I
n
n
f
f
o
o
[
[
G
G
]
]
This tutorial shows how to create Spring Boot Application that returns Person(s) in JSON format.
We will use it to demonstrate WebClient functionality (so that WebClient has something to call and work with).
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables @Controller, @RequestMapping and Tomcat HTTP Server.
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: controller_returns_text_json (add Spring Boot Starters from the table)
Edit File: application.properties (specify port 8085 so that Client can run on 8080)
Create Package: entities (inside main package)
Create Interface: Person.java (inside package entities)
Create Package: controllers (inside main package)
Create Class: MyController.java (inside controllers package)
application.properties
# PORT
server.port = 8085
Person.java
package com.ivoronline.springboot_webclient_server.entities;
public class Person {
public Integer id;
public String name;
public Integer age;
}
Person
http://localhost:8085/GetPerson
Tomcat
Browser
MyController
http://localhost:8085/GetPersons
getPersons()
MyController.java
package com.ivoronline.springboot_webclient_server.controllers;
import com.ivoronline.springboot_webclient_server.entities.Person;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.List;
@Controller
public class MyController {
//===============================================================
// GET PERSON
//===============================================================
@ResponseBody
@RequestMapping("/GetPerson")
public Person getPerson() {
//CREATE PERSON
Person person = new Person();
person.id = 1;
person.name = "Susan";
person.age = 30;
//RETURN PERSON
return person;
}
//===============================================================
// GET PERSONS
//===============================================================
@ResponseBody
@RequestMapping("/GetPersons")
public List<Person> getPersons() {
//CREATE PERSON 1
Person person1 = new Person();
person1.id = 2;
person1.name = "John";
person1.age = 40;
//CREATE PERSON 2
Person person2 = new Person();
person2.id = 3;
person2.name = "Bill";
person2.age = 50;
//CREATE LIST
List<Person> persons = new ArrayList();
persons.add(person1);
persons.add(person2);
//RETURN PERSON
return persons;
}
}
R
R
e
e
s
s
u
u
l
l
t
t
s
s
http://localhost:8085/GetPerson
http://localhost:8085/GetPersons
Application Structure
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>