Spring MVC controller return HTML

元气小坏坏 提交于 2019-12-03 16:11:00

问题


I am having an issue trying when trying to return HTML to my Spring MVC controller.

It looks like this:

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

My Spring config:

<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>

I am seeing firebug that response is coming like: {"String":"<div></div>"} how can I tell this method to send me plain HTML as the response?


回答1:


Change your Spring config like this: html=text/html and add produces = MediaType.TEXT_HTML_VALUE to your's @RequestMapping annotation.



来源:https://stackoverflow.com/questions/17611896/spring-mvc-controller-return-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!