3

I want to migrate my application to Spring Boot Jar Deployment. It currently uses Spring 4 without Boot.

I have a REST-API listening at /api/* and a Javascript-Frontend at src/main/webapp which can be accessed at /*.

Now i don't find a way doing the same in Boot.

I managed to get my api listening at /api/* by changing the server.context-path property but I didn't manage to register a second servlet to serve my js-frontend at /*. I know that src/main/webappis not supported by a jar deployment and i also know that Spring Boot serves static files from src/resources/public and src/resources/static. But in my case these folder then also points to /api/* because of the server.context-path change.

I tried is to register another servlet as a bean. This destroyed my api-endpoint.

What would be the best way to achieve this?

David Schilling
  • 2,442
  • 3
  • 17
  • 24

2 Answers2

0

First option - copy everything from src/main/webapp into src/resources/static. This is where Spring Boot looks for that static content.

Second option, keep using src/main/webapp, but configure your build to copy the static resources into target/classes/static. I provided the Maven config for this on a previous answer: Refreshing static content with Spring MVC and Boot

Note that if you go with the first option, if you wish to modify content and have it automatically reloaded without running a build, you will need to run your application inside your IDE. Going with the second option gives you the usual Tomcat reloading of static content if you're executing your jar, but could lead to some confusion. Personally, I go with the second option most of the time, as I like to run the application on the command line and edit my HTML and JavaScript with Chrome Dev Tools.

Community
  • 1
  • 1
Steve
  • 9,270
  • 5
  • 47
  • 61
  • I know this solution, but the problem is that i mapped the default servlet to `/api/*`. So also everything under `src/resources/static`will be mapped to `/api/*`. But i want it to be mapped to `/*`. – David Schilling Dec 12 '14 at 12:19
  • If you want something to map to `/*`, then you need to remove your servlet context mapping to `/api/*` and everything should work. Your REST controllers should define their own mappings to `/api/whatever`. – Steve Dec 12 '14 at 14:30
  • but because of that i tried to register a second servlet. it would also be okay if it starts at `/web/*`. but prefixing every controller with `api` doesn't sound very clean. – David Schilling Dec 12 '14 at 14:32
  • If you want a single application to respond to `/*`, then its root needs to be `/`, which means that the `@RequestMapping` at the top of each REST controller can be `/api/something`, and everything works out of the Spring Boot box. So far I find that having the actual mapping to my REST controllers in their mapping annotations makes the code easier to follow. – Steve Dec 12 '14 at 15:12
  • I want my REST-API to respond at `/api/* `and my static HTML to respond at `/*` or `/web/*`. And i don't want to prefix all my `@RequestMapping`s with `/api`. – David Schilling Dec 12 '14 at 15:19
  • 1
    In that case it would probably be worth re-writing your question to make clear that's what you're trying to achieve. – Steve Dec 12 '14 at 16:57
  • 1
    @DavidSchilling If all your API endpoints are within a single controller you can add a class-level `@RequestMapping("/api/")` that will be prefixed to all method-level `@RequestMapping`s. – kryger Dec 16 '14 at 15:11
0

For multiple mappings, you will have to add one per mapping and map that in controller like below.

 @Controller
 @RequestMapping(value = { "${url.mapping}","${url.mapping.two}" })
 public class CustomerController{
Deepika
  • 53
  • 5