This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Spring and the fly likes What are the differences in these 2 controllers? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "What are the differences in these 2 controllers?" Watch "What are the differences in these 2 controllers?" New topic

What are the differences in these 2 controllers?

Andrea Nobili

Joined: Nov 08, 2012
Posts: 22
Hello to all,

I am studing the Spring MVC showcase example downloaded from the STS dashboard.

I have a basic question for you:

In my home.jsp view I have two links, something like that:

So, in this example the first one is handled by a controller class named SimpleController and the second one is handled by a controller class named SimpleControllerRevisited

This is the code of SimpleController class:

As you can see the simple() method is annoted by @RequestMapping("/simple") annotation so it mean that this method handles request towards "/simple" folder. This method return a String to the caller and this string is encapsulated into the BODY section of the HTTP Response...ok it is very simple !!

Now this is the code of SimpleControllerRevisited

It is very similar to the previus SimpleController() method. It handles request towards "/simple/revisited" folder and return a string into the BODY of the HTTP Response

what is the difference between these two methods?

When I do:
method=RequestMethod.GET I'm specifying that the HTTP Request type is a GET...but is it not the default HTTP Request? Also in the other method is a GET although it is not explicitly specified? Or what?

What do exactly the: headers="Accept=text/plain" parameter of the @RequestMapping annotation?

thank you very much

Mark Spritzler

Joined: Feb 05, 2001
Posts: 17271

OK, the first one will also map tp POST, PUT and DELETE RequestMethodTypes. The second will only allow GET requests. For the second one the accept portion means the Request Header must have headers="Accept=text/plain" Otherwise that method will not be called regardless of URL or RequestMethod type.


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: What are the differences in these 2 controllers?
It's not a secret anymore!