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?

2

2 Answers 2

3

Just change your controller code as below

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

    }

See documentation of ResponseBody here

Sign up to request clarification or add additional context in comments.

Comments

1

Try:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.