Difference between @Mock, @MockBean and Mockito.mock()

Plain Mockito library import org.mockito.Mock; … @Mock MyService myservice; and import org.mockito.Mockito; … MyService myservice = Mockito.mock(MyService.class); come from the Mockito library and are functionally equivalent. They allow to mock a class or an interface and to record and verify behaviors on it. The way using annotation is shorter, so preferable and often preferred. Note … Read more

Combining Spring project and Jersey

NOTE: The linked example projects below are from the Jersey master branch, which is currently a snapshot of Jersey 3, which is not yet released. Jersey 3 will be using Spring 4, so you may notice a dependency jersey-spring4. This dependency does not exist yet, as Jersey 3 is not yet released (probably not for … Read more

How to inject dependencies into a self-instantiated object in Spring?

You can do this using the autowireBean() method of AutowireCapableBeanFactory. You pass it an arbitrary object, and Spring will treat it like something it created itself, and will apply the various autowiring bits and pieces. To get hold of the AutowireCapableBeanFactory, just autowire that: private @Autowired AutowireCapableBeanFactory beanFactory; public void doStuff() { MyBean obj = … Read more

ContextLoaderListener or not?

In your case, no, there’s no reason to keep the ContextLoaderListener and applicationContext.xml. If your app works fine with just the servlet’s context, that stick with that, it’s simpler. Yes, the generally-encouraged pattern is to keep non-web stuff in the webapp-level context, but it’s nothing more than a weak convention. The only compelling reasons to … Read more

Angular2/Spring Boot Allow Cross Origin on PUT

Create CORSFilter.java file in your project. @Component @Order(Ordered.HIGHEST_PRECEDENCE) public class CORSFilter implements Filter { /** * CORS filter for http-request and response */ public CORSFilter() { } /** * Do Filter on every http-request. */ @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletResponse response = (HttpServletResponse) res; HttpServletRequest request … Read more

Bypass GeneratedValue in Hibernate (merge data not in db?)

Another implementation, way simpler. This one works with both annotation-based or xml-based configuration: it rely on hibernate meta-data to get the id value for the object. Replace SequenceGenerator by IdentityGenerator (or any other generator) depending on your configuration. (The creation of a decorator instead of subclassing, passing the decorated ID generator as a parameter to … Read more

Unable to get spring boot to automatically create database schema

There are several possible causes: Your entity classes are in the same or in a sub-package relative one where you have you class with @EnableAutoConfiguration. If not then your spring app does not see them and hence will not create anything in db Check your config, it seems that you are using some hibernate specific … Read more

Spring security CORS Filter

Since Spring Security 4.1, this is the proper way to make Spring Security support CORS (also needed in Spring Boot 1.4/1.5): @Configuration public class WebConfig extends WebMvcConfigurerAdapter { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping(“/**”) .allowedMethods(“HEAD”, “GET”, “PUT”, “POST”, “DELETE”, “PATCH”); } } and: @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity … Read more

Spring Resttemplate exception handling

You want to create a class that implements ResponseErrorHandler and then use an instance of it to set the error handling of your rest template: public class MyErrorHandler implements ResponseErrorHandler { @Override public void handleError(ClientHttpResponse response) throws IOException { // your error handling here } @Override public boolean hasError(ClientHttpResponse response) throws IOException { … } … Read more