0

I have the following Spring Controller

@Controller
@RequestMapping("/accreq")

with the following mapping and file similar to org.springframework.samples.mvc.ajax.account.AvailabilityStatus with an extra boolean field someBooleanValue

@RequestMapping(value = "/defRoles", method=RequestMethod.GET)
public @ResponseBody AvailabilityStatus loadDefaultRoles(
    @RequestParam(value="idGroup", required=false) String groupID {

I'm trying to call this method with the following jquery ajax

$.getJSON("${pageContext. request. contextPath}/accreq/defRoles.htm", { idGroup: $('#infoGroup').val() }, function(availability) {
        if (availability.someBooleanValue) {
            //Do this
        } else {
            //Do else
        }
    });

Spring method is being executed but I get a 406 response back. What dataType I need to set to get a successful response? This use to work under Spring 3.1.4 and now it does not work on higher version of Spring like 3.2.4 or 4.0.0. In short, how to handle object return in Ajax response?

Response header - 406 Error

Request Headersview source
Accept  application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Connection  keep-alive

Config

<context:component-scan base-package="com.X" />
<mvc:annotation-driven />
<cache:annotation-driven />
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="messages" />
</bean>
...mapping for controller.....database etc

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
    <property name="cache"  value="true" />
    <property name="order"  value="1" />
</bean>
9
  • If I recall correctly the jQuery getJSON method is sending the data as JSON to the server and not as a request parameter. So basically your content send to the server is JSON whereas spring expects it to be a request parameter. Commented Sep 2, 2013 at 10:02
  • I feel the same but the method is getting invoked. The problem is more about the response. How should the response be formulated to escape 406 error? Commented Sep 2, 2013 at 10:12
  • How are you testing that the method is getting called? The response should be returning HTML as that is what you are requesting (you are calling a .htm URL which expects the content to be HTML). Commented Sep 2, 2013 at 10:19
  • Using breakpoint. After the ajax call I see the method stops at the debug point. So you are suggesting the returned String should be HTML type. But can I not specify the return type in Ajax call like dataType: "text", or some other type for the object (if there exists any)? Wondering how this worked for 3.1.4? Commented Sep 2, 2013 at 10:27
  • Internally there has been quite an overhaul. I'm saying that the response request for json (probably) but the extension (.htm) leads spring to believe that it must return HTML. They don't match so it doesn't know what to do, can you post your configuration. You might want to read this section of the Spring Reference Guide Commented Sep 2, 2013 at 11:04

1 Answer 1

2

In Spring 3.2 there was an overhaul of the content-negotiation implementation. With it there was a ContentNegotiationManager introduced and with it also the fact that the path takes precedence over the Accept-Header. (Using the Accept-Header in a crossbrowser compatible way is quite a pain as different browsers send different headers :s).

As I mentioned in one of the comments the reference guide clearly documents how one can configure the ContentNegotiationManager.

Something like the following should fix things

<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <property name="favorPathExtension" value="false" />
</bean>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.