0

I have created a spring application, which runs successfully when I open the following link: localhost:8080/test/greeting. The server has been fully deployed, but when I open that link a file is called greeting is downloaded, with the json integrated inside. I tried to open the link on Internet Explorer, and I thought the json would directly appear on the screen. Is this not the case?

Here is the dispatcher servlet:

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

    <context:component-scan base-package="test" />
    <mvc:annotation-driven />
</beans>

And here is the web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">

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

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

</web-app>

Here is the GreetingController code:

package test;

import java.util.concurrent.atomic.AtomicLong;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @RequestMapping("/greeting")
    public @ResponseBody Greeting greeting(
            @RequestParam(value="name", required=false, defaultValue="World") String name) {
        return new Greeting(counter.incrementAndGet(),
                            String.format(template, name));
    }
}

I've been searching for a while, and have not been able to find a solution to this.

4
  • can you show the controller code ? Commented Nov 27, 2013 at 18:59
  • 1
    I think the response Content-Type which Spring (mvc) returns is not correct. It should be "application/json". Search for "Spring mvc content-type". This overflow Q/A may help you. It suggest annotate your servlet with @ResponseBody and use response.setContentType. Commented Nov 27, 2013 at 19:13
  • Post the controller that handles that request Commented Nov 27, 2013 at 19:21
  • @SotiriosDelimanolis I have added it now Commented Nov 27, 2013 at 20:37

2 Answers 2

1

It appears that the correct content type is not being set for the response. If you want to set your default response content type and converter to JSON, you could add the following bean definitions to your spring context file, in addition to the @ResponseBody annotation on the return definition.

<bean id="jacksonMessageConverter"class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
      <property name="messageConverters">
           <list>
              <ref bean="jacksonMessageConverter" />
           </list>
       </property>
</bean>

You will also need to add Jackson to your classpath to handle the actual Java <=> JSON conversion. If this is what you want by default, it could help prevent you from the boilerplate code that would be needed to manually set the content type to JSON for every request mapping definition method.

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

Comments

0

Managed to work out that it's just Internet Explorer, works fine in other browsers!

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.