I found a piece of code that I wanted to implement in my application. The code works fine, but has Deprecated methods. The code I'm talking about is:
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
PageableHandlerMethodArgumentResolver resolver = new PageableHandlerMethodArgumentResolver();
resolver.setFallbackPageable(new PageRequest(0, 20));
argumentResolvers.add(resolver);
super.addArgumentResolvers(argumentResolvers);
}
}
And specifically the Deprecated class is WebMvcConfigurerAdapter and it's method addArgumentResolvers. According to documentation:
as of 5.0 WebMvcConfigurer has default methods (made possible by a Java 8 baseline) and can be implemented directly without the need for this adapter
And so I've replaced the deprecated class with a interface - WebMvcConfigurer. Problem is the interface won't allow me to use addArgumentResolvers method. What do I do now? Should I create an instance of the interface? Are there any drawbacks to using a deprecated methods?
Drawbacks to use deprecated methods is that when you'll migrate to the newer version of library, the method may be gone completely and you'll have to rewrite your code. Another thing is that usually there are reasons for library developers to deprecate methods - either they are ineffective, or lead to bad design, or (most common) have analogs.
WebMvcConfigurer according to Docs has default method addArgumentResolvers(java.util.List<HandlerMethodArgumentResolver> resolvers). If you can't use it, check that your project supports Java 8.
Example how it works:
DefaultMethods.java
public interface DefaultMethods { default void example(){
System.out.println("default method call"); } }
DefalutMethodsTest.java
public class DefalutMethodsTest implements DefaultMethods{
public static void main(String[] args) {
new DefalutMethodsTest().example();
}
@Override
public void example() {
System.out.println("implementer call");
DefaultMethods.super.example();
}
}
Output:
implementer call
default method call
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With