Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC controller return HTML

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?

like image 499
user2219247 Avatar asked Jul 12 '13 09:07

user2219247


People also ask

Can I return html as response from rest controller?

Of course. This is not a hybris feature.

CAN REST API return html?

Rest api returns html instead of json if api route does not exists.

What does a controller return in Spring?

Introduction. In Spring Boot, the controller class is responsible for processing incoming REST API requests, preparing a model, and returning the view to be rendered as a response.


1 Answers

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

like image 130
Paulius Matulionis Avatar answered Sep 29 '22 06:09

Paulius Matulionis