How to Set Base Url for Rest in Spring Boot

How to set base url for rest in spring boot?

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

spring.data.rest.basePath=/api

ref link : https://docs.spring.io/spring-data/rest/docs/current/reference/html/#getting-started.changing-base-uri

For 2.x, use

server.servlet.context-path=/api

Setting Spring REST controllers base url without changing the static resources base url

You should not use this property as it changes the context path for the whole application.

Why not simply specify /api/yourResource in the RequestMapping annotation such as :

@RestController
@RequestMapping("/api/oneController")
public class OneController { ... }

.....

@RestController
@RequestMapping("/api/anotherController")
public class AnotherController { ... }

How do I rewrite the app's base URL in spring-data-rest links?

If Spring MVC knows about your load balancer/proxy, it can automatically rewrite generated URLs in Spring Data REST and your own controllers (with MvcUriComponentsBuilder) to reflect the original request URL, including scheme, port, and more.

There's a built-in Servlet filter named ForwardedHeaderFilter that handles this for you by inspecting well-known forwarding headers such as X-Forwarded-For; if you're using Spring Boot, you can set server.forward-headers-strategy = framework to get one registered, or you can use @Import(ForwardedHeaderFilter.class) to create a bean that Boot will then apply to the container. If you're using Spring MVC without Boot, add this filter to your Servlet configuration.

How to set a different base url for each controller

Yes, you can extract the common part of the path and put it into @RequestMapping on your controller:

@RestController
@RequestMapping("/api/customer")
public class CustomerController {

// ...

@GetMapping("/getById/{id}")
public Customer findCustomerById(@PathVariable int id) {
return service.getCustomerById(id);
}
}

and

@RestController
@RequestMapping("/api/provider")
public class ProviderController {

// ...

@GetMapping("/getById/{id}")
public Provider findProviderById(@PathVariable int id) {
return service.getProviderById(id);
}
}

How to ignore base path url in Spring MVC?

You could use

@RestController
@RequestMapping("/v1")
public class SomeController {

on classlevel. Then all @RequestMapping in this Class will be accesible with the prefix "/v1". On a sidenote I would suggest not to use "." in your urls.

If you don´t want to use the String in each Controller the only option that I can think of is the following. I tried it out and it seems to be working fine for me.

@RestController
@RequestMapping("/${your.custom.property.in.application.properties}")
public class SomeController {

your.custom.property.in.application.properties = 1

--> The URL http://localhost:8080/1/test sends me a repsonse. The URL mapping was successful. Debugger stops in breakpoints in this Controller

How to query data based based on different query parameter spring boot

you can pass multiple params in url like, for example:

http://localhost:2000/custom?brand=dell&limit=20&price=20000&sort=asc

and in order to get this query fields , you can use map like

@RequestMapping(method = RequestMethod.GET, value = "/custom")
public String controllerMethod(@RequestParam Map<String, String> customQuery) {

System.out.println("customQuery = brand " + customQuery.get("brand"));
System.out.println("customQuery = limit " + customQuery.get("limit"));
System.out.println("customQuery = price " + customQuery.get("price"));
System.out.println("customQuery = other " + customQuery.get("other"));
System.out.println("customQuery = sort " + customQuery.get("sort"));

return customQuery.toString();
}

Spring REST set base URL by module

You can:

Build each module into different war. This would make servlet container add default war name at the start of url, for all the content in war.
Change routing in dispatcher-servlet.xml for each module and prefix that with the name you want.

These 2 would work if these modules are all "subprojects".

Ultimately you can fiddle with you dispatcher-servlet to map everything the way you want.



Related Topics



Leave a reply



Submit