Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

spring mvc - @RequestMapping with placeholder not working

So i have spend hours to try to get the anwser of this post working:
Overriding RequestMapping on SpringMVC controller

But it really is not working. What I have so far:

springmvc-servlet.xml

<context:property-placeholder location="classpath:numbernick.properties"/>
<context:component-scan base-package="com.numbernick" />
<context:annotation-config />

And I've got a Controller:

@Value("${requestmapping.test}")
private String test;

@RequestMapping("${requestmapping.test}.html")
public ModelAndView test() {
    ModelAndView mav = new ModelAndView();
    mav.setViewName(test.html);

    log.debug("Test: "+test);
    return mav;
}

numbernick.properties:

requestmapping.test=myUrl

This should work fine. When I call the page, I get a logmessage saying "Test: myUrl" . BUT! this comes when I call "/${requestmapping.test},html". And it should work with calling "/myUrl.html". I have absolutely no Idea why it is this way. Obviously the PropertyPlaceholder works and doesn't work at the same time. (BTW: It is a nested RequestMapping. But it also doesn't work at topLvl-RequestMapping as well)

How can this be and what can I do to fix this? I'm currently working with spring verion 3.2.8

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I had this problem also and solved it once I realized that a PropertyPlaceholderConfigurer bean wasn't loaded into the context of the module where many of the placeholders existed.

Simple solution was to refactor our externalized configuration. In the end, I moved the @PropertySources definition and PropertyPlaceholderConfigurer bean to a common module and all is well:

@Configuration
@PropertySources(value = {@PropertySource("classpath:app-config.properties")})
public class ExternalizedConfig {

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}

The request mappings like this work as expected now:

@RequestMapping(value="/${foo.bar.rest_proxy_uri}/**", method = RequestMethod.GET)

In fact, on server startup, you will see the placeholders have been resolved:

2015-05-06 16:21:52 INFO  RequestMappingHandlerMapping:220 - Mapped "{[/restProxy/**],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public org.springframework.http.ResponseEntity<java.lang.String> foo.bar.web.controllers.RestfulFooBarProxyController.proxyGet(javax.servlet.http.HttpServletRequest)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...