AkshaiShah
AkshaiShah

Reputation: 5939

Spring rest json output

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.

Upvotes: 0

Views: 1148

Answers (2)

AkshaiShah
AkshaiShah

Reputation: 5939

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

Upvotes: 0

drembert
drembert

Reputation: 1246

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.

Upvotes: 1

Related Questions