有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring rest json输出

我创建了一个spring应用程序,当我打开以下链接时,它会成功运行:localhost:8080/test/greeting。服务器已经完全部署,但是当我打开该链接时,会下载一个名为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>

这是网络。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>

以下是问候控制器代码:

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));
    }
}

我已经搜索了一段时间,但没有找到解决方案


共 (2) 个答案

  1. # 1 楼答案

    设法弄明白它只是一个Internet Explorer,在其他浏览器中运行良好

  2. # 2 楼答案

    似乎没有为响应设置正确的内容类型。如果要将默认响应内容类型和转换器设置为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添加到类路径中,以处理实际的Java<=&燃气轮机;JSON转换如果这是默认情况下所需的,它可以帮助您避免使用样板代码,手动将每个请求映射定义方法的内容类型设置为JSON