如何正确解组JAX-RS Webservice响应(JSON数组)? [英] How to unmarshall JAX-RS Webservice response (JSON Array) correctly?

查看:158
本文介绍了如何正确解组JAX-RS Webservice响应(JSON数组)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有以下域名类:

  @XmlRootElement 
public class User {

private long id;
私人字符串电子邮件;
private String forename;
私人字符串角色;
$ b $ public User(){
// JAXB
所需的值}

// + Getters + Setters
}

和以下资源类:

  @Produces(MediaType.APPLICATION_JSON)
@Path(/ users)
public class UserService {

@GET
public User [] getUsers(){
User [] users = ... //来自DB
的用户返回用户;






如果我要求资源(/ users)以下的json响应:

  {
user:
[
{
id:1,
email:Toby@email.com,
forename:Toby,
role:admin
}

{
id:2,
email:Rob@email.com,
forename: Rob,
角色:开发者
}
]
}

在客户端(我有相同的User.class),我想将这个JSON响应解组到相应的POJO。这意味着在这个例子中,我想获得两个类型的用户对象。我使用GSON做了一些实验性的东西,比如

  User [] users = gson.fromJson(JSONString,User []。class)

但无法使其正常工作。例外:

  JSONParseException:期望对象,但找到数组

任何人都可以告诉我Iam在这里做错了什么吗?或者是我的json格式的问题?



编辑:
使用自己的集合类型适配器实现前面提到的<

  Type usersType = new TypeToken< List< User>>(){} .getType(); 
列表<用户> users = new ArrayList< User>();
users = gson.fromJson(reader,usersType);

现在Iam得到一个JsonParseException:

<$ p JsonDeserializer无法反序列化Json Object {user:[{id:1,email:Toby@email.com,forename:Toby ,....


解决方案

JSON响应!在此帖子的帮助下,我绕过了以Object-> XML-> JSON方式直接进行Object-> JSON转换,从而导致由gson解释的干净的JSON语法。将尽快用我的完整示例源更新答案。


I have some problem using Jersey REST webservices.

I have the following domain class:

@XmlRootElement
public class User {

    private long id;
    private String email;
    private String forename;
    private String role;

    public User() {
        // required for JAXB
    }

    // + Getters + Setters
}

and the following resource class:

@Produces(MediaType.APPLICATION_JSON)
@Path("/users")
public class UserService {

    @GET
    public User[] getUsers() {
        User[] users = ... // Users from DB 
        return users;
    }
}

If I request that resource (/users) I get the following json response:

{
 "user":  
 [
  {
   "id":"1",
   "email":"Toby@email.com",
   "forename":"Toby",
   "role":"admin"
  }
 ,
  {
   "id":"2",
   "email":"Rob@email.com",
   "forename":"Rob",
   "role":"developer"
  }
 ]
}

On the client side (where I have the same User.class) I want to unmarshall this JSON response back to the corresponding POJOs. That means in this example I would like to get two Object of type User. I did some experimental stuff with GSON like

User[] users = gson.fromJson(JSONString, User[].class)

but was not able to get it working. Exception:

JSONParseException: Expecting object but found array

Can anyone tell me what Iam doing wrong here? Or is my json format the problem?

Edit: Tried it with an own implementation of a collection type adapter as mentioned here:

Type usersType = new TypeToken<List<User>>() {}.getType();
List<User> users = new ArrayList<User>();
users = gson.fromJson(reader, usersType);

Now Iam getting an JsonParseException:

The JsonDeserializer failed to deserialize Json Object {"user":[{"id":"1","email":"Toby@email.com","forename":"Toby",....

解决方案

it really was the format of the json response! with the help of this post I bypassed the Object->XML->JSON way to a direct Object->JSON transformation which leads to a clean JSON syntax interpretable by gson. will update the answer with my full example source soon.

这篇关于如何正确解组JAX-RS Webservice响应(JSON数组)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆