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

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.

Related

SpringMVC global setting for ignoring unknown properties during deserialization

Spring Boot sets "spring.jackson.deserialization.fail-on-unknown-properties=false" by default. I have a library that works fine in Spring Boot, but when used in an existing SpringMVC app it throws "Unrecognized field, not marked as ignorable". Is there some comparable global setting for SpringMVC I can set in the config or otherwise?
edit: spring webmvc version 3.2.15.RELEASE
You can annotate the mapped classes with
#JsonIgnoreProperties(ignoreUnknown = true)
or create add the following configuration to the ObjectMapper as follows:
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
You can follow two method that I have mention in this answer. If I'm not wrong either one will work for you. (But method 1 won't work if your clinet class does not have a no-arg default constructor)

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

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.

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.

Why do we need #Component spring annotation for Jersey resource in spring-boot-starter-jersey project?

This question is regarding the sample:
https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/spring-boot-sample-jersey/src/main/java/sample/jersey/Endpoint.java
Why do we need "#Component" annotation for Jersey resource when using spring-boot -starter-jersey project?
If I remove it, the Jersey servlet can still serve resources.
So what is the need for "#Component"?
You don't need it. Jersey uses HK2 as it's internal DI framework, and HK2 has a Spring bridge. This is what's used internally to bridge Spring components into the HK2 IoC container, so that they can be injected into Jersey components. And Jersey implements an AutowiredInjectionResolver1 that allows for injection of Spring components using #Autowired. You don't even need #Autowired though. All the Spring components can be injected with the normal #Inject.
The only drawback I've ran into, not making the Jersey components a Spring #Component is that it doesn't support #Value when you want to inject property values.
The one thing I don't like is that when you declare something a Spring #Component, it automatically makes it a singleton. But Jersey resources are by default request scoped. You can add a Spring #Scope("request"), and it should change the resource to a request scoped resource. Jersey has declared the Spring RequestScope, so we can use it. How exactly it ties in to Jersey's request scope, I am not a hundred percent sure. I ran into a problem a while back. I can't remember what it was, but that has kept me from ever using the Spring request scope again.
Assuming I want to keep all my resources request scoped, I would take sticking to the normal Jersey request scope, and not being able to inject #Values, over having to use Spring's request scope. Maybe I'm imagining things, and there was no issue using it, but personally I'll just stick to what I know works :-)
UPDATE
Another thing that does't work if you don't make the resource a Spring #Component is Spring's AOP. That's fine with me though as HK2 also has AOP.
1 - An InjectionResolver allows you to use custom annotations to create injection targets.
When you remove #Component jersey takes control of the scope of the instance. With #Component a singleton instance is created, removing it you can use the following jersey annotations:
• Request scope (Default):
By using the #RequestScope annotation or none, we can have a life-cycle till
the request lasts. This is the default scope of the root-resource classes. For
each new request, a new root-resource instance is being created and served
accordingly for the first time. However, when the same root-resource method
is being called, then the old instance will be used to serve the request.
• Per-lookup scope:
The #PerLookup annotation creates root-resource instances for every request.
• Singleton:
The #Singleton annotation allows us to create only a single instance
throughout the application.
Try different behaviors using a counter inside your class...
public class MyWebResource {
private int counter;
#GET
#Path("/counter")
#Produces(MediaType.APPLICATION_JSON)
public Response getCounter() {
counter++;
return Response.status(Status.OK).entity(counter).build();
}
}

Spring 3.2 and #WebFilter annontation..is it supported?

I can't seem to find anything that talks about using the #WebFilter annotation, and having Spring 3.2 AbstractAnnotationConfigDispatcherServletInitializer correctly handle the params used.
Does Spring 3.2 support that annotation?
EDIT: based upon nicohlas answer
If I use the #WebFilter annontation, and set params inside of that...it looks like AbstractAnnotationConfigDispatcherServletInitializer registerServletFilter does not look at those annontations and setup things properly from those params: e.g url-mappings
Knowing that Spring 3.2 supports Servlet 3.0, shouldn't it support #WebFilter?
or am i missing the whole point of the WebFilter annontation?
EDIT 2:
When I deploy my war, and have #WebFilters defined inside that war, the container looks to "register" those...but, for some reason, even with the proper urlPatterns set, when the request comes into the DispatchServlet...it's like the filter doesn't get called.
#WebFilter is a JEE6 component, and is new to Servlet 3.0.
It is not something that Spring would support, but rather your application container (Tomcat, WebSphere, Glassfish, JBoss,...)
The idea here is to move configuration of your application container to Java, rather than utilizing the web.xml deployment descriptor.
EDIT:
It would seem to me that using the #WebFilter annotation would be for discovering Filter's via classpath scanning. The AbstractAnnotationConfigDispatcherServletInitializer#registerServletFilter method that you are asking about is taking in a Filter and does not look at the annotations on it.

Resources