Caio Cunha
Caio Cunha

Reputation: 23394

Forward request to a non @RequestMapping

Is it possible to use forward:/ syntax to redirect a request to a non @RequestMapping annotated method.

What I'm trying to achieve is a way to internally redirect a request to a path that can not be accessed directly from the browser.

For example, if the user requests /image.jpg, I'd like to redirect it to a controller that would answer for images, mapped to /img/image.jpg. The point is that /img/image.jpg cannot be accessed directly through the browser.

Is it possible? How could I achieve this necessity?

Upvotes: 4

Views: 967

Answers (1)

Satish Pandey
Satish Pandey

Reputation: 1184

I think You need to create request mapping path if you want to use forward:/ to call the resource. However you can achieve this by:

Create a request mapping path that is not accessible by end-user direct browser calls. Before forwarding the request for path put some extra request parameters those are check by request mapper then.

Example :

@RequestMapping(value = "/direct/browser/call")
public String connections(WebRequest request) throws SQLException {
    //validate request
    request.setAttribute("formButton", isPost, WebRequest.SCOPE_REQUEST);
    return "redirect:/some/value";
}

@RequestMapping(value = "/some/value")
public String connections(WebRequest request) throws SQLException {
    //validate request
    if(request.getParameter("formButton")!=null) { return "/homepage"; }
    // else : do processing
}

You can also restrict browser HTTP GET calls in your request mappings. Example:

@RequestMapping(value = "/some/value", method = RequestMethod.POST)

HTTP Methods : HEAD, PUT, DELETE, TRACE and OPTIONS are also useful in your case.

Upvotes: 1

Related Questions