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 add a uniform request prefix to the web layer

2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces the knowledge of "how springboot adds a unified request prefix to the web layer". The editor shows you the operation process through an actual case. The operation method is simple and fast, and it is practical. I hope this article "how to add a unified request prefix to the web layer by springboot" can help you solve the problem.

How to add a uniform request prefix profile to the web layer

Application.properties global profile configuration:

Implementing WebMvcConfigurer Interface with server.servlet.context-path=/api

Override the configurePathMatch () method, code:

@ Configurationpublic class WebMvcConfig implements WebMvcConfigurer {/ * request path with a unified prefix * * @ param configurer * / @ Override public void configurePathMatch (PathMatchConfigurer configurer) {configurer.addPathPrefix ("/ api", c-> c.isAnnotationPresent (RestController.class) | | c.isAnnotationPresent (Controller.class));}}

All of the above add a unified prefix for the controller layer. If different versions want to use different request prefixes, you can optimize them as follows:

@ Configurationpublic class WebMvcConfig implements WebMvcConfigurer {/ * request path with a unified prefix * * @ param configurer * / @ Override public void configurePathMatch (PathMatchConfigurer configurer) {configurer.addPathPrefix ("/ api", c-> c.isAnnotationPresent (ApiRestController.class)) .addPathPrefix ("/ api/v2", c-> c.isAnnotationPresent (ApiV2RestController.class));}}

Add / api prefix to controller annotated with @ ApiRestController and / api/v2 prefix to controller annotated with @ ApiV2RestController.

@ ApiRestController and @ ApiV2RestController are custom annotations, inherited from @ RestController:

Import org.springframework.core.annotation.AliasFor;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.lang.annotation.*; @ Target (ElementType.TYPE) @ Retention (RetentionPolicy.RUNTIME) @ Documented@RestController@RequestMappingpublic @ interface ApiRestController {/ * * Alias for {@ link RequestMapping#name}. * / @ AliasFor (annotation = RequestMapping.class) String name () default "; / * * Alias for {@ link RequestMapping#value}. * / @ AliasFor (annotation = RequestMapping.class) String [] value () default {}; / * * Alias for {@ link RequestMapping#path}. * / @ AliasFor (annotation = RequestMapping.class) String [] path () default {};}

Use:

@ ApiRestController ("/ demo") public class DemoController extends BaseController {}

So the request address is: http://localhost:8080/api/demo

There are extra prefixes and suffixes on the spring web access page. Hello.jsp appears in the page.

Solution method

Remove the prefix and suffix configuration items in servlet

This is the end of the introduction to "how springboot adds a uniform request prefix to the web layer". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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