I will be in charge of the project that realizes API ⇔ API cooperation processing with external services. Make a note of the various research / implementation of the title.
The person in charge on the external service side said the following.
In response to the above, when I was designing the API on this side, I thought about the following.
If you do the above, I wonder if it will meet the demands of the external service first. Even here, it's easier to return what you return first. The code image is below.
SampleController.java
@RestController
public class SampleController() {
//Accepting requests from external services
@RequestMapping(value = "/sample")
public String sample(@Validated SampleForm form) {
//Minimal processing to return a response
return "";
}
//Post-processing(Image: If you read this article, it will not be implemented like this)
private void after() {
//Processing required other than returning a response
}
}
After investigating, it seems that it can be realized by HandlerInterceptor # afterCompletion ()
.
I referred to the following article.
Understanding how to implement common request processing on Spring MVC (+ Spring Boot)
HandlerInterceptor
is an interface that also has methods for purposes other than post-processing,
This time, I want to implement only the post-processing individually, so
Prepare a class that inherits HandlerInterceptorAdapter
, which is an implementation class of HandlerInterceptor
.
Also, from the perspective of post-processing, HandlerInterceptor # postHandle ()
is also a candidate,
This is excluded because it does not meet our requirements as it will not be called when an exception occurs.
HandlerInterceptor # afterCompletion ()
is called whether it ends normally or an exception occurs.
SampleInterceptor.java
public class SampleInterceptor extends HandlerInterceptorAdapter {
//At the end of normal/AfterCompletion because I want to implement only post-processing regardless of abnormal termination()Only override
@override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex) throws Exception {
//Post-processing implementation
}
}
Next, our requirement is to apply post-processing only to requests for / sample
in the above code image.
This can be addressed with the following implementation, which is also mentioned in the article above.
WebMvcConfig.java
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
//I have to register the bean
@Bean
public SampleInterceptor sampleInterceptor() {
return new SampleInterceptor();
}
@Bean
public HogeInterceptor hogeInterceptor() {
return new HogeInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(sampleInterceptor())
.addPathPatterns("/sample"); //The target is specified here
//If you want to specify multiple Interceptors, write them side by side. You can also chain and specify multiple paths
registry.addInterceptor(hogeInterceptor())
.addPathPatterns("/hoge").addPathPatterns("/huga")
//If you specify the same path in different Interceptors, both afterCompletions()Is executed
//It has not been investigated how the execution order is decided
.addPathPatterns("/sample");
}
}
With the above, the required processing has been realized. If you don't specify a target with ʻaddPathPatterns`, all Controllers seem to be subject to post-processing.
The actual code is not limited to the sample code above, but there are a few more. That's all I want to write down.
Recommended Posts