如何通过代码、消息和列表传递 JSON 对象?

How to pass JSON object with code, message and list?

在我的休息网站 API 中,如果用户发送带有学生 ID 的请求,作为响应,API 会发送消息、代码和学生创建的主题列表ID。 如何发送包含所有这些详细信息的回复

我用键值对创建了一个 JSON 对象。和 return JSON 对象作为响应。

public class SubjectResources {

  SubjectService subjectService = new SubjectService();

  @GET
  @Produces(MediaType.APPLICATION_JSON)
  public List<Subject> getSubjectList(@QueryParam("user_id") long user_id) {

  List<Subject> sub = subjectService.getAllSubejcts(user_id);//checked and List sub has all details.

  JSONObject json = new JSONObject();

  json.put("code", 200);
  json.put("message", "Success");
  json.put("subject", sub);`

  return json;
}

预期结果: 响应格式应为:

{ 
   "code":200, 
   "message":"Success", 
   "subjects":
         {
            "id":"1", 
            "name":"Maths", 
            "total_chapters":"3", 
            "total_assignments":"3"
          },
        {
            "id":"2", 
            "name":"Maths2", 
            "total_chapters":"4", 
            "total_assignments":"5"
          },


.....}

实际结果 :

{
     "empty" :false
}

您不是 return 列表,您是 return JSONObject。 有两种方法可以解决这个问题,要么更改 return 类型,要么更改您想要 return json 对象的方式。

您可以创建包装器或响应 class 而不是 JSON 对象

public class Response{
private String code;
private String msg;
private Subject subject;
//Add getter setter here

}

现在在您的代码中

Response res = new Response;

//设置值后

return res;

此外,更改响应类型如下

public Response getSubjectList(@QueryParam("user_id") long user_id){}

我认为您应该将函数的 return 类型更改为 JSONObject。

您可以使用Response

JSONObject json = new JSONObject();

json.put("code", 200);
json.put("message", "Success");
json.put("subject", sub);

return Response.ok().entity(json).build();

使用列表或您的主题 class 创建响应 class,如图所示 below.Use 多个学生的列表。

public class Response{
private String code;
private String msg;
// private Subject subject; 
        // (OR)
 private List<Subject> subject;
// getters and setters
}

您的 class 应该是这样的。

    @RestController
public class SubjectResources {

SubjectService subjectService = new SubjectService();

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getSubjectList(@QueryParam("user_id") long user_id){

List<Subject> sub = subjectService.getAllSubejcts(user_id);//checked and List sub has all details.

Response resp=new Response();
resp.setCode("--------");
resp.setMsg("-------");
resp.setSubject(sub);
return resp;
}

使用@RestController 并在spring dispatcher-servlet 文件中配置json 转换器。你也可以使用@ResponseBody(google它)。无论谁调用您的 SubjectResources class,他们都会以 json 的形式收到响应 class。 Spring 会自动转换成 json.If 你想测试你的 class 使用 Soup UI 工具。

<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jsonConverter" />
            </list>
        </property>
    </bean>

如果您正在为学习而开发并想在没有 soap ui 的情况下测试您的 class,请在您的 class

中使用下面代码中的 ObjectMapper class.Place
String jsonStr="{}";
try{
ObjectMapper mapper=new ObjectMapper();
jsonStr=mapper.writeValueAsString(resp);
System.out.println(jsonStr);