弹簧MVC控制器返回HTML

2022-09-03 00:54:49

我在尝试将HTML返回到我的Spring MVC控制器时遇到问题。

它看起来像这样:

@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>";
}

我的弹簧配置:

<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的响应是这样的:我怎么能告诉这种方法给我发送纯HTML作为响应?{"String":"<div></div>"}


答案 1

像这样更改您的Spring配置:并添加到您的注释中。html=text/htmlproduces = MediaType.TEXT_HTML_VALUE@RequestMapping


答案 2