我在尝试将 HTML 返回到我的 Spring MVC Controller 时遇到问题。
看起来像这样:
@RequestMapping(value = QUESTION_GROUP_CREATE_URL, method = RequestMethod.POST)
public
@ResponseBody
String createQuestionGroup(@RequestBody JsonQuestionGroup questionGroup, HttpServletResponse response) {
// questionGroup - this comes OK.
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
return "<div></div>";
}
我的 Spring 配置:
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="false"/>
<property name="favorParameter" value="true"/>
<property name="mediaTypes">
<value>
json=application/json
xml=application/xml
html=application/html
</value>
</property>
</bean>
我看到 Firebug 的 react 是这样的:{"String":"<div></div>"}
如何告诉此方法向我发送纯 HTML 作为响应?
最佳答案
像这样更改您的 Spring 配置:html=text/html
并将 produces = MediaType.TEXT_HTML_VALUE
添加到您的 @RequestMapping
注释中。
关于java - Spring MVC Controller 返回 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17611896/