在我的 Controller 中,@GetMapping 的以下使用有效:
@GetMapping(value = "/new")
public String newEssay(){
return "articles/essay_new";
}
但它不是这样工作的:
@GetMapping(value = "/essays/{essayId: [0-9]+}")
//@RequestMapping(value = "/essays/{essayId:[0-9]+}", method = RequestMethod.GET)
public String getEssay(Model model,
@PathVariable("essayId") long essayId) throws NoFindException, ForBiddenException, ParseException {
JsEssay jsEssay = jsBiz.get(JsEssay.class, essayId);
model.addAttribute("jsEssay", jsEssay);
return "articles/essay";
}
我在 Spring 4.3.3 和 5.0.0.M5 上尝试过。
配置:
@Configuration
@ComponentScan( basePackages = {"me.freezehome.blog"},
excludeFilters = {
@ComponentScan.Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class)
}
)
public class RootConfig {
}
@Configuration
@EnableWebMvc
@Import({WebSecurityConfig.class})
public class WebConfig extends WebMvcConfigurerAdapter{
@Bean
public RequestMappingHandlerMapping requestMappingHandlerMapping(){
return new RequestMappingHandlerMapping();
}
@Bean
public RequestMappingHandlerAdapter requestMappingHandlerAdapter(){
return new RequestMappingHandlerAdapter();
}
}
谷歌结果:
- Add support for @GetMapping, @PostMapping etc. introduced in Spring 4.3 in ControllerLinkBuilder #471
- GetMapping and PostMapping annotations Ask
github来源:lbfreeze-blog-develop
最佳答案
请删除essayId:
后面的空格
此外,您不需要为 @GetMapping
编写 value =
。
关于java - 为什么 @Getmapping 在某些情况下不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42638707/