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 does SpringBoot generate the url path of the interface based on the directory path

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

Share

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

This article mainly explains the "SpringBoot how to generate the interface url path according to the directory path", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how SpringBoot generates the interface url path according to the directory path"!

Generate the url path of the interface based on the directory path

First, we create a new AutoPrefixUrlMapping class, which inherits from RequestMappingHandlerMapping, to get the new url.

Public class AutoPrefixUrlMapping extends RequestMappingHandlerMapping {/ / reads the root directory @ Value ("${api-package}") private String apiPackagePath; @ Override protected RequestMappingInfo getMappingForMethod (Method method, Class handlerType) {RequestMappingInfo mappingInfo = super.getMappingForMethod (method, handlerType); if (mappingInfo! = null) {String prefix = this.getPrefix (handlerType); RequestMappingInfo newMappingInfo = RequestMappingInfo.paths (prefix). Build (). Combine (mappingInfo) Return newMappingInfo;} return mappingInfo;} / / get the prefix private String getPrefix (Class handlerType) {String packageName = handlerType.getPackage (). GetName (); String newPath = packageName.replaceAll (this.apiPackagePath, "); return newPath.replace (". "," / ");}} configuration file application.proprties is as follows: api-package = com.lyn.miniprogram.api

The root directory used to declare the url

Then we create an AutoPrefixConfiguration class to add AutoPrefixUrlMapping to the container of Springboot

@ Componentpublic class AutoPrefixConfiguration implements WebMvcRegistrations {@ Override public RequestMappingHandlerMapping getRequestMappingHandlerMapping () {return new AutoPrefixUrlMapping ();}}

The test interfaces are as follows:

Url will automatically be prefixed with / v1, and the test passes! Of course, if the directory level is more complex, it can also be achieved through the above code.

API springboot requests interface path to return 404.

Springboot starts the project normally, and the access interfaces are all normal. A new API request interface path is added, and an error 404 is reported when accessing the interface.

Idea springboot

Http://localhost:8080/cuer/apSw?ad=2893e6fce42&_=161607509 404

The interface was not scanned

Baidu said that the packet of the interface was placed in the wrong position, so that the interface was not scanned, but in my case, all the existing interfaces of the system can be accessed, and the location of the new interface is the same as that of the old interface. And check that the new interface package matches the location of the spring boot startup class and can be scanned, so rule out the situation where the new interface package is placed incorrectly.

Configuration or code writing problem

Check that the backend interface code is written correctly.

Check whether there is an error in calling the backend API to pass parameters (the API has parameters, no parameters are passed directly, and the breakpoint accesses the interface, and you can enter the interface, indicating that the interface is right). It is found that the return page reports 404. After comparison, it is found that there is no page corresponding to the page path. Modify the page path and revisit it successfully.

@ Slf4j@Controller@RequestMapping (value = {"/ customer"}) @ AllArgsConstructorpublic class CerCustomerController {private final PsionsUtil pnsUtil;private final ICCredService crdService;private final ICrEvalService ervice;/** * jumps to the list interface * * @ return * / @ GetMapping ("/ index") public String customerCredIndex () {return "/ business/customer/index" Jump to the view interface * * @ return*/@GetMapping ("/ apeShw") public String apseShw (String ad, Model model) {model.addAttribute ("apId", aId); if ("CD" .equals (perUtil.getreTpe () {return "/ bess/cuer/evfo";} else {CeerVo ceo = creice.gtCByAlyId (ad); model.addAttribute ("cd", cVo); return "/ busis/cr/co" Thank you for your reading. The above is the content of "how SpringBoot generates the url path of the interface according to the directory path". After the study of this article, I believe you have a deeper understanding of how SpringBoot generates the url path of the interface according to the directory path, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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