I wrote an simple api by spring boot.
And when save is success, it response with 200 httpStatus code that I made by json type.
But I want to use this response globally reusing it.
@PostMapping("/notice")
public ResponseEntity<Object> createNotice(@RequestBody @Valid NoticeDto noticeDto, HttpServletResponse response) {
noticeService.createNotice(noticeDto);
ObjectNode jsonResponse = null;
try {
ObjectMapper mapper = new ObjectMapper();
jsonResponse = mapper.createObjectNode();
jsonResponse.put("code", 200);
jsonResponse.put("success", true);
jsonResponse.put("msg", "OK");
} catch (Exception ex) {
ex.printStackTrace();
}
PrintWriter out = response.getWriter();
response.setStatus(200);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
out.flush();
}
For example, I want to use it like this.
@PostMapping("/notice")
public ResponseEntity<Object> createNotice(@RequestBody @Valid NoticeDto noticeDto, HttpServletResponse response) {
noticeService.createNotice(noticeDto);
return successResponse something...
}
For this purpose, which class file should I write?
Thank you for reading my questiong.
You can change the controller method this way:
You can achieve this in multiple ways. One of the way is as mentioned below.
package com.example.demo.controller;
import com.example.demo.dto.NoticeDto;
import com.example.demo.service.NoticeService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
@RestController
public class DemoController {
@Autowired(required = false)
private NoticeService noticeService;
@PostMapping(value = "/notice", produces = "application/json",
consumes="application/json")
public ResponseEntity<Map> createNotice(@RequestBody NoticeDto noticeDto) { //
noticeService.createNotice(noticeDto);
ObjectNode jsonResponse = null;
try {
ObjectMapper mapper = new ObjectMapper();
jsonResponse = mapper.createObjectNode();
jsonResponse.put("code", 200);
jsonResponse.put("success", true);
jsonResponse.put("msg", "OK");
} catch (Exception ex) {
ex.printStackTrace();
}
return new SuccessResponseWrapper<String>(jsonResponse.toPrettyString()).build();
}
}
And
You can add a new class called SuccessResponseWrapper.
package com.example.demo.controller;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
public class SuccessResponseWrapper<T> {
private ResponseEntity<T> responseEntity;
public SuccessResponseWrapper(T body) {
HttpStatus status = HttpStatus.OK;
responseEntity = new ResponseEntity<>(body, status);
}
public SuccessResponseWrapper(MultiValueMap<String, String> headers) {
HttpStatus status = HttpStatus.OK;
responseEntity = new ResponseEntity<>(headers, status);
}
public SuccessResponseWrapper(T body, MultiValueMap<String, String> headers) {
HttpStatus status = HttpStatus.OK;
responseEntity = new ResponseEntity<>(body, headers, status);
}
public SuccessResponseWrapper(T body, MultiValueMap<String, String> headers, int rawStatus) {
responseEntity = new ResponseEntity<>(body, headers, 200);
}
public ResponseEntity build() {
return this.responseEntity;
}
}
In this way, you can reuse the SuccessResponseWrapper with Predefined http response code 200.
You can do something like this
class HttpResponsePrinter{
public static void ok(HttpServletResponse response){
write(response, 200, 200, true, "OK");
}
private static void write(HttpServletResponse response,int httpCode, int code, bool success, String message) {
ObjectNode jsonResponse = null;
try {
// you can reuse object mapper as well
ObjectMapper mapper = new ObjectMapper();
jsonResponse = mapper.createObjectNode();
jsonResponse.put("code", code);
jsonResponse.put("success", success);
jsonResponse.put("msg", message);
} catch (Exception ex) {
ex.printStackTrace();
}
PrintWriter out = response.getWriter();
response.setStatus(httpCode);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
out.flush();
}
}
Usage:
@PostMapping("/notice")
public ResponseEntity<Object> createNotice(@RequestBody @Valid NoticeDto noticeDto, HttpServletResponse response) {
noticeService.createNotice(noticeDto);
HttpResponsePrinter.ok(response);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.