Get the App
SLTechnology News&Howtos  ›  Development  › 

How does springboot add a uniform request prefix to the web layer

Shulou Source: shulou.com Published: 2022-06-01 15:09:12 10月06日 Update

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.

Tags: Prefixes unification configuration differences methods annotations knowledge suffixes files industries paths pages utility code global content address practicality reality situation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Technology Apple Xiaomi Linux