自定义接口返回json
- 最近在写接口返回json数据时老是出现问题,采用的也是返回的数据是自定义的格式,在controller层中使用@ResponseBody相当于是自定义返回json,这样的话就是返回的数据实在太不好看。刚好今天看了一下博客,拿了一个工具类刚好借用一下。
- 首先定义返回实体Result,这里面重点看下里面的构造函数,用了的方法的重载。
package com.steven.utils.result;
public class Result {
private Integer code;
private Boolean isSuccess;
private String massege;
private Object result;
public Result(){
super();
}
public Result(Boolean success, Integer code, String massege){
super();
this.isSuccess=success;
this.code=code;
this.massege=massege;
}
public Result(Boolean success, Integer code, Object result){
super();
this.isSuccess=success;
this.code=code;
this.result=result;
}
public Result(Boolean success, Integer code, String massege, Object result){
super();
this.isSuccess=success;
this.code=code;
this.massege=massege;
this.result=result;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public Boolean getIsSuccess() {
return isSuccess;
}
public void setIsSuccess(Boolean isSuccess) {
this.isSuccess = isSuccess;
}
public String getMassege() {
return massege;
}
public void setMassege(String massege) {
this.massege = massege;
}
public Object getResult() {
return result;
}
public void setResult(Object result) {
this.result = result;
}
}
- Result实体创建了接下来还需要把转换为json,我这里用的fastJson,这方面的资料网上很多,下面看下Json这个类。
package com.steven.utils.json;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.steven.utils.result.Result;
public class Json {
public static void toJson(Result result, HttpServletResponse response) throws Exception{
response.setContentType("text/json");
response.setHeader("Cache-Control", "no-cache");
response.setCharacterEncoding("UTF-8");
PrintWriter writer = response.getWriter();
String json = JSONObject.toJSONString(result,
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullNumberAsZero,
SerializerFeature.WriteNullListAsEmpty,
SerializerFeature.WriteNullStringAsEmpty,
SerializerFeature.WriteNullBooleanAsFalse,
SerializerFeature.DisableCircularReferenceDetect);
writer.write(json);
writer.close();
}
}
- 调用接口
@RequestMapping(value = "/testJson",method = RequestMethod.GET)
public void testJson(@RequestParam String risenPhone, @RequestParam String risenfgAuditDsc, HttpServletResponse response) {
Map<String, String> resultMap = new HashMap<>();
String url = "http://116.62.244.37/yqx/v1/sms/single_send";
Map<String,String> params = new HashMap<>();
params.put("account","7552");
params.put("mobile",risenPhone);
params.put("text", "您提交的预审登记未通过,不通过原因为:" + risenfgAuditDsc);
params.put("sign","M2NlOGQ0MzBhYjFlOTBlZjcwOWJkN2Y1NzhhNWUwMTE=");
params.put("extend","");
try {
ArrayList<String> resultParams = new ArrayList<>();
resultParams.add("code");
resultMap = HttpClientUtil.invokeHttp(url, com.risen.wechat.unit.HttpClientUtil.HTTP_POST, params,resultParams);
Result result = new Result(true,200,"成功",resultMap);
Json.toJson(result,response);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
- 利用postman进行接口测试
- 返回json如下
{
"code": 200,
"isSuccess": true,
"massege": "成功",
"result": {
"msgid": "1910301815450866",
"code": "3007",
"msg": "当天单个号码发送超过限制数"
}
}
- 感觉还是可以的,下次还是希望返回的json能正常的显示,通俗易懂。