Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How SpringBoot automatically configures Url prefixes according to directory structure

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you the "SpringBoot how to automatically configure Url prefix according to the directory structure", the content is easy to understand, well-organized, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "SpringBoot how to automatically configure Url prefix according to directory structure" this article.

In many other frameworks, such as Python's Flask and node.js 's KOA,Controller, we need to actively register with the application in order to respond to the front-end request. On the other hand, Spring does not need to be registered by ourselves, and Spring actively discovers it by scanning annotations.

Custom RequestMappingInfo

The RequestMappingHandlerMapping in Spring is specifically responsible for handling controllers labeled @ RequestMapping. Create a class that inherits and overrides the methods in it to modify the default mechanism.

Override the getMappingForMethod method, and the return value RequestMappingInfo of this method contains the requested Url. Modify the Url in the RequestMappingInfo to modify the Url in the route.

Package com.lin.missyou.hack;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.servlet.mvc.method.RequestMappingInfo;import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;import java.lang.reflect.Method;public class AutoPrefixUrlMapping extends RequestMappingHandlerMapping {@ Value ("${missyou.api-package}") private String apiPackagePath / / get the path of the root package from the configuration file @ Override protected RequestMappingInfo getMappingForMethod (Method method, Class handlerType) {RequestMappingInfo requestMappingInfo = super.getMappingForMethod (method, handlerType); if (null! = requestMappingInfo) {/ / get the url prefix String prefix = getPrefix (handlerType) / / generate RequestMappingInfo according to the url prefix and merge with the original RequestMappingInfo RequestMappingInfo mappingInfo = RequestMappingInfo.paths (prefix). Build (). Combine (requestMappingInfo); return mappingInfo;} return requestMappingInfo;} private String getPrefix (Class handlerType) {String packageName = handlerType.getPackage (). GetName () / / get the packet path String dotPath = packageName.replaceAll (this.apiPackagePath, ") of the controller; / / truncate the return dotPath.replace (". "," / ") from the more than part of the packet path; / / truncate the. Replace with /}} discover classes in the form of interfaces

Create a configuration class AutoPrefixConfiguration to add AutoPrefixUrlMapping to the container. Configure the class AutoPrefixConfiguration to implement interface WebMvcRegistrations and override the getRequestMappingHandlerMapping method in it

Package com.lin.missyou.core.config;import com.lin.missyou.hack.AutoPrefixUrlMapping;import org.springframework.boot.autoconfigure.web.servlet.WebMvcRegistrations;import org.springframework.stereotype.Component;import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;@Componentpublic class AutoPrefixConfiguration implements WebMvcRegistrations {@ Override public RequestMappingHandlerMapping getRequestMappingHandlerMapping () {return new AutoPrefixUrlMapping ();}}

Specify the root package in the configuration file

Missyou.api-package = com.lin.missyou.api

There are two mechanisms for SprinBoot discovery. One is to mark specific comments on the controller, such as @ ControllerAdvice on GlobalExceptionAdvice in the previous blog post SpringBoot global exception handling. The other is to implement a specific interface and override specific methods in it, such as AutoPrefixConfiguration above.

Test it

Access result, access path / v1/banner/test can access the controller

Change the access path to / banner/test and you will not be able to access it.

Move BannerController to the sample folder access path / v1/sample/banner/test can access the controller

There is some controversy about this method. On the one hand, it is considered that although it is relatively simple to generate url automatically according to the directory structure, less code is written, but the url can not be directly seen by the parameters in @ RequestMapping marked on the controller, and the readability of the code is not very good.

On the other hand, it is considered that this method greatly simplifies the writing of our code, and is easier to maintain, and the controller does not need to modify the code to adjust the directory at will.

The above is all the contents of the article "how SpringBoot automatically configures Url prefixes according to directory structure". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report