Clay Kirk
Clay Kirk

Reputation: 61

How to Set Micronaut Context Path

How does one go about setting a context path for a micronaut microservice? I want to do something similar to what is available in the Spring Framework where you can set the 'server.servlet.contextPath' property. I haven't been able to find anything in the micronaut docs here. I would like to set a base path for my microservice and my 'bar' controller (e.g. http://domain/foo/bar). In Spring this would look like

server:
  servlet:
    context-path: foo

I am currently using micronaut 1.0.0.M4. I appreciate the help.

Upvotes: 6

Views: 4575

Answers (4)

abedurftig
abedurftig

Reputation: 1248

Tested with Micronaut 3:

micronaut:
  application:
    name: micronaut-demo
  server:
    context-path: ${ENV_CONTEXT_PATH:`/`}

With this config you can define the context path as env variable, otherwise default to "/" (no specific context path).

Upvotes: 0

pbthorste
pbthorste

Reputation: 468

You can set the context path in the configuration with

micronaut:
  server:
    context-path: /my-path

Documentation: link

Upvotes: 9

Fredrik Hov
Fredrik Hov

Reputation: 142

Maybe not the answer you're looking for, but you can add a context path to a controller in the following way

@Controller("${micronaut.context.path:}/api"

This works for controllers, and will give you /api if the path is not defined. One big caveat with this, is that your swagger doc will no longer function correctly as the compile time generation does not take into account the property and will resolve it as /api

Upvotes: -1

Jeff Scott Brown
Jeff Scott Brown

Reputation: 27245

I would like to set a base path for my microservice and my 'bar' controller (e.g. http://domain/foo/bar)

If you want BarController to respond when a request is sent to /foo/bar, one way to get there is like this...

@Controller("/foo/bar")
public class BarController {

    @Get("/")
    public SomeReturnType index() {
        // ...
    }
}

Upvotes: 1

Related Questions