Spring Boot / MVC - How to customize handling of return types for controller methods? - spring-boot

Spring Boot 2.3
I would like to return Kotlin's Result type from a Spring Boot controller method. This type can either be an exception or a success. If it is an exception, I would like to extract this and allow it to flow to the normal ExceptionHandler. If it is a success, then I want to extract this and allow it to flow out as if it had been returned from the controller method.
I am having trouble finding information on how to add additional controller result type handlers in Spring Boot. Any help that can be provided toward this would be much appreciated.

Related

Spring Boot Best approach for validation in facade

I use Spring Boot with Kotlin and my project is splitted on modules where I have application layer with REST controllers and facades, domain layer with services and business logic and infrastructures layer for communication with external services or DBs. I think (but I can be wrong) that the best place for basic validation like notNull, notEmpty, max and min, size etc. is facade because both REST controllers and another modules communicate with it, but the problem is that javax validation annotation like #Valid are working only on REST Controller layer (in classes with annotation #RestController) and when I try to create some tests for this facade then fields with wrong values return NullPointerException instead of MethodArgumentNotValidException. I tried to create WebMvcTest but also returns wrong exception. Is it some solution for it? I saw that I could call some validator inside the method but it looks like much more complex approach than annotation on method's argument.
You can inject the javax.validation.Validator into any Spring component. For example, into a facade component, and perform validation in the facade layer:
#Autowired
Validator validator;
Set<ConstraintViolation<UserRequestDTO>> violations = validator.validate(userRequest);
This way you can remove the #Valid annotation from controllers at all. And this approach puts validation results under the your control.

Using #ConfigurationProperties statically - such as on #RequestMapping

Let's ignore for a moment whether doing this is a great idea, but I'm creating Spring Boot AutoConfiguration for an internal library and as part of this I want to auto-register a Controller that accepts GET/POST/DELETE requests (it is responsible for setting/clearing a cookie value for application testing purposes)
The issue is that I would like the request mapping path to be configurable by the end user. I have a #ConfigurationProperties(prefix = "my.configs") class that contains all the configuration values with their defaults for example: private String path = "default-path"
Ideally i'd be able to reference this in my controller like so: #RequestMapping(path=${my.configs.path}) but this does not work, Spring reports that it is unable to find that configuration parameter, if I place it into a properties file instead of into a the type-safe #ConfigurationProperties it works as expected.
I know I could get around this by putting a default value into the Request mapping, but I'd like to understand just what is happening here, and why I cannot statically refer environment variables read / defaulted into #ConfigurationProperties in the way that I can those defined in files.
#RequestMapping is a Spring MVC annotation and it gets processed by Spring MVC - no matter if it is all wrapped in Spring Boot app or not.
#ConfiguationProperties is on the other hand 100% Spring Boot code and to my knowledge both types of properties are processed at different moments during Spring Context startup lifecycle.

Does a Spring controller returning a ListenableFuture needs #AsynchEnable in configuration?

As of spring 4.1, spring controllers accept return value that can be of type ListenableFuture. is returning a ListenableFuture return value sufficient in making the controller async? Or does it also need #enableAsync annotation somewhere in spring configuration file or/and anything else? I am following this tutorial
I found out that what i was looking for is not #enableAsync but a servlet 3.0 property called async-supported. According to this link, spring-boot defaults async-supported to true.
Hence, there is no need of any further configuration to do if you're using spring-boot.

Make Api available as Restful Service

I am trying to make our apis available as restful service.
Defined a controller with #Controller and inside defined a method as follows
#RequestMapping(value="/empDetails/{empName}", method=RequestMethod.GET)
public void getUserData(#PathVariable("empName") String empName, Model model) {
}
Does the above makes the api as Restful ???
In Simple just giving annotation makes the method as Restful API???
Thanks.
You make it RESTFul API by returning an object, usually a JSON, rather than using MVC.
For your own convenience you could use #RestController instead of #Controller.
You can learn about the differences here.
Just an advice, depending on your snippet, when you use GET methods, return a JSON rather than void.
You can see a spring REST example (ignore the spring boot part).
And you could also see a spring MVC example
I hope the differences will give you a clear point of view

Automatic invocation of Bean Validator for #Valid outside of Spring MVC

Simple scenario:-
In a Spring 4 app the following #Valid gets triggered when I make a REST call but not when I make an API call. The docs talks about it but does not state how to do it the way I want.
#RequestMapping(name="/something")
public Entity save(#Valid Entity e){ return repo.save(e);}
Without writing any custom AOP code to achieve this myself etc. how do I get bean validation #Valid triggered in Spring ? Looking for any Spring or Hibernate configuration that will turn this on for simple API calls.
Example: When I bundle my component as a maven I would like to get my beans validated irrespective of whether they run in a web context or outside , say Spring Batch or just JUnit Tests.
And when I bundle this jar in a web app I definitely don't want this executed twice.

Resources