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

时间:2019-04-17 09:57:48

标签: java json rest jax-rs

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

我用键值对创建了一个JSON对象。并返回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
}

5 个答案:

答案 0 :(得分:0)

您不返回List,而是返回JSONObject。 有两种解决方法,要么更改返回类型,要么更改您想要返回json对象的方式。

答案 1 :(得分:0)

您可以创建Wrapper或Response类,而不是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){}

答案 2 :(得分:0)

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

答案 3 :(得分:0)

您可以使用Response

JSONObject json = new JSONObject();

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

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

答案 4 :(得分:0)

使用List或您的Subject类创建一个Response类,如下所示。将List用于多个学生。

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

您的班级应该看起来像。

    @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(将其谷歌搜索)。谁调用您的SubjectResources类,他们都会以json的形式获取Response类。 Spring会自动转换为json.If要测试您的类,请使用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>

如果您是为学习而开发的,并且想在不使用皂UI的情况下测试您的班级,请使用ObjectMapper类。在您的班级中放置以下代码

String jsonStr="{}";
try{
ObjectMapper mapper=new ObjectMapper();
jsonStr=mapper.writeValueAsString(resp);
System.out.println(jsonStr);