简体   繁体   English

如何在JAXRS中将字段标记为不由JAXB扩展

[英]How to mark a field as not to be expanded by JAXB in JAXRS

I have 2 classes as follows: 我有2个类,如下所示:

@XmlRootElement
@PersistenceCapable(detachable="true")
public class User {

    @Persistent(primaryKey="true", valueStrategy=IdGeneratorStrategy.IDENTITY)
    private Long id;

    private String firstname;
    private String lastname;
    private UserDetails userDetails;
...
}

and

@XmlRootElement
@PersistenceCapable(detachable="true")
public class UserDetails {
    private String streetaddress;
    private String address;
    private String city;
    private int pincode;
    private Date dateofbirth;
...
}

I need to retrieve the user objects only, without having the userdetails in one JAXRS function, and both the user object and its attached userdetails object in another as shown. 我只需要检索用户对象,而不必在一个JAXRS函数中包含userdetails,并且如图所示,用户对象及其附加的userdetails对象都在另一个JAXRS函数中。

//UserManager.java
public List<User> getUsers() {
    javax.jdo.Query q = pm.newQuery("SELECT FROM "+ User.class.getName());
    List<User> users = (List<User>) q.execute();
    return users;
}

and the JAXRS service: 和JAXRS服务:

@GET
@Produces({MediaType.APPLICATION_JSON})  
public List<User> getUsers() {        //Retrieves all users and user details
    UserManager um=new UserManager(); //working perfectly
    return um.getUsers();
}

@GET
@Produces({MediaType.APPLICATION_JSON})  
public List<User> getUsers() {        //should retrieve user only, but how?
    UserManager um=new UserManager();
    return ?????
}

How can I prevent the UserDetails object from being expanded by JAXRS and JAXB for the second function??? 如何防止UserDetails对象被JAXRS和JAXB扩展为第二个功能???

if you are returning same type from both functions then jaxb can't help you in hiding information from one method and showing in second. 如果您从这两个函数返回相同的类型,那么jaxb不能帮助您从一种方法中隐藏信息并在第二种方法中显示。

there are two ways to achieve this: 有两种方法可以实现此目的:

1) either return different user object fron second function which does not aggregate userdetails. 1)从第二个函数返回不同的用户对象,该第二个函数不聚合用户详细信息。 2) in second function write a query that does not populate user details data. 2)在第二个函数中编写一个不填充用户详细信息数据的查询。

You can combine the use of JDO fetch plans and lifecycle policies. 您可以结合使用JDO提取计划和生命周期策略。

Create a named fetch group for your userDetails eg 为您的userDetails创建一个命名获取组,例如

@FetchGroup(name="details", members={@Persistent(name="userDetails")})

When you want everything, you don't have to do anything technically because JAXB is navigating the graph, but to be complete you'd specify this fetch plan and make your returned object transient following the fetch plan, eg: 当您需要所有内容时,无需进行任何技术上的操作,因为JAXB正在浏览图形,但是要完全完成,您可以指定此获取计划,并使返回的对象在获取计划之后成为瞬态,例如:

pm.getFetchPlan().addGroup("details");
[...]
return (List<User>)pm.makeTransientAll(um.getUsers(), true); // use fetch plan

When you want the shallow fetch, use the default fetch plan and make your returned object transient, eg: 当需要浅层获取时,请使用默认的获取计划并使返回的对象成为瞬态,例如:

return (List<User>)pm.makeTransientAll(um.getUsers(), true); // use fetch plan

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM