beta.blog

hybris: Create a configurable @RequestMapping

by on Nov.07, 2016, under Programming

If you ever wondered how to make a request mapping path configurable in hybris (well, basically it’s possible for any web application that uses the Spring framework) there is an easy solution for that. As stated in the Spring Framework documentation:

Patterns in @RequestMapping annotations support ${…} placeholders against local properties and/or system properties and environment variables. This may be useful in cases where the path a controller is mapped to may need to be customized through configuration. For more information on placeholders, see the javadocs of the PropertyPlaceholderConfigurer class.

Well said, but what does it mean? Simply add a new property to e.g. your local.properties file which is located under hybris/config/ by default.

moderator.cp.path=modcp

You may now add a request mapping to your controller as shown below. Pay close attention to the line in which we used our previously created property as well as a fallback path ( /fallbackPath ) just in case the property is not available:

@RequestMapping("/${moderator.cp.path:fallbackPath}")

A complete controller could look like this:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
class ModeratorController {
  @RequestMapping("/${moderator.cp.path:fallbackPath}")
  public ModelAndView get() {
    String message = "Hello flexible world!";
    return new ModelAndView("Hello world", "message", message);
  }
}

Your controller will now be available under /modcp or /fallbackPath, depending on if your configured property was found or not.

:, ,

Leave a Reply

*

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!