Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to POST a form using Jersey 2.0

I am trying to get some data from a Form with jersey and i though it would be an easy task to accomplish, however I am getting an error when I try to POST something.

Caused by: java.lang.IllegalStateException: The @FormParam is utilized when the content type of the request entity is not application/x-www-form-urlencoded
at org.glassfish.jersey.server.internal.inject.FormParamValueFactoryProvider$FormParamValueFactory.ensureValidRequest(FormParamValueFactoryProvider.java:126)
at org.glassfish.jersey.server.internal.inject.FormParamValueFactoryProvider$FormParamValueFactory.getForm(FormParamValueFactoryProvider.java:111)
at org.glassfish.jersey.server.internal.inject.FormParamValueFactoryProvider$FormParamValueFactory.get(FormParamValueFactoryProvider.java:94)
at org.glassfish.jersey.server.internal.inject.AbstractHttpContextValueFactory.provide(AbstractHttpContextValueFactory.java:65)
at org.glassfish.jersey.server.spi.internal.ParameterValueHelper.getParameterValues(ParameterValueHelper.java:80)
... 36 more

I think this is the relevant part of the stack trace. Now for the code I am using:

@POST
@Path("/delete")
@Produces("application/json")
public String delete(@FormParam("id")String id){

And I am trying to POST using a test html page like this:

<form action="<path to the server>/delete" method="post">
    primary_id: <input type="text" name="primary_id" /><br />
    <input type="submit" value="Submit" />
</form>

I've been trying to make it work but no chance. I have tried adding the @Consumes() annotation with multipart-form-data but can't really make it work. I hope someone can give me a hand.

like image 657
XFCC Avatar asked Sep 10 '12 17:09

XFCC


3 Answers

Your code is working fine except you need to change

<input type="text" name="id" />

Since you havent define @Consumes(), by default delete method consumes mediaType="application/x-www-form-urlencoded" .

I have tested your code and it is working perfectly fine here. My suggestion is check your jersey jar files Specially jsersey-server.jar file in your lib folder.

like image 25
Prabash B Avatar answered Nov 15 '22 15:11

Prabash B


Thank you all for the help. With some code reviewing i found the problem. Even though I don't think anyone else will make this particular mistake I'll post it anyway for future reference.

The problem was that I am not using a standard web server. I've implemented a netty server with Jersey and the problem was in that said implementation. The problem was that i wasn't passing the headers in the HTTP request to Jersey as I should. I was loosing the Content-Type in the middle of the operation, this means that Jersey couldn't identify the message type.

So, for future reference, for anyone having a similar problem while trying to implement a non-standard server using jersey: when you don't pass the media type correctly (there is a method called getMediaType() in the ContentRequest class that is used, among other things, to validate the Content-Type when @FormParam is used) you will get this type of Exception.

Again, thank you all for the help :)

like image 189
XFCC Avatar answered Nov 15 '22 15:11

XFCC


You are naming your input as "primary_id", but you are receiving "id" name in your @FormParm annotation. Change the id and name in your input tag to "id".

Also if you are consuming application/x-www-form-urlencoded, add this attribute to your form tag: enctype="application/x-www-form-urlencoded"

like image 1
john 4d5 Avatar answered Nov 15 '22 15:11

john 4d5