highlysignificantbit
highlysignificantbit

Reputation: 158

spring boot: separate REST from static content

I'm using spring-boot-starter-data-rest and spring-boot-starter-web. I've made a simple project using a CrudRepository, letting spring boot generate the rest request mappings.

Now, I want to add a client -- making the rest calls -- live under ./. Hence, I'm trying to prefix the paths for the rest calls (and only those!) with /api.

I've tried the answers from : How to specify prefix for all controllers in Spring Boot? using settings in the application.properties file

But still the static content (e.g. index.html, *.js, *.css) is not fetched using ./. There urls are also prefixed by "/api/". The rest calls are properly served under /api/foos.

Is there a way to tell spring not to treat urls that lead to sources located in src/main/resources/public as 'rest-controllers'?

Update

Setting the property
spring.data.rest.basePath=/api/*
works perfectly. (I still had a programmatic bean configuration in my sandbox overriding this setting).

Upvotes: 4

Views: 2102

Answers (1)

gogstad
gogstad

Reputation: 3739

Spring controllers are made for serving both HTML and JSON/XML. The first one is done via Spring MVC Views and some template engine like Thymeleaf, the latter is handled entirely by Spring and @RestController.

There's no way to have a context path for only the controllers that returns JSON or XML data, and not for the other controllers as well, this also goes for static content. What you typically do is have some static variable containing the prefix you want for your APIs, and the use that in the controller's @RequestMapping. i.e.

@RestController
@RequestMapping(MyConstants.API_LATEST + "/bookings")
public class MyBookingsController {
    ...
}

You probably want to approach the prefix problem with something along these lines anyway. It is common to have to support older API versions when you have breaking changes, at least for some time.

Upvotes: 1

Related Questions