1.封装ResponseUtils(对标准的Code、Message进行设置)
package com.imooc.oa.utils;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.LinkedHashMap;
import java.util.Map;
public class ResponseUtils {
private String code;
private String message;
private Map data = new LinkedHashMap<>();
public ResponseUtils(){
this.code = "0";
this.message = "success";
}
public ResponseUtils(String code , String message){
this.code = code;
this.message = message;
}
public ResponseUtils put(String key , Object value){
this.data.put(key, value);
return this;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Map getData() {
return data;
}
public void setData(Map data) {
this.data = data;
}
public String toJsonString(){
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
try {
String json = objectMapper.writeValueAsString(this);
return json;
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}
标签:code,封装,String,006,message,data,public,ResponseUtils From: https://www.cnblogs.com/LLL0617/p/16961737.html