1

I have the following endpoint declared in my JAX-RS application:

@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);

}

I'm using the following curl command to call this endpoint:

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

but keep getting the following error when I make the request:

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

Note Line breaks in curl syntax added for readability.

What am I doing wrong?

7
  • Works fine for me, after adding all the @FormParam annotations to the parameters Commented Dec 16, 2014 at 15:24
  • That's correct. I just discovered this after some additional googling. Please add as an answer and I'll accept.
    – JSK NS
    Commented Dec 16, 2014 at 15:25
  • @peeskillet how did you get your environment set up that quickly btw? It always takes me a while to create new projects. Did you modify an old one?
    – JSK NS
    Commented Dec 16, 2014 at 15:26
  • I am actually working on a project right now. I use Jetty Maven plugin for development. Every change is picked, so no reloading. I just created your class and registered it. It was automatically picked up :-) Commented Dec 16, 2014 at 15:27
  • Also sometimes I'll use a maven archetype. That will get you up and running right away also Commented Dec 16, 2014 at 15:30

1 Answer 1

1

You will need to add @FormParam to your method parameters if you want them to be injected as such

@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) {
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.