3

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?

Subodh Joshi
  • 12,717
  • 29
  • 108
  • 202
Jomy George
  • 313
  • 3
  • 13

2 Answers2

3

Just change your controller code as below

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

    }

See documentation of ResponseBody here

Neeraj Gupta
  • 173
  • 2
  • 14
1

Try:

@RequestMapping("/greet")
    public @ResponseBody String user(User user) {
        return "Hi User";
    }
Amila
  • 5,195
  • 1
  • 27
  • 46