1

Spring 4 MVC based project with xml based configuration, want to use Jackson binding to return Json for some of the controller mappings (and it's going to Google App Engine). Did a lot of investigating, many different solution but they doing, each one, a bit different approach then the one we have. No yet working solution.

web.xml file:

<?xml version="1.0" encoding="utf-8"?>
    <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring-config/application-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

Spring application-context.xml:

 <?xml version="1.0" encoding="UTF-8"?>
        <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd ">

        <context:component-scan base-package="com.wixpress.automation"/>

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

My example controller is pretty slim:

@Controller 
@RequestMapping("/api/")
public class Controller {

   @RequestMapping(value = "/name", method = RequestMethod.GET)
    @ResponseBody
    public Name nameexample(WebRequest request, Model model) {
        final Name name = new Name("name", "class", "package");
        return name;
    }
}

The class Name is a simple POJO with getters/setters, implements Serializable.

pom.xml got all the Jackson dependencies: jackson-databind, jackson-core, jackson-annotations, version 2.6.0. Spring used: 4.2.0.RELEASE

The error I receive when trying to GET the url: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

4
  • So, what do you do, what do you expect to happen, and what happens instead? Commented Aug 27, 2015 at 16:49
  • @JBNizet expect to receive the JSON representation of the object (got other url mapping that returns VIEWs, they work). Adding all the other relevant info to the q. Please tell me if something missing. Commented Aug 27, 2015 at 16:50
  • What is the value of your Accept request header? Commented Aug 27, 2015 at 16:52
  • @JBNizet Accept: application/json Commented Aug 27, 2015 at 19:14

1 Answer 1

0

You could try using @RestController instead of @Controller.

Sign up to request clarification or add additional context in comments.

1 Comment

If I understand correctly, it's the same as using ResponseBody + Controller together, the same as I have.

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.