ucas
ucas

Reputation: 487

Java RESTful service errs on @FormParam annotation

I'm writing a RESTful service app in Java EE 6. I've encountered a difficulty along defining HTTP GET service method which uses the @FormParam annotation.

Technologies that I use:

Java:

@GET
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_HTML)
@Path("/getFromForm")
public String getFromForm(@FormParam("input") String input){
    log.log(Level.INFO, "Invoked getFromForm");
    String html = "<html> <body>"
            +"<center><h1> "+input+"</h1></center>"
            +"</body></html>";
    return html;
}

JSP:

 <form action="/RESTfulServiceDrill/rest/v6/html/getFromForm" method="get" 

enctype="application/x-www-form-urlencoded">

<label> Add message: </label>
<input type="text" name="input"/>

<input type="submit" value="OK"/>
<input  type="reset" value="clear"/>

</form>

Exception:

java.lang.IllegalStateException: The @FormParam is utilized when the content type of the request entity is not application/x-www-form-urlencoded

Any ideas what the culprit is?

Upvotes: 1

Views: 2777

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 208944

You should be using POST, not GET. With GET, the browser will not set the Content-Type header as the params are sent in the URL, not the body. If you want to keep it with GET, use @QueryParam, which captures the param from the query string in the URL

Upvotes: 1

Related Questions