4

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

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

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).

Community
  • 1
  • 1
  • What are frame work are you using for the front end? thymeleaf, jsp etc.... You want something like @RestController @RequestMapping("/") public class IndexController{ ... }. So, when you go localhost:8080/ (index.html should display ,correct)? – JayC May 12 '17 at 15:04
  • Hi @Jesse, I'm using AngularJS for front end, hence no server side technologies. As you suggest I want localhost:8080/ to show index.html – highlysignificantbit May 12 '17 at 16:30
  • Put your AnuglarJS app in a separate project and serve it from a separate location during development. It means more work up front, but it pays dividends in superior design and massive tooling improvements within a month or two – Aluan Haddad May 14 '17 at 12:08
  • I indeed have two separate projects as I'm in favor of loose coupling. It only became a problem when wanting to deploy both back end and front end together :). – highlysignificantbit May 15 '17 at 11:43

1 Answers1

1

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.

gogstad
  • 3,607
  • 1
  • 29
  • 32
  • Hi @gogstad. I'm new to Spring and just trying to make a basic back end/front end setup work. Note that at this point I did not need to use Restcontroller nor RequestMapping since I used a CrudRepository and spring did take care of the request/handler mapping. However, since Spring boot is promoting convention over configuration, it seemed to me there should be some general solution for deploying a front end html server backed by a rest service. Therefor, I was looking for a setting that told Spring, for all **RestController**s, prefix its path with /api/ and map that path to this controller. – highlysignificantbit May 12 '17 at 22:27
  • (Shouldn't versioning should be threated as an orthogonal feature compared to separating front end from back end in stead of a by-product?) – highlysignificantbit May 12 '17 at 22:28
  • Spring-boot will serve anything you put in a `public` or `static` path on your classpath. As for accessing data in a `CrudRepository`, you need to have `@RequestMapping`s in a `@Controller` or `@RestController` in front in order to define the url-to-crudrepository method mapping; I'm not aware of any convention for this. There's no easy way to prefix only the `@RestControllers` of an application. The point about versioning was that if you need to version your api, you (probably) end up with static constants in RequestMappings anyway, you might as well have the api prefix in there as well. – gogstad May 13 '17 at 08:09