JAX-RS多态POST请求:我应该如何编写JSON? [英] JAX-RS polymorphic POST request: how should I write my JSON?
问题描述
我在尝试用JAX-RS解决此问题时遇到了麻烦,我认为这与编组/解组过程有关(我想我不太了解),并且我想重新创建这个:
I'm having a bad time trying to solve this problem with JAX-RS and I believe that it has something to do with the marshalling/unmarshalling process (that I don't know very much about, I assume), and I want to recreate this:
-
要发布信息的REST端点是/rest/register,因此我的服务定义如下:
The REST endpoint to make a post is /rest/register so my services are defined like this:
@ApplicationPath("/rest")
public interface RestRegistration {
@POST
@Path("/register")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
String register(UsernameRegistration usernameAccess, String network) throws RegistrationException;
@POST
@Path("/register")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
String register(EmailRegistration emailAccess, String network) throws RegistrationException;
}
public class RestRegistrationImpl implements RestRegistration {
public String register(UsernameRegistration usernameAccess, String network) throws RegistrationException {
return "StackOverflow example: username=" + usernameAccess.getUser + ", network="+network;
}
public String register(EmailRegistration emailAccess, String network) throws RegistrationException{
return "StackOverflow example: email=" + emailAccess.getEmail + ", network="+network;
}
}
它应该至少接收两个不同的JSON消息:
It should receive at least two different JSON messages:
{ "usernameAccess" : { "user" : "AUser", "password" : "APass"}, "network" : "Facebook"}
或...
{ "emailAccess" : { "email" : "AnEmail@domain.com", "password" : "APass"}, "network" : "LinkedIn"}
这些类由
The classes are represented by
public abstract class Registration {
private Long _id;
private String _password;
private String _network;
// usual getters and setters...
}
public class UsernameRegistration extends Registration {
private String _user;
// usual getters and setters...
}
public class EmailRegistration extends Registration {
private String _email;
// usual getters and setters...
}
所以,我现在的问题是:
So, my problem now is:
- 通过此实现,我不断收到HTTP 500错误.
- 我不确定如何分隔两个JSON节点:usernameAccess/emailAccess和network.
- 也:使用"UsernameRegistration"和"EmailRegistration"两个类都继承自相同的"Registration",使"upper"上的类看起来更加优雅,有什么办法可以改进我? /li>
- With this implemenationt I keep receiving an HTTP 500 error.
- And I'm not sure on how to separate the two JSON nodes: usernameAccess/emailAccess and network.
- Also: is there any way on how can I improve using the fact that both classes "UsernameRegistration" and "EmailRegistration" inherit from the same "Registration", making the classes on the "upper" 1. look more elegant?
有人可以提供我的书目,文章或方法吗? 提前致谢! :)
Is there any bibliography, article or howto someone can hand me over? Thanks in advance! :)
推荐答案
在Jackson中,您要注册的每个实现的继承结构都需要包含@JsonTypeInfo
和@JsonSubTypes
以及@JsonSubTypes.Type
. JsonTypeInfo将需要一个唯一的标识符来区分类型.
In Jackson, your inheritance structure needs to include @JsonTypeInfo
and @JsonSubTypes
with a @JsonSubTypes.Type
for each implementation that you're registering. JsonTypeInfo will require a unique identifier to differentiate the types.
这篇关于JAX-RS多态POST请求:我应该如何编写JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!