1

in my exercise i have to develop a spring application which should be accessible through a WebGUI AND a REST service. Now i browed through the examples of Spring MVC, there is this hello world tutorial on Spring MVC.

The controller looks like as follows:

 @Controller
 @RequestMapping("/welcome")
 public class HelloController {

    @RequestMapping(method = RequestMethod.GET)
    public String printWelcome(ModelMap model) {
        model.addAttribute("message", "Spring 3 MVC Hello World");
    return "hello";
    }
}

Then i looked through the Spring REST example which looks like this:

@Controller
@RequestMapping("/movie")
public class MovieController {

@RequestMapping(value = "/{name}", method = RequestMethod.GET)
public String getMovie(@PathVariable String name, ModelMap model) {

    model.addAttribute("movie", name);
    return "list";

}

@RequestMapping(value = "/", method = RequestMethod.GET)
public String getDefaultMovie(ModelMap model) {

    model.addAttribute("movie", "this is default movie");
    return "list";

}

}

Now I am wondering, how do these two examples (Spring-mvc and Spring-rest) differ? They both use the same annotations and work similar. Aren't that both just REST examples?

How can I provide a Rest-Interface to a Spring-MVC application?

regards

1
  • A service being RESTful has to do with how resources are accessed. See: en.wikipedia.org/wiki/… Commented Apr 17, 2013 at 19:23

3 Answers 3

2

In order to provide rest interface to Spring MVC application, you can apply @RequestMapping annotation with a path name to each of the methods in controller, this creates a unique URL path for each of the rest services you would like to provide.

Meaning, the rest services are nothing but the methods in Spring MVC controller with @RequestMapping annotation.

If you would like to learn how Spring MVC supports Rest Based services, the below link might help:

http://blog.springsource.org/2009/03/08/rest-in-spring-3-mvc/#features

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

Comments

1

Both samples are about Spring Web MVC.

You should pay more attention to definitions, like what is REST

https://en.wikipedia.org/wiki/Representational_state_transfer

Representational State Transfer is intended to evoke an image of how a well-designed Web application behaves: presented with a network of Web pages (a virtual state-machine), the user progresses through an application by selecting links (state transitions), resulting in the next page (representing the next state of the application) being transferred to the user and rendered for their use.

Spring Web MVC greatly facilitates developing REST web APIs and that's it.

Comments

-1

Rememeber @ResponseBody as return type on method is going to be REST. ofcourse returned object can be negotiated with either JSON or XML.

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.