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

穿精又带淫゛_ 提交于 2019-12-10 16:40:09

问题


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. When doing POST on the POJO, the request body is sent as a JSON, essentially modeling the POJO. When doing GET, I want to return the POJO, but with only a subset of the POJO properties.

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.

First, here is my 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:

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:

{
  "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:

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

回答1:


You could always return a partially populated User object as by default null values are not marshalled. You may even be able to implement this in your retrieveUserFromDB method to avoid the copy step.

  @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

  • http://blog.bdoughan.com/2012/04/binding-to-json-xml-handling-null.html


来源:https://stackoverflow.com/questions/11107874/jersey-jaxb-use-same-pojo-for-http-post-and-get-but-return-only-a-subset-of-th

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!