JAX-RS端点中的BadRequestException

时间:2014-12-16 14:48:18

标签: java curl jax-rs

我在JAX-RS应用程序中声明了以下端点:

@WebService
public interface AlertWeb
{
    @POST
    @Path("/add")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public StringResponse addAlert(String name,
            int amount, String timespan, String repo, String action);

}

我正在使用以下curl命令来调用此端点:

curl -X POST -H "Cache-Control: no-cache" 
-H "Content-Type: application/x-www-form-urlencoded" 
-d "name=yellow&amount=2&timespan=DAY&repo=A&action=%7Baction%3A'GreenDivAction'%2C+message%3A'Simple+Message'%2C+args%3A+%5B'arg1'%2C'arg2'%5D%7D"
http://localhost:8080/AdminService/alert/add

但在发出请求时不断收到以下错误:

javax.ws.rs.BadRequestException: java.lang.NumberFormatException: For input string: ""

注意为了便于阅读,添加了curl语法中的换行符。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

如果您希望将它们注入方法参数,则需要将@FormParam添加到

@POST
@Path("/add")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public Response addAlert(
        @FormParam("name") String name,
        @FormParam("amount") int amount, 
        @FormParam("timespan") String timespan, 
        @FormParam("repo") String repo, 
        @FormParam("action") String action) {
}