JAX-RS 多态 POST 请求:我应该如何编写 JSON?

3

我在尝试解决JAX-RS相关问题时遇到了困难,我认为这与编组/解组过程有关(而我并不是很了解),我希望重新创建以下代码:

  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 "StackOverflow example: username=" + usernameAccess.getUser + ", network="+network;
        }
    
        public String register(EmailRegistration emailAccess, String network) throws RegistrationException{
            return "StackOverflow example: email=" + emailAccess.getEmail + ", network="+network;
        }
    
    }
    
  2. It should receive at least two different JSON messages:

    { "usernameAccess" : { "user" : "AUser", "password" : "APass"}, "network" : "Facebook"}
    

    or...

    { "emailAccess" : { "email" : "AnEmail@domain.com", "password" : "APass"}, "network" : "LinkedIn"}
    
  3. 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...
    }
    
所以,我的问题是:
  1. 使用这个实现方式,我一直收到HTTP 500错误。
  2. 我不确定如何分离两个JSON节点:usernameAccess / emailAccess和network。
  3. 此外:有没有办法利用“UsernameRegistration”和“EmailRegistration”两个类都继承自同一个“Registration”类来改进代码,使得代码看起来更加优雅?

是否有任何文献、文章或教程可以提供给我呢? 提前感谢! :)


更新:关于我的问题2,我刚刚发现我不能按照我想要的方式在单个POST上发送两个不同的对象。 解决方案可以在这里找到: https://dev59.com/RW035IYBdhLWcg3wNtRw - Hugo Tavares
2
你能发布一下你收到的错误信息和服务器上的任何适用的堆栈跟踪吗?你正在使用哪个JSON提供程序? - John Ament
@JohnAment:我正在使用Jackson。错误信息大致如下:(异常) 由于抽象类型只能通过额外的类型信息进行实例化,因此无法构造...Registration的实例。(谢谢!);) - Hugo Tavares
1个回答

5
在Jackson中,你的继承结构需要包含@JsonTypeInfo@JsonSubTypes,并且对于每个要注册的实现都需要有@JsonSubTypes.Type。JsonTypeInfo将需要一个唯一标识符来区分类型。

我在这里找到了解决方法:http://wiki.fasterxml.com/JacksonPolymorphicDeserialization。一开始我不确定是否是同一个问题,但最终通过该解决方案,我成功地处理了UserRegistration/EmailRegistration的子类。再次感谢您,John。 - Hugo Tavares
有没有不使用注释的方法?因为我依赖于一个依赖项的模型,而我没有这个依赖项的源代码。 - xxfast
你可以将其作为自定义的JAX-RS MessageBodyReader/MessageBodyWriter来完成,但是我担心这种方式会尝试使用外部模型。我发现这有点反模式,因此建议人们提出基于自己领域的模型表示。这样,您只需读取您关心的属性并更好地封装即可。 - John Ament

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接