#
ResponseEntity example
This tutorial give you an example of using ResponseEntity.
Info
- ResponseEntity is specific to Spring Framework, not just Spring Boot, though it is commonly used in Spring Boot applications as well.
- used when you need to control the HTTP response sent back to the client (the status code, the headers, the body format).
- ResponseEntity can return both JSON and XML responses (by default, Spring will use JSON).
Here we have an example:
DemoApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Emp.java
package com.example.demo.model;
public class Emp {
private String id;
private String nume;
public Emp(String id, String nume) {
this.id = id;
this.nume = nume;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getNume() {
return nume;
}
public void setNume(String nume) {
this.nume = nume;
}
}
EmpDto.java
package com.example.demo.dto;
public class EmpDto {
private String empId;
private String name;
public EmpDto() { }
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
MyController.java
package com.example.demo.controler;
import com.example.demo.dto.EmpDto;
import com.example.demo.model.Emp;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.example.demo.service.MyService;
import java.util.Optional;
@RestController
@RequestMapping()
public class MyController {
private final MyService myService;
public MyController(MyService myService) {
this.myService = myService;
}
@GetMapping ("/view")
public ResponseEntity<Emp> view(@RequestParam("id") String id) {
Optional<Emp> response = myService.view(id);
// Create HttpHeaders object
HttpHeaders headers = new HttpHeaders();
headers.add("X-Custom-Header", "ABCD");
if (response.isEmpty()){
return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body(null);
}
return ResponseEntity
.status(HttpStatus.OK)
.headers(headers)
.body(response.get());
}
@PostMapping ("/add/{id}")
public ResponseEntity<Void> view(@PathVariable String id,
@RequestBody EmpDto empDto ) {
myService.add(id, empDto);
return ResponseEntity
.status(HttpStatus.OK)
.body(null);
}
}
MyService.java
package com.example.demo.service;
import com.example.demo.dto.EmpDto;
import com.example.demo.model.Emp;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
@Service
public class MyService {
private final Map<String, Emp> hashMap = new HashMap();
public Optional<Emp> view(String id){
return Optional.ofNullable(hashMap.get(id));
}
public void add(String id, EmpDto empDto){
Emp emp = new Emp(empDto.getEmpId(), empDto.getName());
hashMap.put(id, emp);
}
}