简体   繁体   English

Jersey / JAXB:对HTTP POST和GET使用相同的POJO,但在JSON响应中仅返回GET的一部分属性。

[英]Jersey/JAXB: Use same POJO for HTTP POST and GET, but return only a subset of the properties for GET in JSON response.

This seems like another fairly simple thing to do, but I'm again struggling on how to do it. 这似乎是另一个相当简单的事情,但我再次在如何做到这一点上苦苦挣扎。

I have a POJO, with Jersey/JAXB annotations that has HTTP POST and GET methods associated to it. 我有一个POJO,其中包含与之关联的HTTP POST和GET方法的Jersey / JAXB注释。 When doing POST on the POJO, the request body is sent as a JSON, essentially modeling the POJO. 在POJO上进行POST时,请求体将作为JSON发送,基本上是对POJO进行建模。 When doing GET, I want to return the POJO, but with only a subset of the POJO properties. 在进行GET时,我想返回POJO,但只有POJO属性的子集。

I tried using @XmlTransient on the properties I don't want for the GET, but then I cannot use those properties during the HTTP POST. 我尝试在我不想要GET的属性上使用@XmlTransient,但是在HTTP POST期间我不能使用这些属性。

First, here is my POJO (User.java) 首先,这是我的POJO(User.java)

import javax.xml.bind.annotation.*
@XmlRootElement
public class User {
  private String userName;
  private String userEmail;
  private String userType;  // Do not return this property in GET
  private String userTmpPassword;  // Do not return this property in GET

  // User constructor
  public User(String userName,...) {
    this.userName = userName;
    //...etc...
  }

  // getters and setters with @XmlElement on each attribute 
  //...etc...
  @XmlElement(name="user_name")
  public String getUserName() {
    return userName;
  }
  public String setUserName() {
    return userName;
  }
  //...etc...
}

Here is my RESTful service class: 这是我的RESTful服务类:

public class userService{
  @POST
  @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
  public Response getUser(User userInfoAsJSON) {
    User user = new User(userInfoAsJSON.getUserName(), ...);
    // pseudo-code for persisting User
    writeUserToDB(user);
    return Response.status(200);
  }

  @GET
  @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
  public Response getUser(String Id) {
    // Pseudo-code for retrieving user
    User user = retrieveUserFromDB(user);
    Response.status(200).entity(user);
  }
}

As expected, my JSON response for the HTTP GET returns all the properties of User, like this: 正如所料,我对HTTP GET的JSON响应返回User的所有属性,如下所示:

{
  "user_name": "John Doe",
  "user_email": "john_doe@johndoe.com",
  "user_type": "Admin",
  "user_tmp_password": "abc_xyz"
}

Whereas, I would like to only return a couple attributes in the JSON response: 然而,我只想在JSON响应中返回一些属性:

{
  "user_name": "John Doe",
  "user_email": "john_doe@johndoe.com"
}

You could always return a partially populated User object as by default null values are not marshalled. 您始终可以返回部分填充的User对象,因为默认情况下不会编组空值。 You may even be able to implement this in your retrieveUserFromDB method to avoid the copy step. 您甚至可以在retrieveUserFromDB方法中实现此功能,以避免复制步骤。

  @GET
  @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
  public Response getUser(String Id) {
    // Pseudo-code for retrieving user
    User userFromDB = retrieveUserFromDB(user);
    User user = new User();
    user.setUserName(userFromDB.getUserName());
    user.setUserEmail(userFromDB.getUserEmail());
    Response.status(200).entity(user);
  }

For More Information 欲获得更多信息

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

相关问题 Jersey JSON / JAXB忽略或看不到POJO中的“某些”公共属性 - Jersey JSON/JAXB Ignoring or not seeing “some” public properties in POJO 泽西岛HTTP响应返回json值 - Jersey HTTP Response return json value Java Jersey使用GET返回仅返回一些字段而不是全部字段的JSON - Java Jersey use GET to return JSON that returns only some fields instead of all 对于Jersey / JAXB,是否可以使用相同的POJO作为“父”和“子”,但在用作“孩子”时删除某些属性? - For Jersey/JAXB, is it possible to use the same POJO as both 'parent' and 'child' but remove certain attributes when being used as a 'child'? 泽西岛-在WADL中获取HTTP响应状态 - Jersey - Get the HTTP response status in WADL 从Glassfish Jersey Rest服务器获取响应Json对象时获取HTTP错误:500 - Get HTTP ERROR: 500 on get response Json Object from glassfish Jersey Rest server 使用Jaxb / Jersey将动态JSON对象映射到Java POJO - Mapping a dynamic JSON object into Java POJO with Jaxb / Jersey 获取POST的jSON响应 - Get jSON Response for POST 我如何发出HTTP POST请求并使用我应该在android中获得的Json响应 - How can i make an HTTP POST request and and use the Json Response i should get in android 无法对json / xml响应使用模拟HTTP / HTTPS获取发布请求 - Unable to use mock HTTP/HTTPS get post request for json/xml response
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM