Tuesday, August 18, 2015

Migrating Spring Rest Service Application to Spring Boot (Part II)

As I mentioned in my previous post, things were not going smoothly. I got a weird java.lang.reflect.InvocationTargetException which was hard to explain. After some Googling I found an explanation.  Turns out the culprit was @Repository annotation in the Repository interfaces which is not needed there. Removing the annotation fixed the issue.

I ran the application and I was able to make REST calls  at localhost:8080/resturl. VICTORY!!!

Wait... Not so fast. I need to add v1/ in front resturl for versioning so the call would be like localhost:8080/v1/resturl .

Hmm. For that I needed to register DispatcherServlet and define the mapping:

@Bean
public DispatcherServlet dispatcherServlet() {
return new DispatcherServlet();
}
@Bean
public ServletRegistrationBean dispatchcherServletRegistration(){
ServletRegistrationBean registrationBean =new ServletRegistrationBean(dispatcherServlet(), "/rest/v1/*");
registrationBean
.setName(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME );
return registrationBean;
}
view raw gistfile1.txt hosted with ❤ by GitHub
I also needed to add a custom filter for Dispatcher Servlet. That was easy:
@Bean
public CustomFilter customFilter(){
return new CustomFilter();
}
@Bean
public FilterRegistrationBean customFilterRegistration(){
FilterRegistrationBean customFilterRegistration=new FilterRegistrationBean(metricsFilter(), dispatcherServletRegistration());
return customFilterRegistration;
}
view raw registerFilter hosted with ❤ by GitHub
Now I am ready to deploy. But how do I that?

No comments:

Post a Comment