Jomy George
Jomy George

Reputation: 323

Spring MVC- returning string value from controller

I am new to String, and now facing some staring issues with Spring MVC.

In my application I have view resolver which maps view names to respective JSP files.

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix"><value>/WEB-INF/pages/</value></property>
    <property name="suffix"><value>.jsp</value></property>
    <property name="order" value="1" />
</bean>

It is working as expected, but now need to call a method in controller and show returned string in view.

my URL of request looks like http://localhost:8015/demo/greet

And the method in my controller to server this request is

@RequestMapping("/greet")
    public String user(User user) {
        return "Hi User";

    }

When i call this url from browser, given method in browser get invoked, and when it returns a string, InternalResourceViewResolver try to find a page /WEB-INF/pages/greet.jsp, and as it doesn't exist, user gets 404 error. How can i send raw String from my controller method to browser?

Upvotes: 3

Views: 5369

Answers (2)

Neeraj Gupta
Neeraj Gupta

Reputation: 173

Just change your controller code as below

@RequestMapping("/greet")
    public @ResponseBody String user(User user) {
        return "Hi User";

    }

See documentation of ResponseBody here

Upvotes: 3

Amila
Amila

Reputation: 5213

Try:

@RequestMapping("/greet")
    public @ResponseBody String user(User user) {
        return "Hi User";
    }

Upvotes: 1

Related Questions