AndreaNobili
AndreaNobili

Reputation: 42967

Can I specify that a controller level request mapping is not valid for a specific method into a Spring MVC controller class?

I am working on a Spring MVC application and I have the following problem into a controller class that is annotated with @RequestMapping("/profilo/") at class level.

So I have something like this:

@Controller
@RequestMapping("/profilo/")
public class ProfiloController extends BaseController {

    ..................................................................
    ..................................................................
    SOME CONTROLLER METHOD THAT WORK CORRECTLY
    ..................................................................
    ..................................................................

    @RequestMapping(value = "utenze/{username}/confermaEmail/{email:.+}", method = RequestMethod.GET)
    public String confermaModificaEmail(@PathVariable String username, @PathVariable String email, Model model) {

        logger.debug("INTO confermaModificaEmail(), indirizzo e-mail: " + email);
        .......................................................................
        .......................................................................
        .......................................................................
    }
}

So, as you can see in the previous code snippet, I have this ProfiloController class that is annotated with @RequestMapping("/profilo/") so it means that all the HTTP Request handled by the controller method of this class have to start with the /profilo/ in the URL.

It is true for all the method of this class except for the confermaModificaEmail() method that have to handle URL as:

http://localhost:8080/my-project/utenze/mario.rossi/confermaEmail/[email protected]

that don't tart with /profilo/.

So can I specify that for this specific method of the controller the @RequestMapping("/profilo/") controller level mapping is not valid and have to be not considered?

Upvotes: 1

Views: 602

Answers (1)

Arijeet Saha
Arijeet Saha

Reputation: 1168

This is not possible.

Spring has maintained a proper @Controller structure which says for all endpoints related to ControllerName (i.e .Portfolio in your case) should be kept in this class.

Ideally, any other url not related to the functionality should be kept as part of a different Controller class.

If still you want to keep in same controller, try calling the endPoint url confermaModificaEmail() by redirecting the http call from "/portfolio/<"sample"> to the required url. But this is not recommended.

Upvotes: 3

Related Questions