415 error when sending json data back to java rest service using AJAX

I have an ajax request on my front end that sends a JSON doc to a rest service implementation. It is wired up through Spring and the @GET request in the REST service works well.

The issue is that whenever i try to use the @POST method it always throws a 415 error.

I have tried manipulating how i send it back and the type of variable that is accepted in the post method according to various guides online but I am not sure where to look next.

I have double checked that i do have data being sent to the @POST method but I feel like it is not parsing the JSON at all and throwing the error.

Unfortunately i dont see how to fix that if it is the issue.

Below i have dumbed everything down to a simple form that still produces the 415 error.

AJAX Request:

function post_request(json_data) {
    $j.ajax({
        url : '../api/createDisplayGroup/postHtmlVar/' + containerID + '/' + containerType,
        data: JSON.stringify(json_data),
        dataType: 'json',
        type : 'post'
    }).done(function(response) {
        run_update(response);
    }).error(function(jQXHR, textStatus, errorThrown) {
        alert('error getting request');
    });
};

Spring XML:

<jaxrs:server id="displayGroupService" address="/createDisplayGroup">  
      <jaxrs:serviceBeans>  
                <ref bean="peopleFilterRestService" />  
      </jaxrs:serviceBeans>  
      <jaxrs:providers>  
                <ref bean="jacksonJsonProvider"/>  
                <!-- disable XHR token check - allow external sources to call the service -->
                <!--ref bean="securityInterceptor" /-->  
      </jaxrs:providers>  
</jaxrs:server>

<bean id="peopleFilterRestService" class="mil.milsuite.community.rest.PeopleFilterRestService">
    <property name="peopleFilterService" ref="peopleFilterService"/> 
</bean>

<bean id="peopleFilterService" class="mil.milsuite.community.members.service.PeopleFilterServiceImpl">
    <property name="communityManager" ref="communityManager"/> 
    <property name="socialGroupManager" ref="socialGroupManagerImpl"/>
</bean>

REST Implementation (only @POST):

@POST
 @Path("/postHtmlVar/{containerId}/{contentType}")
 @Consumes(MediaType.APPLICATION_JSON)
 public List<TabDefinition> postHtml(@PathParam("containerId") String containerId, @PathParam("contentType") String contentType, List<TabDefinition> displayGroups) {
     Long contId = Long.parseLong(containerId);
     Long contType = Long.parseLong(contentType);


     //return convertToResponse(peopleFilterService.getDisplayGroups(contId, contType));*/

     return testDisplayGroup();
} 

The error you are receiving is as follows:

415 Unsupported Media Type

The request entity has a media type which the server or resource does not support. For example, the client uploads an image as image/svg+xml, but the server requires that images use a different format. (wikipedia)

The cause may be the $j.ajax call, try adding the contentType argument, eg:

...
$j.ajax({
    url : '../api/createDisplayGroup/postHtmlVar/' + containerID + '/' + containerType,
    data: JSON.stringify(json_data),
    dataType: 'json',
    type : 'post',
    contentType: 'application/json'
...

This will then match the value of the @Consumes annotation used by your postHtml method, note that:

The @Consumes annotation is used to specify which MIME media types of representations a resource can accept, or consume, from the client. (Oracle)

By default the ajax call's media type will be application/x-www-form-urlencoded , so your handler method will not be matched, resulting in the 415 error.

Aside: In the ajax call, contentType specifies the type of data to be sent to the server whereas dataType specifies the type of data that will be returned.

链接地址: http://www.djcxy.com/p/45584.html

上一篇: 在web.xml中定义的页面总是返回HTTP状态200

下一篇: 使用AJAX将json数据发送回java rest服务时发生错误415