[英]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.