@Post Jersey 在带有 javax.ws.rs.POST 注释的方法中只允许一个未注释的参数

@Post Jersey Only one unannotated parameter is allowed in a method with javax.ws.rs.POST annotation

我正在使用 Jersey 并提议做一个网络服务。

我正在尝试做的是一种@Post 方法,该方法按形式和 url 组合 1 个参数。

@POST
@Path("/{contestType}/pay")
public Response pay(@PathParam("contestType") String contestType,
        @FormParam("contestId") Long contestId, @FormParam("username") String username,
        @FormParam("amount") Long amount)
{
    User user = dao.getByKey(User.class, username);
    if (user == null)
        return Response.status(Status.NOT_FOUND.getStatusCode(), "username not valid").build();

    Contest contest = dao.getByKey(Contest.class, contestId);
    if (contest == null)
        return Response.status(Status.NOT_FOUND.getStatusCode(), "contest not valid").build();

    try
    {
        if (contestType.equals("raffles"))
            user.pay(contest, amount);
        else
            user.pay(contest);
    }
    catch (ContestException | PaymentException e)
    {
        Logger.getGlobal().log(Level.WARNING, e.getLocalizedMessage(), e);
    }
    return Response.ok().build();
}

但是当我这样做时,我收到了来自 eclipse 的警告:在带有 javax.ws.rs.POST 注释的方法中只允许一个未注释的参数。 而且当执行时我得到下一个错误:

message java.lang.IllegalStateException:当请求实体的内容类型不是application/x-www-form-urlencoded

时使用@FormParam
javax.servlet.ServletException: java.lang.IllegalStateException: The @FormParam is utilized when the content type of the request entity is not application/x-www-form-urlencoded
    org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:432)
    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:370)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:389)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:342)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:229)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
causa raíz

我需要从客户端接收一个Json,所以我不能使用application/x-www-form-urlencoded,也不想为每个@Post创建无限的POJO对象我有,解决这个问题的正确方法是什么?

@FormParam 应该用于负载是表单的请求(并且该方法用 @Consumes 注释,值如 MediaType.APPLICATION_FORM_URLENCODED)。

考虑以下代码:

@POST
@Path("/{contestType}/pay")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response pay(@PathParam("contestType") String contestType,
                    @FormParam("contestId") Long contestId, 
                    @FormParam("username") String username,
                    @FormParam("amount") Long amount) {
    ...
}

它需要以下格式的输入:

POST /api/foo/pay HTTP/1.1
Host: example.org
ContentType: application/x-www-form-urlencoded

contestId=1&username=johndoe&amount=1000

如果您的负载是 JSON 文档,那么 @FormParam 不适合您。 JAX-RS 运行时不会从负载中的 JSON 文档中提取值并将它们绑定到方法参数。 在这种情况下,定义一个 class 到将 JSON 文档绑定到:

public class PaymentDetails {

    private Long contestId;
    private String username;
    private Long amount;

    // Getters and setters
}
@POST
@Path("/{contestType}/pay")
@Consumes(MediaType.APPLICATION_JSON)
public Response pay(@PathParam("contestType") String contestType,
                    PaymentDetails paymentDetails) {
    ...
}