使用 POST 传递对象

Passing Object Using POST

我试图在 REST 中传递一个参数,但它抛出了以下异常:

Mar 15, 2015 3:09:00 AM com.sun.jersey.spi.container.ContainerRequest getEntity SEVERE: A message body reader for Java class com.backend.service.impl.resource.DummyParam, and Java type class com.backend.service.impl.resource.DummyParam, and MIME media type application/x-www-form-urlencoded was not found. The registered message body readers compatible with the MIME media type are: application/x-www-form-urlencoded ->   com.sun.jersey.core.impl.provider.entity.FormProvider   com.sun.jersey.core.impl.provider.entity.FormMultivaluedMapProvider

代码片段如下:

   @Path("/test/dummy")
public class DummyService {

    @POST
    @Consumes({MediaType.APPLICATION_FORM_URLENCODED})
    @Produces({MediaType.APPLICATION_FORM_URLENCODED, MediaType.APPLICATION_JSON})
    @Path("/doStuff1")
    public Response doStuff1(DummyParam param) {
        System.out.println("Hiiiiiiiiii : "+param.getUsername());
        return Response.ok().build();
    }
}
@XmlRootElement(name = "dummyparam")
public class DummyParam {
    private String username;
    private String password;

    @XmlElement(name = "user")
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }

    @XmlElement(name = "pwd")
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }



}

有人可以帮我解决这个问题的原因吗?

您似乎需要为您的 DummyParam class 提供自定义 MessageBodyReader 才能在您的请求处理程序中传递。 看看这里的例子 https://jersey.java.net/documentation/latest/message-body-workers.html