006.封装ResponseUtils工具类

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);
}
}
}

 

posted @ 2022-12-06 23:07  李林林  阅读(218)  评论(0编辑  收藏  举报