Home > Java > javaTutorial > body text

How to add URL prefix to SpringBoot multiple controllers

WBOY
Release: 2023-05-12 18:37:20
forward
2037 people have browsed it
Preface

In some cases, the prefixes in the service controller are consistent. For example, the prefix of all URLs is /context-path/api/v1, and you need to add it to some URLs. Uniform prefix.

The conceivable solution is to modify the context-path of the service and add api/v1 to the context-path. Modifying the global prefix in this way can solve the above problem, but there are drawbacks. If the URL has multiple prefixes , for example, some URLs need to be prefixed with api/v2, so they cannot be distinguished. If some static resources in the service do not want to add api/v1, they cannot be distinguished.

The following implements the unified addition of certain URL prefixes through custom annotations.

1. Add prefix configuration in the configuration file

If you need multiple prefixes, add multiple sets of configurations, for example, add: api.prefix.v2=/api/v2


url prefix configuration

##api.prefix.v1=/api /v1

2. Configure mapped entities

@Data
@Component
@ConfigurationProperties(prefix = "api.prefix")
public class ApiPrefix {
    private String v1;
}
Copy after login

3. Custom annotations

This annotation function is consistent with

@RestController

and corresponds to For the configuration of api.prefix.v1, if there are multiple sets of configurations, just define multiple annotationsHow to add URL prefix to SpringBoot multiple controllers

@RestController
@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiV1RestController {
}
Copy after login

4. Customize PathMatch to add prefix######Add a configuration class to inherit WebMvcConfigurer and override configurePathMatch Method, add the corresponding prefix to the interface in the controller with the ApiV1RestController annotation on the class. ###
@AutoConfiguration
public class WebMvcConfig implements WebMvcConfigurer {
    @Autowired
    private ApiPrefix apiPrefix;
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.addPathPrefix(apiPrefix.getV1(), c -> c.isAnnotationPresent(ApiV1RestController.class));
    }
}
Copy after login
###5. Test######You need to use @ApiV1RestController annotation instead of @RestController annotation on the corresponding controller###
@ApiV1RestController
@RequestMapping("/test/apiv1")
public class TestApiV1RestController {
    @GetMapping()
    public ResponseEntity get() {
        return new ResponseEntity();
    }
}
Copy after login
#########

The above is the detailed content of How to add URL prefix to SpringBoot multiple controllers. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template