Spring boot: separate REST from static content

I am using spring-boot-starter-data-rest and spring-boot-starter-web. I made a simple project using CrudRepository allowing spring boot to generate remainder query mappings.

Now I want to add a client - make the rest of the calls - live under ./ . Hence I am trying to prefix rest call paths (and only those!) With / api .

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

  • server.contextPath = / api / *
  • spring.data.rest.basePath = / API / *.

But still static content (e.g. index.html, * .js, * .css) is not retrieved using. /. It also contains the "/ api /" prefix. The rest of the 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
spring.data.rest.basePath = / api / * property
works fine. (I still had a programmatic bean configuration in my sandbox overriding this setting).

+3


source to share


1 answer


Controllers

Spring is built to serve both HTML and JSON / XML. The former is done with Spring MVC Views and some templating engines like Thymeleaf, the latter is handled entirely by Spring and @RestController

.

It is not possible to have a context path only for controllers that return JSON or XML data and not for other controllers, this also applies to static content. Usually you use a static variable containing the prefix you want your APIs to use and use in your controller @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. You usually have to maintain older API versions when you break changes, at least for some time.

0


source







All Articles