81,092
社区成员
发帖
与我相关
我的任务
分享
public String responseMap(
int code,
String msg,
Object info,
Map<String, Object> params
) {
Map<String, Object> map = new HashMap<String, Object>();
String result = code == 200 ? "true" : "false";
map.put("result", result);
map.put("msg", msg);
map.put("info", info);
// if (info == null) {
// map.put("info", "");
// } else
// map.put("info", info);
if (null != params) {
for (String key : params.keySet()) {
map.put(key, params.get(key));
}
}
// Gson gson = new Gson();
String s = null;
Gson gson = new GsonBuilder()
.serializeNulls()
.setDateFormat("yyyy-MM-dd HH:mm:ss")
.create();
String json = gson.toJson(map);
return json;
}
,具体运用就是String json = responseMap(200, "连接成功", menuList, null);
package com.appleyk.controller;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
class A{
public A(String name,int age){
this.name = name ;
this.age = age ;
}
String name;
int age ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
@Controller
@RequestMapping("/api/v1.0.1/test")
public class TestController {
@RequestMapping("/hello")
@ResponseBody
public List<A> getAList(HttpServletResponse response){
List<A> lists = new ArrayList<>();
A a = new A("appleyk",27);
lists.add(a);
return lists;
}
}