Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 454 Vote(s) - 3.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to set base url for rest in spring boot?

#11
With spring-boot 2.x you can configure in application.properties:

spring.mvc.servlet.path=/api

Reply

#12
Per Spring Data REST [docs][1], if using *application.properties*, use this property to set your base path:

spring.data.rest.basePath=/api

But note that Spring [uses][2] [relaxed binding][3], so this variation can be used:

spring.data.rest.base-path=/api

... or this one if you prefer:

spring.data.rest.base_path=/api

If using *application.yml*, you would use colons for key separators:

spring:
data:
rest:
basePath: /api

(For reference, a related [ticket][4] was created in March 2018 to clarify the docs.)


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

[4]:

[To see links please register here]

Reply

#13
worked server.contextPath=/path

Reply

#14
Try using a PathMatchConfigurer (Spring Boot 2.x):

```
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.addPathPrefix("api", HandlerTypePredicate.forAnnotation(RestController.class));
}
}
```
Reply

#15
With Spring Boot 1.2+ (<2.0) all it takes is a single property in application.properties:


spring.data.rest.basePath=/api

ref link :

[To see links please register here]


For 2.x, use

server.servlet.context-path=/api
Reply

#16
You can create a custom annotation for your controllers:

Use it instead of the usual @RestController on your controller classes and annotate methods with @RequestMapping.

Works fine in Spring 4.2!
Reply

#17
For those who use YAML configuration(application.yaml).

**Note**: this works only for `Spring Boot 2.x.x`

```
server:
servlet:
contextPath: /api

```

If you are still using `Spring Boot 1.x`

```
server:
contextPath: /api
```
Reply

#18
`server.servlet.context-path=/api` would be the solution I guess. I had the same issue and this got me solved. I used server.context-path. However, that seemed to be deprecated and I found that `server.servlet.context-path` solves the issue now. Another workaround I found was adding a base tag to my front end (H5) pages. I hope this helps someone out there.

Cheers
Reply

#19
I did some research on the differences of spring properties mentioned in this thread. Here are my findings if anybody is wondering.

spring.data.rest.basePath Property
----

```
spring.data.rest.basePath=/api
```
This property is specifically for **Spring Data Rest** projects. It won't work in a usual Spring MVC projects.

To change the context path in MVC projects, you can use those two properties mentioned below. Let me mention the differences too.

server.servlet.context-path Property
-----------


```
server.servlet.context-path=/api
```
This one sets the context path on your web servelet. This property perfectly works fine in both spring mvc and spring data rest projects. **But,** the differnce is the request url will be filter out before reaching spring interceptors. So it will respond with HTML on bad request. Not Spring's or your own custom JSON response (in @ResponseBodyAdvice annotated class) defined. To overcome that, you should use this property below.

spring.mvc.servlet.path Property
-----

```
spring.mvc.servlet.path=/api
```
This will filter the request URL in spring mvc interceptors and will respond default/your custom JSON response if you invoke a bad request.

Conclusion:
---

So as the OP's question, I would suggest that he should use **spring.mvc.servlet.path** to change the context path.
Reply

#20
For Spring WebFlux the approach is similar to Harald's, but with the obvious WebFlux configuration set up:

@Configuration
public class WebFluxConfig implements WebFluxConfigurer {

@Override
public void configurePathMatching(PathMatchConfigurer configurer) {
configurer.addPathPrefix("/api", HandlerTypePredicate.forAnnotation(RestController.class));
}
}

And for Kotlin it's:

@Configuration
class WebFluxConfig : WebFluxConfigurer {
override fun configurePathMatching(configurer: PathMatchConfigurer) {
configurer.addPathPrefix("/api", HandlerTypePredicate.forAnnotation(RestController::class.java))
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through