Optional #PathVariable in Spring based rest call - java

I am using Spring boot 2.2.2 with java 1.8. I have a rest end point /getViewAnalyticsByDimension of post type. Now to accommodate a feature I need to change the api to /getViewAnalyticsByDimension/{priceAgreementId}. I want to declare path variable priceAgreementId as optional. But when I post the request to /getViewAnalyticsByDimension when there is no price agreement id from Postman it is not able to find the controller method. I can ask my frontend counter-part to send "blank" when there is no priceAgreementId and the actual priceAgreementId when it is available. But that is not elegant.

You can have multiple request mappings for a given method. So in your case you can do something as follows to make it work with and without path variable:
#RequestMapping(value = {"/getViewAnalyticsByDimension", "/getViewAnalyticsByDimension/{priceAgreementId}"}")
public Object getObject(#PathVariable Optional<Object> priceAgreementId) {
//...
}
If you are using a legacy version of spring you can have two different methods:
#RequestMapping(value = "/getViewAnalyticsByDimension/{priceAgreementId}")
public Object getObject(#PathVariable(name = "priceAgreementId") Object priceAgreementId) {
//...
}
#RequestMapping(value = "/getViewAnalyticsByDimension")
public Object getObject() {
//...
}
You can checkout https://www.baeldung.com/spring-optional-path-variables more info

You have to modify your method following way,
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Optional;
#RestController
public class Test {
#RequestMapping(value = {"/getViewAnalyticsByDimension", "/getViewAnalyticsByDimension/{priceAgreementId}"})
public Object getObject(#PathVariable(required = false) Optional<String> priceAgreementId) {
if (priceAgreementId.isPresent()) {
// do something
} else {
// do something else
}
return something;
}
}
I checked both URLs accessible, (It's just an example)
http://localhost:8888/getViewAnalyticsByDimension
http://localhost:8888/getViewAnalyticsByDimension/5

Related

micronaut #RequestScope - not creating bean per incoming http-request

I have a class the following class as RequestScope bean:
#RequestScope
class RequestContext {
private String requestId;
private String traceId;
private String authorisedId;
private String routeName;
// few more fields
#Inject RequestContext(SecurityContext securityContext) {
this.requestId = UUID.randomUUID().toString();
if(securityService.getAuthentication().isPresent()){
this.authorisedId = (securityService
.getAuthentication().get()).getUserId().toString();
}
}
/* to be updated in controller method interceptors */
public void updateRouteName(String name){
this.routeName = name;
}
The idea is to have an object containing the REST request level custom data accessible across the application, the scope of the this obviously should be within the current request. This can be used for say.. logging - whenever devs log anything from the application, some of the request meta data goes with it.
I am not clear what the #RequestScope bean really is:
From its definition - my assumption is it is created for every new http-request and same instance is shared for the life of that request.
when is it constructed by Micronaut ? Is it immutable ?
Across multiple requests I can see the same requestId ( expecting new UUID for every request)
Is it the right use-case for #RequestScope bean?
I was running into an issue regarding #RequestScope so I'll post an answer here for others.
I was trying to inject a #RequestScope bean into an HTTP filter, set a value in the bean, and then read it later from another bean. For example
#RequestScope
class RequestScopeBean() {
var id: Int? = null
}
#Filter
class SetRequestScopeBeanHere(
private val requestScopeBean: Provider<RequestScopeBean>
) {
override fun doFilterOnce(request: HttpRequest<*>, chain: ServerFilterChain): Publisher<MutableHttpResponse<*>> {
requestScopeBean.get().id = // id from Http Request
}
}
#Singleton
class GetRequestScopeBeanHere(
private val requestScopeBean: Provider<RequestScopeBean>
) {
fun getIdFromRequestScopeBean() {
println(requestScopeBean.get().id)
}
}
In this example before any controller is executed my filter (SetRequestScope) is called, this will set requestScopeBean.id but the key is that the request scope bean must be wrapped in a javax.inject.Provider, otherwise setting the field won't work.
Down the line, when GetRequestScopeBeanHere::getIdFromRequestScopeBean is called it'll have access to the requestScopeBean.id set earlier
This is intentional by Micronaut:
https://github.com/micronaut-projects/micronaut-core/issues/1615
when is it constructed by Micronaut ?
A #RequestScope bean is created during request processing, the first time the bean is needed.
Is it immutable ?
It could be. You get to decide if the bean is mutable or not when you write the class. As written in your example, RequestContext is mutable. If you remove the updateRouteName method, that bean would be immutable.
Is it the right use-case for #RequestScope bean?
I don't think so, but that is really an opinion based question.
EDIT: Based On Comments Added Below
See the project at https://github.com/jeffbrown/rscope.
https://github.com/jeffbrown/rscope/blob/2935a4c1fc60f350198d7d3c1dbf9a7eedd333b3/src/main/java/rscope/DemoController.java
package rscope;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
#Controller("/")
public class DemoController {
private final DemoBean demoBean;
public DemoController(DemoBean demoBean) {
this.demoBean = demoBean;
}
#Get("/doit")
public String doit() {
return String.format("Bean identity: %d", demoBean.getBeanIdentity());
}
}
https://github.com/jeffbrown/rscope/blob/2935a4c1fc60f350198d7d3c1dbf9a7eedd333b3/src/main/java/rscope/DemoBean.java
package rscope;
import io.micronaut.runtime.http.scope.RequestScope;
#RequestScope
public class DemoBean {
public DemoBean() {
}
public int getBeanIdentity() {
return System.identityHashCode(this);
}
}
https://github.com/jeffbrown/rscope/blob/2935a4c1fc60f350198d7d3c1dbf9a7eedd333b3/src/test/java/rscope/DemoControllerTest.java
package rscope;
import io.micronaut.http.client.RxHttpClient;
import io.micronaut.http.client.annotation.Client;
import io.micronaut.test.annotation.MicronautTest;
import org.junit.jupiter.api.Test;
import javax.inject.Inject;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
#MicronautTest
public class DemoControllerTest {
#Inject
#Client("/")
RxHttpClient client;
#Test
public void testIndex() throws Exception {
// these will contain the identity of the the DemoBean used to handle these requests
String firstResponse = client.toBlocking().retrieve("/doit");
String secondResponse = client.toBlocking().retrieve("/doit");
assertTrue(firstResponse.matches("^Bean identity: \\d*$"));
assertTrue(secondResponse.matches("^Bean identity: \\d*$"));
// if you modify DemoBean to be #Singleton instead of
// #RequestScope, this will fail because the same instance
// will be used for both requests
assertNotEquals(firstResponse, secondResponse);
}
}

Spring Boot - Ambiguous Mapping

Pulling my hair out over this one, makes absolutely no sense
#RestController("/firmwareAShkcwdsdskl")
public class FirmwareController {
#PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Result> createNewFirmwareVersion(DetailedFirmwareVersionRequest detailedFirmwareVersionRequest) {
// Code Block
}
}
#RestController("/jobs/firmwareUpgrade")
public class FirmwareUpgradeController {
#PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Result> createNewJob(DetailedFirmwareUpgradeRequest detailedFirmwareUpgradeRequest) {
// Code Block
}
}
Attempting to start my Spring Boot application with these two controllers is throwing the following error
Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map '/jobs/firmwareUpgrade' method
eshepherd.admin.api.controller.FirmwareUpgradeController#createNewJob(DetailedFirmwareUpgradeRequest)
to {POST , produces [application/json]}: There is already '/firmwareAShkcwdsdskl' bean method
eshepherd.admin.api.controller.FirmwareController#createNewFirmwareVersion(DetailedFirmwareVersionRequest) mapped.
The class name, method name, request mapping and parameters are all unique, I just don't understand it.
As you can tell I've descended into pure frustration trying all kinds of string changes to just get it to work initially, but if anyone could help me identify the problem I'd be extremely grateful.
Edit: Using Spring-Boot 2.2.4
As you mentioned in your own answer: Yes, you misunderstood. This is an easy one to confuse.
#RestController's value is the component name, not the request mapping path. Take a look at the source for org.springframework.web.bind.annotation.RestController#value.
It is very similar to org.springframework.stereotype.Component#value and the others in org.springframework.stereotype (in spring-context). You have have the correct annotations now:
#RestController
#RequestMapping("/firmware")
public class FirmwareRestController {
// ...
}
Okay.. I managed to get it compiling.. I think I was misunderstanding how you can use the #RestController annotation
If you provide
#RestController
#RequestMapping("/firmware")
Instead of
#RestController("/firmware")
It will build. I guess I had assumed the default argument on the controller would be more useful, and looked to be working as I expected from the build messages.
Hope this helps someone!
The framework is complaining that there exists a post path / from the 2 controllers where the mapping is not specified for any one of them hence it cannot distinguish the exact path.
Can you please try the following code:
#RestController
#RequestMapping("/firmwareAShkcwdsdskl")
public class FirmwareController {
#PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Result> createNewFirmwareVersion(DetailedFirmwareVersionRequest detailedFirmwareVersionRequest) {
// Code Block
}
}
#RestController
#RequestMapping("/jobs/firmwareUpgrade")
public class FirmwareUpgradeController {
#PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Result> createNewJob(DetailedFirmwareUpgradeRequest detailedFirmwareUpgradeRequest) {
// Code Block
}
}
#RequestMapping at controller level should define the root path for all the APIs in that path say for example I have to create a controller related to feeds API I would do:
#RestController
#RequestMapping("/api/feeds")
class FeedsController {
//Constructor inject fields here
#Autowired
FeedsController() {
}
#GetMapping("/")
ResponseEntity<Map<String, String>> get() {
return ResponseEntity.ok(Collections.emptyMap());
}
#PostMapping("/")
ResponseEntity<Map<String, String>> post() {
return ResponseEntity.ok(Collections.emptyMap());
}
/**
* Standard way to implement delete is to soft delete
* #return
*/
#DeleteMapping("/")
ResponseEntity<Map<String, String>> delete() {
return ResponseEntity.ok(Collections.emptyMap());
}
}

Modifying object for each web service call

In our web application we have a lot of REST services. Suddenly it found out that we need to modify one object inside of each request before we go on.
So let's say we have n different controllers with REST services. In each controller, before we call the service from next layer, we need to modify an object inside the request.
The question is how to achieve this without providing hundreds of changes inside the controllers... Is there any simple way to do this?
UPDATE:
#RestController
public class OrderController {
#Autowired
private OrderService orderService;
#RequestMapping(path = "/order", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public OrderResponse getOrderData(#RequestHeader HttpHeaders httpHeaders,
#RequestBody OrderDataRequest orderDataRequest) {
// Use here interceptor to modify the object Details
// (inside OrderDataRequest) before below call:
OrderResponse resp = orderService.getOrderData(orderDataRequest);
return resp;
}
#RequestMapping(path = "/cancel/{orderId}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public boolean cancelOrder(#RequestHeader HttpHeaders httpHeaders,
#RequestBody Details details, #PathVariable Integer orderId) {
// Use here interceptor to modify object Details before below call:
return orderService.cancelOrder(details, orderId);
}
}
In each controller I need to modift the object Details, which as you can see could be inside another object like in the first example or exist alone like in the second option.
You can use Spring AOP to achieve this. Another option using traditional Filters.
You should consider writing an interceptor, that would allow you to do what you want .
You could also use AOP to do this.. though, I think it's quite over-complicated, especially when such a solution already exists through interceptors!
EDIT :
A few other links :
EDIT 2 :
Follow the "before advice" from mykong.com example, then go that way to edit your specific object according to its class (for exemple) :
package com.your.company;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class HijackBeforeMethod implements MethodBeforeAdvice
{
#Override
public void before(Method method, Object[] args, Object target)
throws Throwable {
for(Object arg : args) {
if(com.your.company.OrderDataRequest.class.isAssignableFrom(arg.getClass())) {
// update you object here
}
}
}
}
- Get response body
- Json To Java

Using #RequestLine with Feign

I have a working Feign interface defined as:
#FeignClient("content-link-service")
public interface ContentLinkServiceClient {
#RequestMapping(method = RequestMethod.GET, value = "/{trackid}/links")
List<Link> getLinksForTrack(#PathVariable("trackid") Long trackId);
}
If I change this to use #RequestLine
#FeignClient("content-link-service")
public interface ContentLinkServiceClient {
#RequestLine("GET /{trackid}/links")
List<Link> getLinksForTrack(#Param("trackid") Long trackId);
}
I get the exception
Caused by: java.lang.IllegalStateException: Method getLinksForTrack not annotated with HTTP method type (ex. GET, POST)
Any ideas why?
I wouldn't expect this to work.
#RequestLine is a core Feign annotation, but you are using the Spring Cloud #FeignClient which uses Spring MVC annotations.
Spring has created their own Feign Contract to allow you to use Spring's #RequestMapping annotations instead of Feigns. You can disable this behavior by including a bean of type feign.Contract.Default in your application context.
If you're using spring-boot (or anything using Java config), including this in an #Configuration class should re-enable Feign's annotations:
#Bean
public Contract useFeignAnnotations() {
return new Contract.Default();
}
The reason I got this error is that I used both #FeignClient and #RequestLine annotations in my FooClient interface.
Before a fix.
import org.springframework.cloud.openfeign.FeignClient; // #FeignClient
import feign.RequestLine; // #RequestLine
import org.springframework.web.bind.annotation.PathVariable;
#FeignClient("foo")
public interface FooClient {
#RequestLine("GET /api/v1/foos/{fooId}")
#Headers("Content-Type: application/json")
ResponseEntity getFooById(#PathVariable("fooId") Long fooId); // I mistakenly used #PathVariable annotation here, but this should be #Param
}
Then, I got this error.
Caused by: java.lang.IllegalStateException: Method FooClient#getFooById(Long) not annotated with HTTP method type (ex. GET, POST)
After a fix
// removed #FeignClient
// removed #PathVariable
import feign.Param; // Added
import feign.RequestLine; // #RequestLine
// removed #FeignClient("foo")
public interface FooClient {
#RequestLine("GET /api/v1/foos/{fooId}")
#Headers("Content-Type: application/json")
Foo getFooById(#Param("fooId") Long fooId); // used #Param
}
If you are interested in the configuration classes.
Please note that I tried to create Feign Clients Manually.
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
import org.springframework.context.annotation.Configuration;
#Configuration
#ComponentScans(value = {
#ComponentScan(basePackages = {
"com.example.app.service.web.client",
})
})
public class FeignConfig {
#Value(value = "${app.foo.service.client.url}")
protected String url; // http://localhost:8081/app
#Bean
public FooClient fooClient() {
FooClient fooClient = Feign.builder()
// .client(RibbonClient.create())
.client(new OkHttpClient())
.encoder(new GsonEncoder())
.decoder(new GsonDecoder())
.logger(new Slf4jLogger(FooClient.class))
.logLevel(Logger.Level.FULL)
.target(FooClient.class, url);
return fooClient;
}
}
References
https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-feign.html
https://www.baeldung.com/intro-to-feign
https://www.baeldung.com/feign-requestline
https://stackoverflow.com/a/32488372/12898581
Your #RequestMapping value looks ok, but you're likely should consider slightly rewriting it:
#GetMapping(value = "/{trackid}/links")
List<Link> getLinksForTrack(#PathVariable(name = "trackid") Long trackId);
Btw I did not succeeded with getting #RequestLine to work due to same error as yours.
Also for #ReactiveFeignClients Contract.Default() yields to following errors:
java.lang.IllegalStateException: Method MyClient#doStuff(String,String) not annotated with HTTP method type (ex. GET, POST)
Warnings:
- Class MyClient has annotations [Component, ReactiveFeignClient, Metadata] that are not used by contract Default
- Method doStuff has an annotation GetMapping that is not used by contract Default
and should be fixed like:
var MyClient = WebReactiveFeign.builder()
.contract(new ReactiveContract(new SpringMvcContract()))
.target(MyClient, "http://example.com")

How to manage REST API versioning with spring?

I've been searching how to manage a REST API versions using Spring 3.2.x, but I haven't find anything that is easy to maintain. I'll explain first the problem I have, and then a solution... but I do wonder if I'm re-inventing the wheel here.
I want to manage the version based on the Accept header, and for example if a request has the Accept header application/vnd.company.app-1.1+json, I want spring MVC to forward this to the method that handles this version. And since not all methods in an API change in the same release, I don't want to go to each of my controllers and change anything for a handler that hasn't changed between versions. I also don't want to have the logic to figure out which version to use in the controller themselves (using service locators) as Spring is already discovering which method to call.
So taken an API with versions 1.0, to 1.8 where a handler was introduced in version 1.0 and modified in v1.7, I would like handle this in the following way. Imagine that the code is inside a controller, and that there's some code that is able to extract the version from the header. (The following is invalid in Spring)
#RequestMapping(...)
#VersionRange(1.0,1.6)
#ResponseBody
public Object method1() {
// so something
return object;
}
#RequestMapping(...) //same Request mapping annotation
#VersionRange(1.7)
#ResponseBody
public Object method2() {
// so something
return object;
}
This is not possible in spring as the 2 methods have the same RequestMapping annotation and Spring fails to load. The idea is that the VersionRange annotation can define an open or closed version range. The first method is valid from versions 1.0 to 1.6, while the second for version 1.7 onwards (including the latest version 1.8). I know that this approach breaks if someone decides to pass version 99.99, but that's something I'm OK to live with.
Now, since the above is not possible without a serious rework of how spring works, I was thinking of tinkering with the way handlers matched to requests, in particular to write my own ProducesRequestCondition, and have the version range in there. For example
Code:
#RequestMapping(..., produces = "application/vnd.company.app-[1.0-1.6]+json)
#ResponseBody
public Object method1() {
// so something
return object;
}
#RequestMapping(..., produces = "application/vnd.company.app-[1.7-]+json)
#ResponseBody
public Object method2() {
// so something
return object;
}
In this way, I can have closed or open version ranges defined in the produces part of the annotation. I'm working on this solution now, with the problem that I still had to replace some core Spring MVC classes (RequestMappingInfoHandlerMapping, RequestMappingHandlerMapping and RequestMappingInfo), which I don't like, because it means extra work whenever I decide to upgrade to a newer version of spring.
I would appreciate any thoughts... and especially, any suggestion to do this in a simpler, easier to maintain way.
Edit
Adding a bounty. To get the bounty, please answer the question above without suggesting to have this logic in the controller themselves. Spring already has a lot of logic to select which controller method to call, and I want to piggyback on that.
Edit 2
I've shared the original POC (with some improvements) in github: https://github.com/augusto/restVersioning
Regardless whether versioning can be avoided by doing backwards compatible changes (which might not always possible when you are bound by some corporate guidelines or your API clients are implemented in a buggy way and would break even if they should not) the abstracted requirement is an interesting one:
How can I do a custom request mapping that does arbitrary evaluations of header values from the request without doing the evaluation in the method body?
As described in this SO answer you actually can have the same #RequestMapping and use a different annotation to differentiate during the actual routing that happens during runtime. To do so, you will have to:
Create a new annotation VersionRange.
Implement a RequestCondition<VersionRange>. Since you will have something like a best-match algorithm you will have to check whether methods annotated with other VersionRange values provide a better match for the current request.
Implement a VersionRangeRequestMappingHandlerMapping based on the annotation and request condition (as described in the post How to implement #RequestMapping custom properties
).
Configure spring to evaluate your VersionRangeRequestMappingHandlerMapping before using the default RequestMappingHandlerMapping (e.g. by setting its order to 0).
This wouldn't require any hacky replacements of Spring components but uses the Spring configuration and extension mechanisms so it should work even if you update your Spring version (as long as the new version supports these mechanisms).
I just created a custom solution. I'm using the #ApiVersion annotation in combination with #RequestMapping annotation inside #Controller classes.
Example:
#Controller
#RequestMapping("x")
#ApiVersion(1)
class MyController {
#RequestMapping("a")
void a() {} // maps to /v1/x/a
#RequestMapping("b")
#ApiVersion(2)
void b() {} // maps to /v2/x/b
#RequestMapping("c")
#ApiVersion({1,3})
void c() {} // maps to /v1/x/c
// and to /v3/x/c
}
Implementation:
ApiVersion.java annotation:
#Target({ElementType.METHOD, ElementType.TYPE})
#Retention(RetentionPolicy.RUNTIME)
public #interface ApiVersion {
int[] value();
}
ApiVersionRequestMappingHandlerMapping.java (this is mostly copy and paste from RequestMappingHandlerMapping):
public class ApiVersionRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
private final String prefix;
public ApiVersionRequestMappingHandlerMapping(String prefix) {
this.prefix = prefix;
}
#Override
protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
RequestMappingInfo info = super.getMappingForMethod(method, handlerType);
if(info == null) return null;
ApiVersion methodAnnotation = AnnotationUtils.findAnnotation(method, ApiVersion.class);
if(methodAnnotation != null) {
RequestCondition<?> methodCondition = getCustomMethodCondition(method);
// Concatenate our ApiVersion with the usual request mapping
info = createApiVersionInfo(methodAnnotation, methodCondition).combine(info);
} else {
ApiVersion typeAnnotation = AnnotationUtils.findAnnotation(handlerType, ApiVersion.class);
if(typeAnnotation != null) {
RequestCondition<?> typeCondition = getCustomTypeCondition(handlerType);
// Concatenate our ApiVersion with the usual request mapping
info = createApiVersionInfo(typeAnnotation, typeCondition).combine(info);
}
}
return info;
}
private RequestMappingInfo createApiVersionInfo(ApiVersion annotation, RequestCondition<?> customCondition) {
int[] values = annotation.value();
String[] patterns = new String[values.length];
for(int i=0; i<values.length; i++) {
// Build the URL prefix
patterns[i] = prefix+values[i];
}
return new RequestMappingInfo(
new PatternsRequestCondition(patterns, getUrlPathHelper(), getPathMatcher(), useSuffixPatternMatch(), useTrailingSlashMatch(), getFileExtensions()),
new RequestMethodsRequestCondition(),
new ParamsRequestCondition(),
new HeadersRequestCondition(),
new ConsumesRequestCondition(),
new ProducesRequestCondition(),
customCondition);
}
}
Injection into WebMvcConfigurationSupport:
public class WebMvcConfig extends WebMvcConfigurationSupport {
#Override
public RequestMappingHandlerMapping requestMappingHandlerMapping() {
return new ApiVersionRequestMappingHandlerMapping("v");
}
}
I have implemented a solution which handles PERFECTLY the problem with rest versioning.
General Speaking there are 3 major approaches for rest versioning:
Path-based approch, in which the client defines the version in URL:
http://localhost:9001/api/v1/user
http://localhost:9001/api/v2/user
Content-Type header, in which the client defines the version in Accept header:
http://localhost:9001/api/v1/user with
Accept: application/vnd.app-1.0+json OR application/vnd.app-2.0+json
Custom Header, in which the client defines the version in a custom header.
The problem with the first approach is that if you change the version let's say from v1 -> v2, probably you need to copy-paste the v1 resources that haven't changed to v2 path
The problem with the second approach is that some tools like http://swagger.io/ cannot distinct between operations with same path but different Content-Type (check issue https://github.com/OAI/OpenAPI-Specification/issues/146)
The solution
Since i am working a lot with rest documentation tools, i prefer to use the first approach. My solution handles the problem with the first approach, so you don't need to copy-paste the endpoint to the new version.
Let's say we have v1 and v2 versions for the User controller:
package com.mspapant.example.restVersion.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* The user controller.
*
* #author : Manos Papantonakos on 19/8/2016.
*/
#Controller
#Api(value = "user", description = "Operations about users")
public class UserController {
/**
* Return the user.
*
* #return the user
*/
#ResponseBody
#RequestMapping(method = RequestMethod.GET, value = "/api/v1/user")
#ApiOperation(value = "Returns user", notes = "Returns the user", tags = {"GET", "User"})
public String getUserV1() {
return "User V1";
}
/**
* Return the user.
*
* #return the user
*/
#ResponseBody
#RequestMapping(method = RequestMethod.GET, value = "/api/v2/user")
#ApiOperation(value = "Returns user", notes = "Returns the user", tags = {"GET", "User"})
public String getUserV2() {
return "User V2";
}
}
The requirement is if i request the v1 for the user resource i have to take the "User V1" repsonse, otherwise if i request the v2, v3 and so on i have to take the "User V2" response.
In order to implement this in spring, we need to override the default RequestMappingHandlerMapping behavior:
package com.mspapant.example.restVersion.conf.mapping;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
public class VersionRequestMappingHandlerMapping extends RequestMappingHandlerMapping {
#Value("${server.apiContext}")
private String apiContext;
#Value("${server.versionContext}")
private String versionContext;
#Override
protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletRequest request) throws Exception {
HandlerMethod method = super.lookupHandlerMethod(lookupPath, request);
if (method == null && lookupPath.contains(getApiAndVersionContext())) {
String afterAPIURL = lookupPath.substring(lookupPath.indexOf(getApiAndVersionContext()) + getApiAndVersionContext().length());
String version = afterAPIURL.substring(0, afterAPIURL.indexOf("/"));
String path = afterAPIURL.substring(version.length() + 1);
int previousVersion = getPreviousVersion(version);
if (previousVersion != 0) {
lookupPath = getApiAndVersionContext() + previousVersion + "/" + path;
final String lookupFinal = lookupPath;
return lookupHandlerMethod(lookupPath, new HttpServletRequestWrapper(request) {
#Override
public String getRequestURI() {
return lookupFinal;
}
#Override
public String getServletPath() {
return lookupFinal;
}});
}
}
return method;
}
private String getApiAndVersionContext() {
return "/" + apiContext + "/" + versionContext;
}
private int getPreviousVersion(final String version) {
return new Integer(version) - 1 ;
}
}
The implementation reads the version in the URL and asks from spring to resolve the URL .In case this URL does not exists (for example the client requested v3) then we try with v2 and so one until we find the most recent version for the resource.
In order to see the benefits from this implementation, let's say we have two resources: User and Company:
http://localhost:9001/api/v{version}/user
http://localhost:9001/api/v{version}/company
Let's say we made a change in company "contract" that breaks the client. So we implement the http://localhost:9001/api/v2/company and we ask from client to change to v2 instead on v1.
So the new requests from client are:
http://localhost:9001/api/v2/user
http://localhost:9001/api/v2/company
instead of:
http://localhost:9001/api/v1/user
http://localhost:9001/api/v1/company
The best part here is that with this solution the client will get the user information from v1 and company information from v2 without the need to create a new (same) endpoint from user v2!
Rest Documentation
As i said before the reason i select the URL-based versioning approach is that some tools like swagger do not document differently the endpoints with the same URL but different content type. With this solution, both endpoints are displayed since have different URL:
GIT
Solution implementation at:
https://github.com/mspapant/restVersioningExample/
I would still recommend using URL's for versioning because in URLs #RequestMapping supports patterns and path parameters, which format could be specified with regexp.
And to handle client upgrades (which you mentioned in comment) you can use aliases like 'latest'. Or have unversioned version of api which uses latest version (yeah).
Also using path parameters you can implement any complex version handling logic, and if you already want to have ranges, you very well might want something more soon enough.
Here is a couple of examples:
#RequestMapping({
"/**/public_api/1.1/method",
"/**/public_api/1.2/method",
})
public void method1(){
}
#RequestMapping({
"/**/public_api/1.3/method"
"/**/public_api/latest/method"
"/**/public_api/method"
})
public void method2(){
}
#RequestMapping({
"/**/public_api/1.4/method"
"/**/public_api/beta/method"
})
public void method2(){
}
//handles all 1.* requests
#RequestMapping({
"/**/public_api/{version:1\\.\\d+}/method"
})
public void methodManual1(#PathVariable("version") String version){
}
//handles 1.0-1.6 range, but somewhat ugly
#RequestMapping({
"/**/public_api/{version:1\\.[0123456]?}/method"
})
public void methodManual1(#PathVariable("version") String version){
}
//fully manual version handling
#RequestMapping({
"/**/public_api/{version}/method"
})
public void methodManual2(#PathVariable("version") String version){
int[] versionParts = getVersionParts(version);
//manual handling of versions
}
public int[] getVersionParts(String version){
try{
String[] versionParts = version.split("\\.");
int[] result = new int[versionParts.length];
for(int i=0;i<versionParts.length;i++){
result[i] = Integer.parseInt(versionParts[i]);
}
return result;
}catch (Exception ex) {
return null;
}
}
Based on the last approach you can actually implement something like what you want.
For example you can have a controller that contains only method stabs with version handling.
In that handling you look (using reflection/AOP/code generation libraries) in some spring service/component or in the same class for method with the same name/signature and required #VersionRange and invoke it passing all parameters.
The #RequestMapping annotation supports a headers element that allows you to narrow the matching requests. In particular you can use the Accept header here.
#RequestMapping(headers = {
"Accept=application/vnd.company.app-1.0+json",
"Accept=application/vnd.company.app-1.1+json"
})
This isn't exactly what you're describing, since it doesn't directly handle ranges, but the element does support the * wildcard as well as !=. So at least you could get away with using a wildcard for cases where all versions support the endpoint in question, or even all minor versions of a given major version (e.g. 1.*).
I don't think I've actually used this element before (if I have I don't remember), so I'm just going off the documentation at
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html
I already tried to version my API using the URI Versioning, like:
/api/v1/orders
/api/v2/orders
But there are some challenges when trying to make this work: how organize your code with different versions? How manage two (or more) versions at the same time? What's the impact when removing some version?
The best alternative that I found was not version the entire API, but control the version on each endpoint. This pattern is called Versioning using Accept header or Versioning through content negotiation:
This approach allows us to version a single resource representation
instead of versioning the entire API which gives us a more granular
control over versioning. It also creates a smaller footprint in the
code base as we don’t have to fork the entire application when
creating a new version. Another advantage of this approach is that it
doesn’t require implementing URI routing rules introduced by
versioning through the URI path.
Implementation on Spring
First, you create a Controller with a produces attribute, that will applied by default on each endpoint inside the same class.
#RestController
#RequestMapping(value = "/api/orders/", produces = "application/vnd.company.etc.v1+json")
public class OrderController {
}
After that, we can imagine a possible scenario where you have two versions (v1 and v2) of an endpoint for "create an order":
#Deprecated
#PostMapping
public ResponseEntity<OrderResponse> createV1(
#RequestBody OrderRequest orderRequest) {
OrderResponse response = createOrderService.createOrder(orderRequest);
return new ResponseEntity<>(response, HttpStatus.CREATED);
}
#PostMapping(
produces = "application/vnd.company.etc.v2+json",
consumes = "application/vnd.company.etc.v2+json")
public ResponseEntity<OrderResponseV2> createV2(
#RequestBody OrderRequestV2 orderRequest) {
OrderResponse response = createOrderService.createOrder(orderRequest);
return new ResponseEntity<>(response, HttpStatus.CREATED);
}
Done! Just call each endpoint using the desired Http Header version:
Content-Type: application/vnd.company.etc.v1+json
Or, to call the v2:
Content-Type: application/vnd.company.etc.v2+json
About your worries:
And since not all methods in an API change in the same release, I
don't want to go to each of my controllers and change anything for a
handler that hasn't changed between versions
As explained, this strategy maintains each Controller and endpoint with his actual version. You only modify the endpoint that have modifications and needs a new version.
And the Swagger?
Setup the Swagger with different versions is also very easy using this strategy. See this answer to more details.
What about just using inheritance to model versioning? That is what I'm using in my project and it requires no special spring configuration and gets me exactly what I want.
#RestController
#RequestMapping(value = "/test/1")
#Deprecated
public class Test1 {
...Fields Getters Setters...
#RequestMapping(method = RequestMethod.GET)
#Deprecated
public Test getTest(Long id) {
return serviceClass.getTestById(id);
}
#RequestMapping(method = RequestMethod.PUT)
public Test getTest(Test test) {
return serviceClass.updateTest(test);
}
}
#RestController
#RequestMapping(value = "/test/2")
public class Test2 extends Test1 {
...Fields Getters Setters...
#Override
#RequestMapping(method = RequestMethod.GET)
public Test getTest(Long id) {
return serviceClass.getAUpdated(id);
}
#RequestMapping(method = RequestMethod.DELETE)
public Test deleteTest(Long id) {
return serviceClass.deleteTestById(id);
}
}
This set up allows for little duplication of code and the ability to overwrite methods into new versions of the api with little work. It also saves the need to complicate your source code with version switching logic. If you don't code an endpoint in a version it will grab the previous version by default.
Compared to what others are doing this seems way easier. Is there something I'm missing?
In produces you can have negation. So for method1 say produces="!...1.7" and in method2 have the positive.
The produces is also an array so you for method1 you can say produces={"...1.6","!...1.7","...1.8"} etc (accept all except 1.7)
Ofcourse not as ideal as ranges that you have in mind but I think easier to maintain than other custom stuff if this is something uncommon in your system. Good luck!
You can use AOP, around interception
Consider having a request mapping which receives all the /**/public_api/* and in this method do nothing;
#RequestMapping({
"/**/public_api/*"
})
public void method2(Model model){
}
After
#Override
public void around(Method method, Object[] args, Object target)
throws Throwable {
// look for the requested version from model parameter, call it desired range
// check the target object for #VersionRange annotation with reflection and acquire version ranges, call the function if it is in the desired range
}
The only constraint is that all has to be in the same controller.
For AOP configuration have a look at http://www.mkyong.com/spring/spring-aop-examples-advice/

Categories

Resources