jquery - Posting with JavaScript to Restful Java Services does not work as JSON? -


Altough I'm sure this should definitely not be the first thing, I did not find the same topic with my special Are the issue I am trying to post data for my rental service in Java:

  @ POST @ path ("{projectId} / rename") @ Product (MediaType.APILICATION_JSON) public response Rename Project (@PathParam ("Project ID") Last Long Project ID, String Name) {Logger.getLogger (ProjectService.class.getName ()). Info ("Rename Project Service" + name); Return Request Handler Handlerennam Project (Project Ed, Name); }  

I am using this JS function to do this.

Rename the function name project (project id, name) {$ .post} (Change the name of the project / $ ("a [data-pk = '" + + "/ rest / projects /' + Project ID + '/ rename', {name: name}, function (data) {/ sidebar .key.id + "']") .text (name);},' json '); }

Now I wonder the output of log

  [INFO] April 28, 2014 6:56:05 pm de.uniluebeck.collaboratex.service .ProjectService renameProject [INFO] INFORMATION: Rename Project Service Name = New + Project  

How does the dimension have value not only but also its name as a concatenated string? I think I should not send things like JSON? The one who is pointing me in that direction is the fact that in Java, it was annotated with the service

  @Consumes (MediaType.APPLICATION_JSON)  
< P> As well as 415 unsupported media type errors are caused due to any input, it seems that one should be easy. :)

Your parameter name is not annotated as a @ * The ultimate , so it will get a full message body, which in your case describes as application / x-www-form-urlencoded :

Data options in either form key1 = value1 & amp; The key2 = value2 can be a query string or an object of {{1 key: 'value1', key2: 'value2'}. If the latter form is used, the data is converted to the query as jQuery.param () before it is sent. This processing process can be interrupted by setting data incorrectly. If you want to send an XML object to the server, processing may be undesirable; Thus, you have two options:

strong> Option 1: use @formparam as such:

 @ code @ @ POST @ path ("{projectId} / rename") @ product (MediaType APPLICATION_JSON) Rename Public Response Project (@PathParam ("projectId") Long Project ID, @ FarmParam ("name") string name) {Return Reponse.ok (). Build (); }  

Option 2: Send the proper Jason in this way:

  @ POST @ path ("{projectId} / rename "@Consume (MediaType.IPILICADGSAN) @ Productions (MediaType.APILICATION_JSON) Rename Project of Public Reaction (@Pathapram (" Project ID ") Long Project ID, Map & lt; String, String & gt; Paramap) {Return Reponse .ok () Build (); }  

and in your JS:

  $ Ajax ({url: '/ rest / projects' '+ Project ID' / rename ', data: JSON.stringify ({name: name}), process data: wrong, contentType:' application / json ', datatype:' Jason ', success: function (data) {console.log (data);}});  

You usually use Option 2 with complex datatypes, which you can serial with (D) Jackson (i.e., your client-side object is by some entity Which you can now name as a simple parameter, your Java method).


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -