JAX-RS多态POST请求:我应该如何编写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:

  1. 要发布信息的REST端点是/rest/register,因此我的服务定义如下:

  1. 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 "* example: username=" + usernameAccess.getUser + ", network="+network;
    }

    public String register(EmailRegistration emailAccess, String network) throws RegistrationException{
        return "* 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:

    1. 通过此实现,我不断收到HTTP 500错误.
    2. 我不确定如何分隔两个JSON节点:usernameAccess/emailAccess和network.
    3. 也:使用"UsernameRegistration"和"EmailRegistration"两个类都继承自相同的"Registration",使"upper"上的类看起来更加优雅,有什么办法可以改进我? /li>
    1. With this implemenationt I keep receiving an HTTP 500 error.
    2. And I'm not sure on how to separate the two JSON nodes: usernameAccess/emailAccess and network.
    3. 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.