1

my web.xml:

<servlet>
    <servlet-name>rest-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>rest-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

pom.xml:

...
<jackson.mapper.version>1.9.13</jackson.mapper.version>
...
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-lgpl</artifactId>
        <version>${jackson.mapper.version}</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-core-asl</artifactId>
        <version>${jackson.mapper.version}</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-lgpl</artifactId>
        <version>${jackson.mapper.version}</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>${jackson.mapper.version}</version>
    </dependency>

rest-services-config.xml:

<mvc:annotation-driven />
<context:component-scan base-package="com.example.web.rest" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

<bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
    <property name="contentType" value="text/plain"/>
</bean>

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <util:list id="beanList">
            <ref bean="jsonMessageConverter"/>
        </util:list>
    </property>
</bean>

<bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>

rest-dispatcher-servlet.xml:

<context:annotation-config />
<context:component-scan base-package="com.example.web.rest" />



<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>WEB-INF/pages/</value>
    </property>
    <property name="suffix">
        <value>.html</value>
    </property>
</bean>

<mvc:resources mapping="/WEB-INF/pages/**" location="/WEB-INF/pages/" />
<mvc:resources mapping="/vendors/**" location="/resources/vendors/" />
<mvc:resources mapping="/controls/**" location="/resources/controls/" />
<mvc:resources mapping="/css/**" location="/resources/css" />
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />

Finally, here is my controller:

@Controller
@RequestMapping("/user")
public class UserService {

@Autowired(required=true)
private UserDaoImpl dao;

@RequestMapping(value="/getCurrentUserCredentials/", method = RequestMethod.GET)
public @ResponseBody UserCredentials getCurrentUserCredentials() {
    HttpSession session = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest().getSession();
    try {
        return dao.getCurrentUserCredentials((Integer)session.getAttribute("userid"));
    } catch (Exception e) {
        return null;
    }
}

}

...and request which gives to me a 406error:

    $.ajax({
            type: 'GET',
            url: 'user/getCurrentUserCredentials/',
            dataType: 'json',
            success: function(data) {console.log(data); self.currentUserCredentials(data);},
            error: function() {console.log('error');}
        });

Any suggestions about solving this? Help please.

Controller's method returns correct data (checked in debug) while this data can't be handled properly.

Arti88
  • 37
  • 5
  • Try changing the MappingJacksonView in rest-services-config.xml to ` ` – geoand Apr 30 '14 at 10:20
  • unfortunately, "406" still – Arti88 Apr 30 '14 at 10:45
  • Is `getCurrentUserCredentials` throwing an exception or not? – geoand Apr 30 '14 at 10:46
  • Nono as I said - this method works perfectly;Obv.I have mistake somewhere in settings – Arti88 Apr 30 '14 at 10:59
  • have you tried using another rest client instead of jquery? Reason is that there is a similar question that points to the jquery being incorrect, http://stackoverflow.com/questions/4069903/spring-mvc-not-returning-json-content-error-406 – Shiraaz.M Apr 30 '14 at 12:51
  • in the browser open the network inspector, and check the request header `Accept` comparing it against the response `Content-Type`. – guido Apr 30 '14 at 13:16
  • 406 also occurs when you don't have getters on the object you want to JSONify. I just ran into this issue and I thought I'd mention it :) – Julius Oct 14 '14 at 19:58

1 Answers1

0

Are you not meant to add a @Produces annotation here?

@RequestMapping(value="/getCurrentUserCredentials/", method = RequestMethod.GET, produces="application/json")
public @ResponseBody UserCredentials getCurrentUserCredentials() {
    HttpSession session = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest().getSession();
    try {
        return dao.getCurrentUserCredentials((Integer)session.getAttribute("userid"));
    } catch (Exception e) {
        return null;
    }
}

Not sure if this matters, but the @ResponseBody annotation is also before the public keyword. My example is based on this Spring reference code:

@Controller
public class ErrorController {

    @RequestMapping(value="/error", produces="application/json")
    @ResponseBody
    public Map<String, Object> handle(HttpServletRequest request) {

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status", request.getAttribute("javax.servlet.error.status_code"));
        map.put("reason", request.getAttribute("javax.servlet.error.message"));

        return map;
    }

}

There's also the option to have @RestController instead of @Controller for newer versions of Spring.

Shiraaz.M
  • 3,073
  • 2
  • 24
  • 40
  • Hi, tried to prepend "@ResponseBody" before "public ..." but seems that it doesn't matter. Added "produce" parameter but still 406. – Arti88 Apr 30 '14 at 11:50
  • @Arti88, did you also try the produces part of the request mapping (i.e. `produces="application/json"`)? – Shiraaz.M Apr 30 '14 at 12:35
  • Yea... still the same: Failed to load resource: the server responded with a status of 406 (Not Acceptable) http://localhost:8080/PR2/user/getCurrentUserCredentials/ – Arti88 Apr 30 '14 at 12:44