Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot with context path excluding statics

I am using Spring Boot as an API and Angular as the frontend for my Application. I build with Maven and so configured the frontend-maven-plugin so it copies all the Angular dist folder into the final jar when building.

What I would like to have is all of my controllers' mapping to have a prefix like '/api' so it becomes '/api/users', but also that my static resources mappings stay what they are, like only '/sign-up' instead of '/api/sign-up'.

So I searched for the server.context-path and server.servlet-path properties but none of them worked. Anyone can help me please ?

like image 428
Coco Avatar asked Mar 17 '26 20:03

Coco


1 Answers

@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        configurer.addPathPrefix("/api",
            HandlerTypePredicate.forAnnotation(RestController.class)
                .and(HandlerTypePredicate.forBasePackage("com.company.api")));
    }
}
like image 187
Byung-Kwon Moon Avatar answered Mar 19 '26 10:03

Byung-Kwon Moon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!