我正在努力用JAX-RS解决这个问题,我相信它与编组/解组过程有关(我不太了解,我认为),以及我想重新创建这个:
发布帖子的REST端点是/ rest / register,所以我的服务定义如下:
@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消息:
{ "usernameAccess" : { "user" : "AUser", "password" : "APass"}, "network" : "Facebook"}
...或
{ "emailAccess" : { "email" : "AnEmail@domain.com", "password" : "APass"}, "network" : "LinkedIn"}
这些类由
表示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...
}
所以,我现在的问题是:
是否有任何参考书目,文章或如何有人可以把我交给我? 提前致谢! :)
答案 0 :(得分:4)
在Jackson中,您的继承结构需要包含@JsonTypeInfo
和@JsonSubTypes
,并为您正在注册的每个实现添加@JsonSubTypes.Type
。 JsonTypeInfo将需要唯一标识符来区分类型。