我在尝试解决JAX-RS相关问题时遇到了困难,我认为这与编组/解组过程有关(而我并不是很了解),我希望重新创建以下代码:
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; } }
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"}
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... }
- 使用这个实现方式,我一直收到HTTP 500错误。
- 我不确定如何分离两个JSON节点:usernameAccess / emailAccess和network。
- 此外:有没有办法利用“UsernameRegistration”和“EmailRegistration”两个类都继承自同一个“Registration”类来改进代码,使得代码看起来更加优雅?
是否有任何文献、文章或教程可以提供给我呢? 提前感谢! :)