Your parameter name
isn't annotated as a @*Param
, so it'll get the full message body, which in your case is of type application/x-www-form-urlencoded
as jQuery.ajax() explains:
The data option can contain either a query string of the form key1=value1&key2=value2, or an object of the form {key1: 'value1', key2: 'value2'}. If the latter form is used, the data is converted into a query string using jQuery.param() before it is sent. This processing can be circumvented by setting processData to false. The processing might be undesirable if you wish to send an XML object to the server; in this case, change the contentType option from application/x-www-form-urlencoded to a more appropriate MIME type.
So you have two options:
Option 1: Use @FormParam
like so:
@POST
@Path("{projectId}/rename")
@Produces(MediaType.APPLICATION_JSON)
public Response renameProject(@PathParam("projectId") Long projectId,
@FormParam("name") String name) {
return Reponse.ok().build();
}
Option 2: Send proper json like so:
@POST
@Path("{projectId}/rename")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response renameProject(@PathParam("projectId") Long projectId,
Map<String, String> paramMap) {
return Reponse.ok().build();
}
And in your JS:
$.ajax({
url: '/rest/projects/' + projectId + '/rename',
data: JSON.stringify({name : name}),
processData: false,
contentType: 'application/json',
dataType: 'json',
success: function(data) {
console.log(data);
}
});
You typically only use option 2 with complex datatypes you (de-)serialize with jackson (ie, your client-side object is represented by some entity you can now name as a simple parameter to your Java method).