Java 在Spring4中,控制器生成纯文本而不是html

Java 在Spring4中,控制器生成纯文本而不是html,java,spring,spring-mvc,servlets,controller,Java,Spring,Spring Mvc,Servlets,Controller,我正在从 弹簧3.2.5至4.2.0 spring security 3.1.4至4.0.2。 我的许多用于显示HTML的控制器现在显示纯文本。 我创建了一些测试方法,在版本3中显示以下HTML,在版本4中显示纯文本: @RequestMapping(value = "/htmltest", method = RequestMethod.GET) public void test(Writer writer, HttpServletRequest request, HttpServletResp

我正在从
弹簧3.2.5至4.2.0
spring security 3.1.4至4.0.2。

我的许多用于显示HTML的控制器现在显示纯文本。 我创建了一些测试方法,在版本3中显示以下HTML,在版本4中显示纯文本:

@RequestMapping(value = "/htmltest", method = RequestMethod.GET)
public void test(Writer writer, HttpServletRequest request, HttpServletResponse response) throws IOException {
    writer.append("<html><head></head><body>"); //$NON-NLS-1$
    writer.append("this is a HTML test");
    writer.append("</body></html>"); //$NON-NLS-1$
    writer.flush();
}
这将显示HTML,如果切换到
products=“text/plain”
将返回文本

因此,问题是如何让控制器在使用编写器的地方生成HTML?
我发现我可以在response
response.setContentType(“text/html”)
上设置内容类型,但我不希望对所有控制器都这样做。
特别是因为它在以前的spring版本中工作,这可能是一个配置问题吗

@RequestMapping(value = "/htmltest3", method = RequestMethod.GET)
public void test3(Writer writer, HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setContentType("text/html");

    writer.append("<html><head></head><body>"); //$NON-NLS-1$
    writer.append("this is a HTML test");
    writer.append("</body></html>"); //$NON-NLS-1$
    writer.flush();
}
@RequestMapping(value=“/htmltest3”,method=RequestMethod.GET)
public void test3(Writer-Writer、HttpServletRequest请求、HttpServletResponse响应)引发IOException{
response.setContentType(“text/html”);
writer.append(“”;//$NON-NLS-1$
append(“这是一个HTML测试”);
writer.append(“”;//$NON-NLS-1$
writer.flush();
}
编辑:我刚刚发现只有Chrome(44.0.2403.155 m)不能显示HTML、Firefox(40.0.2)和InternetExplorer(11.0.9600.17959)正常工作。
尽管如此,它在较旧的浏览器版本中也可能不起作用,但这可能给出了一个提示?

试试:

@ResponseBody
@RequestMapping(value = "/htmltest3", method = RequestMethod.GET)
public void test3(Writer writer, HttpServletRequest request, HttpServletResponse response) throws IOException {
    String html = "<html><head></head><body>this is a HTML test</body></html>";

    return html;
}
@ResponseBody
@RequestMapping(value=“/htmltest3”,method=RequestMethod.GET)
public void test3(Writer-Writer、HttpServletRequest请求、HttpServletResponse响应)引发IOException{
String html=“这是一个html测试”;
返回html;
}

所以问题在于Spring Security 3.2.0中引入的头文件:

如果将以下行添加到应用程序上下文中,控制器将再次显示HTML

<http>
    ...
    <headers disabled="true"/>
</http>

...
但这是个坏主意,因为它会禁用所有附加的seurity功能(X-XSS-Protection、X-Frame-Options、hst)

因此,一个选项可以是禁用默认值并配置自己的标头:

<http>
    ...
    <headers defaults-disabled="true">
        <frame-options policy="SAMEORIGIN" />
    </headers>
</http>

...
缩小范围后,这解决了我的问题:

<http>
    ...
    <headers>
        <content-type-options disabled="true" />
    </headers>
</http>

...
有一些安全方面需要考虑,但对我来说,这是暂时的。

您可以编写一个过滤器,为您使用writer explicility的URL设置内容类型explicility这很可能会起作用,请参见/htmltest2,但我想使用writer。因为它在我们的系统中被大量使用。
<http>
    ...
    <headers>
        <content-type-options disabled="true" />
    </headers>
</http>