The problem is in REST with Jersey, while a JSON array is sent for POST using jQuery.

I have a Jersey REST implementation that provides an API from Tomcat (Server1)

@POST @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) public Users create(Users users) { return dao.create(users); } 

and the request is sent from the jQuery client from Apache (Server2)

 $.ajax({ type: 'POST', contentType: 'application/json', url: rootURL, dataType: "json", data: formToJSON(), success: function(data, textStatus, jqXHR){ alert('user created successfully'); }, error: function(jqXHR, textStatus, errorThrown){ alert('user error: ' + textStatus); } }); 

This jquery actually sends the request to the server, but the server / API was not able to recognize this request. All other queries like @ Formparam / @ Header etc. also work. with POST / GET.

If I use the same thing using the REST client, it worked, can someone help me sort this problem for POST with the request type Object.

+4
source share
1 answer

The best way to debug communication problems is to check the headers sent to the server. To complete this task, you can view the request in a browser using the Chrome developer tools. Open Chrome, press F12, go to the "Network" tab, filter out XHR and force this query. Copy all the headers and place them here.

Then go to your test with the Jersey client API. Enable logging with adding a filter to the client:

 client.addFilter(new LoggingFilter()); 

Then view the console and compare the output with the previous one from Chrome Dev tools.

0
source

Source: https://habr.com/ru/post/1444612/


All Articles