我正在使用Spring MVC并尝试从我的Controller返回 JSONObject 作为响应。我使用 @ResponseBody 注释了该方法,以便将我的控制器返回的 JSONObject 放入 ResponseBody 。 这是我的控制器:
@GetMapping(value="/student/{roll}",produces="application/json")
@ResponseBody
private JSONObject getStudentDetails(@PathVariable(value="roll") String roll) {
JSONObject response = new JSONObject();
Student student = studentDAO.getStudent(roll);
response.put("firstName",student.getFirstName());
response.put("lastName",student.getLastName());
response.put("roll",student.getRoll());
response.put("email",student.getEmail());
response.put("course",student.getCourse());
response.put("stream",student.getStream());
response.put("year",student.getYear());
response.put("gender",student.getGender());
String date = null;
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
date = String.valueOf(df.parse(student.getSignUpDate()).getTime());
} catch (ParseException e) {
e.printStackTrace();
}
response.put("signUpDate", date);
System.out.println("Response Body::::: "+response.toString());
return response;
}
有效回复应如下:
{
"firstName": "John",
"lastName": "Doe",
"gender": "M",
"stream": "cse",
"year": 3,
"roll": "2013BT2011",
"course": "btech",
"signUpDate": "1476224877000",
"email": "john@doe.com"
}
但我得到了这个:
{
"map": {
"firstName": "John",
"lastName": "Doe",
"gender": "M",
"stream": "cse",
"year": 3,
"roll": "2013BT2011",
"course": "btech",
"signUpDate": "1476224877000",
"email": "john@doe.com"
}
}
这里,我的控制器返回的对象被包装到 map 对象中,然后由Spring返回。
有人能告诉我这里有什么问题。 任何帮助,将不胜感激。 :)
答案 0 :(得分:2)
spring mvc使用jackson databind将Object序列化为JSON /将JSON反序列化为Object。 因此不需要使用@ResponseBody返回JSONObject。 有一些方法:
使用need字段定义一个类(视图对象),然后选择new,填充一个实例并返回它。
使用java.util.Map。所以你的代码将如下:
@GetMapping(value="/student/{roll}",produces="application/json")
@ResponseBody
private Map<String, Object> getStudentDetails(@PathVariable(value="roll") String roll) {
Map<String, Object> response = new HashMap<>();
Student student = studentDAO.getStudent(roll);
response.put("firstName",student.getFirstName());
response.put("lastName",student.getLastName());
response.put("roll",student.getRoll());
response.put("email",student.getEmail());
response.put("course",student.getCourse());
response.put("stream",student.getStream());
response.put("year",student.getYear());
response.put("gender",student.getGender());
String date = null;
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
date = String.valueOf(df.parse(student.getSignUpDate()).getTime());
} catch (ParseException e) {
e.printStackTrace();
}
response.put("signUpDate", date);
System.out.println("Response Body::::: "+response.toString());
return response;
}
您可以在学生班(https://github.com/FasterXML/jackson-annotations/wiki/Jackson-Annotations)上使用正确的杰克逊注释返回学生(PO)。如果要限制返回的字段,可以添加JsonView注释。
public class Student {
public static class Response {}
private String firstName;
private String lastName;
private String roll;
private String email;
private String course;
private String stream;
private String year;
private String gender;
private Date getSignUpDate;
@JsonView(Response.class)
public String getFirstName() {
return firstName;
}
@JsonView(Response.class)
public String getLastName() {
return lastName;
}
@JsonView(Response.class)
public String getRoll() {
return roll;
}
@JsonView(Response.class)
public String getEmail() {
return email;
}
@JsonView(Response.class)
public String getCourse() {
return course;
}
@JsonView(Response.class)
public String getStream() {
return stream;
}
@JsonView(Response.class)
public String getYear() {
return year;
}
@JsonView(Response.class)
public String getGender() {
return gender;
}
@JsonView(Response.class)
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
public Date getGetSignUpDate() {
return getSignUpDate;
}
/// setters are not written.
}
public class StudentCtl {
@GetMapping(value="/student/{roll}",produces="application/json")
@ResponseBody
@JsonView(Student.Response.class)
private Map<String, Object> getStudentDetails(@PathVariable(value="roll") String roll) {
return studentDAO.getStudent(roll);
}
}
答案 1 :(得分:1)
假设您已经拥有包含这些属性的学生课程。因此,你可以做的就是简单地将对象返回到下面,希望这应该可行。
private JSONObject getStudentDetails(@PathVariable(value="roll") String roll) {
Student student = studentDAO.getStudent(roll);
System.out.println("Response Body::::: "+response.toString());
return student ;
}
即使您想要更改这些字段的名称,也可以像
一样注释它们class Student {
@JsonProperty("firstName")
private String fname;
@JsonProperty("signUpDate")
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")
private Date date;
//getters
//setters
}
答案 2 :(得分:0)
你可以简单地从Controller方法返回学生弹簧将进行编组。
@GetMapping(value="/student/{roll}",produces="application/json")
@ResponseBody
private Student getStudentDetails(@PathVariable(value="roll") String roll) {
return studentDAO.getStudent(roll);
}
但是由于您正在进行日期格式化,我还建议您创建一个类似StudentView的单独类,并将学生映射到StudentView
class StudentView{
private String signUpDate;
......
......
}
并从控制器返回studentView。所以它会像
@GetMapping(value="/student/{roll}",produces="application/json")
@ResponseBody
private StudentView getStudentDetails(@PathVariable(value="roll") String roll) {
Student student=studentDAO.getStudent(roll);
return toStudentView(student);
}
private static StudentView toStudentView(Student stu){
......
}