change spring boot 2.0 context-path - spring

I want to change context path for spring boot 2 for example i want to serve on http://localhost:8080/test/
i mean it not working for me with spring-boot-starter-webflux:2.0.0.RELEASE
it only working with spring-boot-starter-web::2.0.0.RELEASE
I have tried
server.servlet.context-path=/test
But nothing happened to me still serve on url http://localhost:8080/

If you use the servlet API then the property is now called
server.servlet.context-path=/myapp

As confirmed by Andy Wilkinson #andy-wilkinson of the Spring Boot team via Gitter
There’s no concept of context path in WebFlux so there’s no equivalent property
i.e WebFlux doesn't support context path configuration

In spring boot 2.3.x you can set spring.webflux.base-path property
spring.webflux.base-path=/path

In reference to Spring Boot 2.x. below configuration apply for application.yml
server:
port: 8080
servlet:
context-path: /test
For application.properties configuration are
server.port=8080
server.servlet.context-path= /test

For use cases where WebFlux application is behind load balancer/proxy you can use dedicated class - ForwardedHeaderTransformer that will extract path context from X-Forwarded-Prefix and will add it to ServerHttpRequest.
Doing so you won't need to modify global context-path (which does not make sense in WebFlux)
More about it here: https://docs.spring.io/spring/docs/current/spring-framework-reference/web-reactive.html#webflux-web-handler-api

Related

Spring cloud Zuul reverse proxy routing not working with versioned and context path specified rest application

Created a spring boot application and sets context path and version. Then added configurations for zuul proxy for routing. Me refer the spring example Routing and Filtering. Only difference in application.properties is added contextPath and version. Also enabled zuul proxy using #EnableZuulProxy annotation. But the routing is not working. When I remove the version and context it works.
application.properties file
server.port=8080
server.servlet.context-path=/app1
version=v1
zuul.routes.foos.path=/api/**
zuul.routes.foos.url=http://localhost:8081/app2/v1/
Finally resolved the issue. Forgot to add version in zuul path. Need to add version also in zuul path.
application.properties
server.port=8080
server.servlet.context-path=/app1
version=v1
zuul.routes.foos.path=/v1/api/**
zuul.routes.foos.url=http://localhost:8081/app2/v1/

How to test a Spring Boot actuator endpoint when the 'server.port' and 'management.server.port' properties are different

I would like to test the /health endpoint of my Spring Boot microservice with the help of the MockMvc bean.
But in my case the server.port and management.server.port properties have different values.
And the GET request issued by my MockMvc always ends on the port defined in server.port.
There is already an answer there : Spring boot's actuator unavailable when set management port
But I cannot find this ManagementContextResolver class in Spring Boot 2.2.2.
So far, I found out that the 'management' ConfigurableWebServerApplicationContext is created in org.springframework.boot.actuate.autoconfigure.web.server.ManagementContextAutoConfiguration.DifferentManagementContextConfiguration, but I don't see how I can obtain a reference to it..
try this, its from the SpringBoot Docs :
#WebIntegrationTest({"server.port=8080", "management.port=8090"})

How to test application yml resource file settings that uses Spring Boot?

For example, I have settings like code below which Spring Boot Actuator uses.
management:
server:
port: 60001
security:
enabled: false
And I found out that if I will write this setting with mistakes. For example (code below):
management:
server:
port: 60001
security:
enabled: fase # wrong typed a `false` word
Spring Boot will not do anything, and the application will have build successfully.
Spring boot will validate the data as long as #ConfigurationProperties exist in core. The problem is that your #ConfigurationProperties may not exist before or already deprecated in your spring boot version.
Below is the comparison of management.server class with #ConfigurationProperties.
Try to change the port with string and spring boot with validate the data.
Look also at ManagementContextAutoConfiguration.class if your using spring boot 2 you will see that it loads the class ManagementServerProperties and WebEndpointProperties
If you still needed to validate your yaml you can refer to SnakeYAML.

Spring Boot 2: Refresh properties on the fly not working

I have followed this official tutorial Getting Started Centralized Configuration using spring boot 2.0.0.RELEASE and spring cloud Finchley.M8
But refreshing properties on the fly (Without restart) is not working.
After Some debugging, I noticed that in method refresh() from ContextRefresher.class, it returns the changed keys correctly, but after reconstructing the bean annotated with #RefreshScope in the next use. It still sees the old value not the updated one.
Note: This was working perfectly with spring boot v 1.5.6 and spring cloud Edgware.RELEASE.
Any help please?
Thanks
It seems spring.cloud.config.uri in spring boot 2.0.1.RELEASE always looking for port 8888 and not accepting other values, so I put the below configuration (you can ignore it, as it is the default value for the client, and the server should run on port 8888)
spring:
cloud:
config:
uri: http://localhost:8888
I also tried to expose all other services in the client for testing as follows
management:
endpoints:
web:
exposure:
include: '*'
or use the following to allow only refresh
management:
endpoints:
web:
exposure:
include: refresh
Then called POST method not GET for refreshing
$ curl -X POST localhost:8080/actuator/refresh -d {} -H "Content-Type: application/json"
Finally, it works.
Instead of Method "POST" , use "OPTIONS" method to call the "actuator/refresh" for spring boot 2.0 or higher.
For lower versions (<2.0), use the endpoint "context/refresh"
Make sure , you have management.endpoints.web.exposure.include=* defined in application.properties.
Use below in application.properties-
management.endpoint.refresh.enabled=true
management.endpoint.restart.enabled=true
management.endpoint.health.enabled=true
management.endpoint.health.show-details=always
management.endpoint.info.enabled=true
management.endpoints.web.exposure.include=info,health,refresh
Using yaml configuration file it was not working for me and when switched to properties file, it worked with above configuration.
Thanks

How to customize Spring Boot Actuator endpoints

I'm trying to implement Spring Boot Actuator on a non "Boot Application", is there a way to configure a pattern to endpoints like endpoint/health,endpoint/metrics? Then I'll create a second realm in my existing Spring Security configuration, I tried the documentation but I could not find a thing about.
Thanks.
Did you mean management.contextPath=/endpoint (docs here)?

Resources