Disable @EnableScheduling on Spring Tests

If you don’t want to use profiles, you can add flag that will enable/disable scheduling for the application In your AppConfiguration add this @ConditionalOnProperty( value = “app.scheduling.enable”, havingValue = “true”, matchIfMissing = true ) @Configuration @EnableScheduling public static class SchedulingConfiguration { } and in your test just add this annotation to disable scheduling @TestPropertySource(properties = … Read more

Prevent Spring Boot from registering a servlet filter

By default Spring Boot creates a FilterRegistrationBean for every Filter in the application context for which a FilterRegistrationBean doesn’t already exist. This allows you to take control of the registration process, including disabling registration, by declaring your own FilterRegistrationBean for the Filter. For your PreAuthenticationFilter the required configuration would look like this: @Bean public FilterRegistrationBean … Read more

Why order of Maven dependencies matter?

The order of dependencies does matter because of how Maven resolves transitive dependencies, starting with version 2.0.9. Excerpt from the documentation: (…) this determines what version of a dependency will be used when multiple versions of an artifact are encountered. (…) You can always guarantee a version by declaring it explicitly in your project’s POM. … Read more

How to work with DTO in Spring Data REST projects?

An approach of how to work with DTO in Spring Data REST projects The working example is here Entities Entities must implement the Identifiable interface. For example: @Entity public class Category implements Identifiable<Integer> { @Id @GeneratedValue private final Integer id; private final String name; @OneToMany private final Set<Product> products = new HashSet<>(); // skipped } … Read more

Converting from String to custom Object for Spring MVC form Data binding?

Just as a supplement to Mark’s answer, here is what I ended up doing in my controller. @Override protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception { binder.registerCustomEditor(Server.class, “serverId”, new PropertyEditorSupport() { @Override public void setAsText(String text) { Server type = (Server) em.createNamedQuery(“Server.findById”) .setParameter(“id”, Short.parseShort(text)).getSingleResult(); setValue(type); } }); } You can also do this using … Read more

Where is the @Autowired annotation supposed to go – on the property or the method?

According to the Javadoc for Autowired, the annotation can be used on “a constructor, field, setter method or config method”. See the full documentation for more details. I personally prefer your first option (constructor injection), because the myDao field can be marked as final: @Controller public class MyControllear { private final MyDao myDao; @Autowired public … Read more

Can Spring Data REST’s QueryDSL integration be used to perform more complex queries?

I think you should be able to get this to work using the following customization: bindings.bind(user.dateOfBirth).all((path, value) -> { Iterator<? extends LocalDate> it = value.iterator(); return path.between(it.next(), it.next()); }); The key here is to use ?dateOfBirth=…&dateOfBirth= (use the property twice) and the ….all(…) binding which will give you access to all values provided. Make sure … Read more

Auto login after successful registration

This can be done with spring security in the following manner(semi-psuedocode): import org.springframework.security.web.savedrequest.RequestCache; import org.springframework.security.web.savedrequest.SavedRequest; @Controller public class SignupController { @Autowired RequestCache requestCache; @Autowired protected AuthenticationManager authenticationManager; @RequestMapping(value = “/account/signup/”, method = RequestMethod.POST) public String createNewUser(@ModelAttribute(“user”) User user, BindingResult result, HttpServletRequest request, HttpServletResponse response) { //After successfully Creating user authenticateUserAndSetSession(user, request); return “redirect:/home/”; } private … Read more