In this post, you will get to know how to build RESTful Web Service using Java Spring. RESTful Web Service is a type of application that gets an HTTP request and returns a JSON response. You can use it to get data from your application, database etc via a simple HTTP request.
First, we need to create a new project or just add libraries to your existing project. I’m using Gradle, so if you too, you can copy entire build
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
buildscript { repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.5.RELEASE") } } apply plugin: 'java' apply plugin: 'idea' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' group 'com.mateuszzbylut' version '1.0-SNAPSHOT' sourceCompatibility = 1.8 repositories { mavenCentral() } dependencies { compile group: 'org.springframework.boot', name: 'spring-boot-starter-web' testCompile group: 'junit', name: 'junit', version: '4.12' testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test' } |
Example
We need some data to work on, so in this example, we will have a simple Person class with parameter id, name, lastName.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public class Person { private final long id; private final String name; private final String lastName; public Person(long id, String name, String lastName) { this.id = id; this.name = name; this.lastName = lastName; } public long getId() { return id; } public String getName() { return name; } public String getLastName() { return lastName; } } |
PersonController
Now, we have a PersonController class where all the magic happen. Look at the code:
1 2 3 4 5 6 7 8 9 10 |
@RestController public class PersonController { private final AtomicLong counter = new AtomicLong(); @RequestMapping("/whoareyou") public Person person(@RequestParam(value = "name", defaultValue = "Joe") String name, @RequestParam(value = "lastname", defaultValue = "Doe") String lastName) { return new Person(counter.incrementAndGet(), name, lastName); } } |
First, look at the first line, this annotation tells to the compiler that this is class that will be used as RESTful Web Service. We have AtomicLong to generate unique ids for our Person objects. Finally, we have method person with annotation RequestMapping in the parameter we will give string that will be part of request URL. Then, in method parameters, we create RequestParams, we need set value – the name of the parameter and default value to this variable. You can add more parameters if you need. In the method body, we return a new Person object.
In the end, we need a main class that will launch the Spring Boot Application.
1 2 3 4 5 6 |
@SpringBootApplication public class Webservice { public static void main(String[] args) { SpringApplication.run(Webservice.class, args); } } |
Now you can build and run this code and make request to your Web Service.
For example, first, make the request without any parameters http://localhost:8080/whoareyou Web Service will return JSON with default parameters.
1 |
{"id":1,"name":"Joe","lastName":"Doe"} |
Let’s add first parameter http://localhost:8080/whoareyou?name=Tom, now, we create an object with name Tom and default last name.
1 |
{"id":2,"name":"Tom","lastName":"Doe"} |
In the end, let’s test the last scenario http://localhost:8080/whoareyou?name=Tom&lastname=Page, Web Service return JSON object with a name and last name given in URL.
1 |
{"id":3,"name":"Tom","lastName":"Page"} |
You just build Web Service using Java Spring. This is a very simple example, you can add more scenarios to this code, integrate database etc. There are so many possibilities and scenarios to create and use Web Services.
Be First to Comment