Java Spring Boot REST API
In this page:
REST Controllers
The @RestController annotation marks a class as handling HTTP requests directly, and combines @Controller with @ResponseBody so every method's return value is automatically serialized to JSON in the response, without you writing serialization code by hand.
Example: REST Controllers
import org.springframework.web.bind.annotation.RestController;
@RestController // combines @Controller + @ResponseBody
public class Main {
public static void main(String[] args) {
System.out.println("Every method's return value is serialized to JSON automatically");
}
}
Login to try C/C++/Java/PHP code in the editor
GET Request Handlers
Use @GetMapping to route incoming GET requests to a specific controller method, and @PathVariable to capture dynamic segments straight out of the URL path (like the id in /users/{id}) as a typed method parameter.
Example: GET Request Handlers
import org.springframework.web.bind.annotation.*;
@RestController
public class Main {
@GetMapping("/users/{id}")
String getUser(@PathVariable int id) {
return "User #" + id;
}
public static void main(String[] args) {
System.out.println(new Main().getUser(7));
}
}
Login to try C/C++/Java/PHP code in the editor
POST Request Handlers
Use @PostMapping to route incoming POST requests, and the @RequestBody annotation to have Spring automatically deserialize the incoming JSON request body directly into a Java object, saving you from manually parsing the payload.
Example: POST Request Handlers
import org.springframework.web.bind.annotation.*;
@RestController
public class Main {
record UserRequest(String name) {}
@PostMapping("/users")
String createUser(@RequestBody UserRequest request) {
return "Created: " + request.name();
}
public static void main(String[] args) {
System.out.println(new Main().createUser(new UserRequest("Riya")));
}
}
Login to try C/C++/Java/PHP code in the editor
Custom Request Mappings
Prefixing a controller class with @RequestMapping lets you group all of its endpoints under a shared base URL path, which keeps a growing REST API organized into clear resource-based sections instead of every endpoint repeating a common prefix.
Example: Custom Request Mappings
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/users") // shared base path for every endpoint below
public class Main {
@GetMapping("/{id}")
String getUser(@PathVariable int id) { return "User #" + id; }
public static void main(String[] args) {
System.out.println(new Main().getUser(3));
}
}
Login to try C/C++/Java/PHP code in the editor
Content Negotiation
REST controllers can automatically format the same response as XML or JSON depending on the client's Accept header and the request's Content-Type, letting one endpoint serve multiple consumers without duplicating handler logic for each format.
Example: Content Negotiation
import org.springframework.web.bind.annotation.*;
@RestController
public class Main {
@GetMapping(value = "/data", produces = {"application/json", "application/xml"})
String getData() {
return "same handler serves JSON or XML based on Accept header";
}
public static void main(String[] args) {
System.out.println(new Main().getData());
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: