小编Hug*_*res的帖子

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

我在尝试用JAX-RS解决此问题时遇到了麻烦,我认为这与编组/解组过程有关(我想我不太了解),我想重新创建此:

  1. 进行发布的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;
        }
    
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 它应该至少接收两个不同的JSON消息:

    { "usernameAccess" …
    Run Code Online (Sandbox Code Playgroud)

java rest json jax-rs marshalling

2
推荐指数
1
解决办法
2153
查看次数

标签 统计

java ×1

jax-rs ×1

json ×1

marshalling ×1

rest ×1