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
303 views
in Technique[技术] by (71.8m points)

java - Exact RequestMapping with Spring

I have the following in Spring

@RequestMapping("/hello")

However Spring automatically adds mappings for /hello/ as well as /hello.*. How do I do an exact URL match?

Only /hello should work, anything else should 404

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

Turning off suffix matching (useSuffixPatternMatch) on RequestMappingHandlerMapping will solve your problem, but doing so is actually not so easy, if you use <mvc:annotation-driven/> in your configuration (instead of manually wiring all the necessary infrastructure beans). In this case defining an additional bean of type RequestMappingHandlerMapping won't have any effect.

You have two options:

  1. Remove <mvc:annotation-driven/> expanding it to an equivalent set of bean definitions where you can apply the useSuffixPatternMatch setting.

  2. Keep <mvc:annotation-driven/> as it is, and use a much easier workaround described here: https://jira.springsource.org/browse/SPR-9371. This basically adds a BeanPostProcessor which retrieves the RequestMappingHandlerMapping bean created by the mvc namespace, and sets the above mentioned flag.

There is also another ticket requesting that it should be much easier to customize the RequestMappingHandlerMapping created by the mvc namespace without applying hacks like above. You can consider voting on this ticket.


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

2.1m questions

2.1m answers

60 comments

57.0k users

...