我在尝试用 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"}
or...
{ "emailAccess" : { "email" : "AnEm[email protected] /cdn-cgi/l/email-protection", "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...
}
所以,我现在的问题是:
- 通过此实现,我不断收到 HTTP 500 错误。
- 我不确定如何分离两个 JSON 节点:usernameAccess/emailAccess 和网络。
- 另外:有没有什么方法可以使用“UsernameRegistration”和“EmailRegistration”类继承自同一个“Registration”这一事实来改进,使“上层”1.上的类看起来更优雅?
有没有任何参考书目、文章或指南可以交给我?
提前致谢! :)
在杰克逊,你的继承结构需要包括@JsonTypeInfo
and @JsonSubTypes
with a @JsonSubTypes.Type
对于您正在注册的每个实现。 JsonTypeInfo 将需要唯一标识符来区分类型。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)