Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 268
Jomy George Avatar asked Jun 30 '26 02:06

Jomy George


1 Answers

Just change your controller code as below

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

    }

See documentation of ResponseBody here

like image 60
Neeraj Gupta Avatar answered Jul 02 '26 15:07

Neeraj Gupta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!