Java SpringRest json输出
我已经创建了一个spring应用程序,当我打开以下链接时,它会成功运行:localhost:8080/test/greeting。服务器已经完全部署,但当我打开该链接时,会下载一个名为Java SpringRest json输出,java,json,spring,rest,jackson,Java,Json,Spring,Rest,Jackson,我已经创建了一个spring应用程序,当我打开以下链接时,它会成功运行:localhost:8080/test/greeting。服务器已经完全部署,但当我打开该链接时,会下载一个名为greeting的文件,其中集成了json。我试图在InternetExplorer上打开链接,我想json会直接出现在屏幕上。不是这样吗 以下是dispatcher servlet: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=
greeting
的文件,其中集成了json。我试图在InternetExplorer上打开链接,我想json会直接出现在屏幕上。不是这样吗
以下是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>
我已经搜索了一段时间,但没有找到解决方案。似乎没有为响应设置正确的内容类型。如果要将默认响应内容类型和转换器设置为JSON,除了返回定义上的@ResponseBody注释外,还可以将以下bean定义添加到spring上下文文件中
<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>
您还需要将Jackson添加到类路径中,以处理实际的javajson转换如果这是默认情况下您想要的,它可以帮助您避免使用样板代码,手动将每个请求映射定义方法的内容类型设置为JSON所需的样板代码。设法确定它只是Internet Explorer,在其他浏览器中工作正常 您能显示控制器代码吗?我认为Spring(mvc)返回的响应内容类型不正确。它应该是“application/json”。搜索“SpringMVC内容类型”。我可以帮你。建议使用
@ResponseBody
注释servlet,并使用response.setContentType
。发布处理该问题的控制器request@SotiriosDelimanolis我现在已经添加了它
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));
}
}
<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>