Java 如何在spring boot中重用定制响应
我用spring boot编写了一个简单的api。Java 如何在spring boot中重用定制响应,java,spring,spring-boot,Java,Spring,Spring Boot,我用spring boot编写了一个简单的api。 当save成功时,它将使用我用json类型生成的200个httpStatus代码进行响应。 但是我想在全球范围内使用这个响应,重用它 @PostMapping("/notice") public ResponseEntity<Object> createNotice(@RequestBody @Valid NoticeDto noticeDto, HttpServletResponse response) {
当save成功时,它将使用我用json类型生成的200个httpStatus代码进行响应。
但是我想在全球范围内使用这个响应,重用它
@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();
}
@PostMapping(“/通知”)
public ResponseEntity createNotice(@RequestBody@Valid NotifiedTo NotifiedTo,HttpServletResponse){
noticeService.createNotice(noticeDto);
ObjectNode jsonResponse=null;
试一试{
ObjectMapper mapper=新的ObjectMapper();
jsonResponse=mapper.createObjectNode();
jsonResponse.put(“代码”,200);
jsonResponse.put(“成功”,true);
jsonResponse.put(“msg”,“OK”);
}捕获(例外情况除外){
例如printStackTrace();
}
PrintWriter out=response.getWriter();
答复:setStatus(200);
setContentType(“应用程序/json”);
响应。setCharacterEncoding(“UTF-8”);
out.flush();
}
例如,我想这样使用它
@PostMapping("/notice")
public ResponseEntity<Object> createNotice(@RequestBody @Valid NoticeDto noticeDto, HttpServletResponse response) {
noticeService.createNotice(noticeDto);
return successResponse something...
}
@PostMapping(“/通知”)
public ResponseEntity createNotice(@RequestBody@Valid NotifiedTo NotifiedTo,HttpServletResponse){
noticeService.createNotice(noticeDto);
返回成功响应某事。。。
}
为此,我应该编写哪个类文件?谢谢你阅读我的问题。你可以这样做
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();
}
}
用法:
@PostMapping("/notice")
public ResponseEntity<Object> createNotice(@RequestBody @Valid NoticeDto noticeDto, HttpServletResponse response) {
noticeService.createNotice(noticeDto);
HttpResponsePrinter.ok(response);
}
@PostMapping(“/通知”)
public ResponseEntity createNotice(@RequestBody@Valid NotifiedTo NotifiedTo,HttpServletResponse){
noticeService.createNotice(noticeDto);
httpresponseprenter.ok(响应);
}
您可以通过以下方式更改控制器方法:
您可以通过多种方式实现这一点。其中一种方法如下所述
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();
}
package com.example.demo.controller;
导入com.example.demo.dto.NoticeDto;
导入com.example.demo.service.NoticeService;
导入com.fasterxml.jackson.databind.ObjectMapper;
导入com.fasterxml.jackson.databind.node.ObjectNode;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.http.ResponseEntity;
导入org.springframework.web.bind.annotation.PostMapping;
导入org.springframework.web.bind.annotation.RequestBody;
导入org.springframework.web.bind.annotation.RestController;
导入java.util.Map;
@RestController
公共类DemoController{
@自动连线(必需=错误)
私人通知服务通知服务;
@PostMapping(value=“/notice”,products=“application/json”,
consumes=“application/json”)
公共响应实体createNotice(@RequestBody NotifiedTo NotifiedTo){//
noticeService.createNotice(noticeDto);
ObjectNode jsonResponse=null;
试一试{
ObjectMapper mapper=新的ObjectMapper();
jsonResponse=mapper.createObjectNode();
jsonResponse.put(“代码”,200);
jsonResponse.put(“成功”,true);
jsonResponse.put(“msg”,“OK”);
}捕获(例外情况除外){
例如printStackTrace();
}
返回新的SuccessResponseWrapper(jsonResponse.toPrettyString()).build();
}
}
及
您可以添加一个名为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;
}
}
package com.example.demo.controller;
导入org.springframework.http.HttpStatus;
导入org.springframework.http.ResponseEntity;
导入org.springframework.util.MultiValueMap;
公共类SuccessResponseWrapper{
私人责任;
公共成功响应包装器(T体){
HttpStatus status=HttpStatus.OK;
responseEntity=新的responseEntity(身体、状态);
}
public SuccessResponseWrapper(多值映射头){
HttpStatus status=HttpStatus.OK;
responseEntity=新的responseEntity(标题、状态);
}
public SuccessResponseWrapper(T体,多值映射头){
HttpStatus status=HttpStatus.OK;
responseEntity=新的responseEntity(正文、标题、状态);
}
public SuccessResponseWrapper(T体、多值映射头、int-rawStatus){
responseEntity=新的responseEntity(主体,标题,200);
}
公共响应能力建设(){
返回此。responseEntity;
}
}
通过这种方式,您可以使用预定义的http响应代码200重用SuccessResponseWrapper。为什么不使用@ResponseBody(或RestController)响应json并返回一个普通对象(属性:code、success、msg)。