Question

I have HTML form data and being passed to RESTful POST method. I have noticed that if @FormParam is declared as

@POST
public void commitProjects(
@FormParam("projectId")
        int projectId)

and if projectId is null or empty then when POST method is executed from browser, I am getting 400 Bad Request error.

What could be reasons for this and what is the best approach to overcome such situation?

Edit 1

from browser

projectDesc=test&projectId=

and method is called from Jquery as

method: 'POST',
//dataType: "json",
contentType : "application/x-www-form-urlencoded",
url: '/rest/projects',

HTTP Header

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8;q=0.6
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:625
Content-Type:application/x-www-form-urlencoded
Cookie:JSESSIONID=0a0a0a0a231b787e42dd10d840d3b035d5998c574416
Host:xxxxxx:8987
Origin:http://xxxxx:8987
Referer:http://xxxxx:8987/project/test.jsp
User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36

Form Data

projectDesc=test
projectId=
Was it helpful?

Solution

null or empty cannot be parsed as a 32–bit signed integer, and an HTTP 400 (Client Error) response is returned.

if value does not exist, then set a default as follows

@POST
public void commitProjects( 
    @DefaultValue("0") @FormParam("projectId") int projectId
    )

More Details

OTHER TIPS

Use @DefaultValue:

@POST
public void commitProjects(@FormParam("projectId") @DefaultValue("0") int projectId)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top