visit
In this spring boot quick start, we will learn how to create a spring application using Spring Initializr. In this article, we will create a spring boot project and import it in any editor like eclipse or sts, then create a rest controller and use @GetMapping for creating spring boot rest API example.
When you have completed this tutorial, you should understand:
After importing this spring boot application, you create HelloController.java class as below. This is the REST controller in the Spring Boot application. Now we will create a Spring Boot services JSON example.
We map your request in HellpController.java class with the help of @GetMapping annotation.
package com.jp.helloWorld.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jp.helloWorld.model.User;
@RestController
public class HelloController {
@GetMapping("/user")
public User getApplicationName(){
User user = new User( 10L, "amit");
return user;
}
}
package com.jp.helloWorld.model;
public class User {
private Long userId;
private String name;
public User(){
}
public User(Long userId, String name) {
super();
this.userId = userId;
this.name = name;
}
public Long getUserId() {
return userId;
}
public String getName() {
return name;
}
}
When you call our Spring Boot REST API using url //localhost:8080/user with get method, then you will get:
{"userId":10,"name":"amit"}