@DateTimeFormat usage example

@GetMapping("/find") public Page<ReportProjection> reports( @RequestParam(required = false, name = "status") ReportStatus status, @RequestParam(required = false, name = "search") String search, @DateTimeFormat(pattern="yyyy-MM-dd") @RequestParam(required = false, name = "from") Date from, @DateTimeFormat(pattern="yyyy-MM-dd") @RequestParam(required = false, name = "to") Date to, Pageable pageable) { return reportService.reports(status, search, from, to, pageable); }@GetMapping("/find") public Page<ReportProjection> reports( @RequestParam(required = false, name Read more about @DateTimeFormat usage example[…]

@PreAuthorize and SpEL params

If @PreAuthorize is used on interface, parameter name (id) in SpEL must match parameter name in implementation (not required on interface – fileId). @Transactional public interface FileService {   @PreAuthorize("@securityService.canAccessFile(#id)") File getFile(Long fileId); }   /** * GOOD */ @Service public class FileServiceImpl implements FileService {   @Override public File getFile(Long id) { /* … Read more about @PreAuthorize and SpEL params[…]

Native query with Enum as parameter

public interface ItemRepository extends JpaRepository<Item, Long> { @Query(value = "select * from items where type = :#{#type?.name()}", nativeQuery = true) List<Item> findByType(@Param("type") ItemType type); }   public enum ItemType { NORMAL, LARGE };public interface ItemRepository extends JpaRepository<Item, Long> { @Query(value = "select * from items where type = :#{#type?.name()}", nativeQuery = true) List<Item> findByType(@Param("type") ItemType Read more about Native query with Enum as parameter[…]

Optional @Value

// default: 60 @Value("${test-value:60}") private int minutes;   // default: null @Value("${test-value2:#{null}}") private String text;// default: 60 @Value("${test-value:60}") private int minutes; // default: null @Value("${test-value2:#{null}}") private String text;

SpEL in @Query (Spring Data)

@Query("select t from Test t where name = :#{#data.name} and type = :#{#data.type}") List<User> findByData(@Param("data") Data data);   public class Data { private String name; private DataType type; }@Query("select t from Test t where name = :#{#data.name} and type = :#{#data.type}") List<User> findByData(@Param("data") Data data); public class Data { private String name; private DataType type; Read more about SpEL in @Query (Spring Data)[…]

Spring Data JPA: LIKE with OR query

@RepositoryRestResource public interface ProductRepository extends JpaRepository<Product, String> { List<Product> findByTitleContainingIgnoreCaseOrIdContainingIgnoreCase( @Param("q") String name, @Param("q") String id); }@RepositoryRestResource public interface ProductRepository extends JpaRepository<Product, String> { List<Product> findByTitleContainingIgnoreCaseOrIdContainingIgnoreCase( @Param("q") String name, @Param("q") String id); } Request path: GET https://localhost:8443/api/products/search/findByTitleContainingIgnoreCaseOrIdContainingIgnoreCase?q=…GET https://localhost:8443/api/products/search/findByTitleContainingIgnoreCaseOrIdContainingIgnoreCase?q=… If we need shorter request path: // GET https://localhost:8443/api/products/search/custom?q=… @RestResource(path = "custom") List<Product> findByTitleContainingIgnoreCaseOrIdContainingIgnoreCase( @Param("q") String name, Read more about Spring Data JPA: LIKE with OR query[…]

Active Directory/LDAP authentication with Spring Boot

package net.marioosh.ldapdemo;   import java.util.Arrays; import java.util.Collection;   import org.springframework.context.annotation.Configuration; import org.springframework.ldap.core.DirContextOperations; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.ldap.userdetails.LdapAuthoritiesPopulator;   @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter {   @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .anyRequest() .fullyAuthenticated() .and().formLogin(); }   @Override public void configure(AuthenticationManagerBuilder auth) throws Exception { Read more about Active Directory/LDAP authentication with Spring Boot[…]

@PreAuthorize with use Spring Expression Language (SpEL) and service method call

public interface EventRepository extends JpaRepository<Event, Long> {   @PreAuthorize("@securityService.canSave(#event,principal)") Event save(@Param("event") Event event);   @PreAuthorize("@securityService.canSave2(#event)") Event save2(@Param("event") Event event); }   @Service("securityService") public class SecurityService {   public boolean canSave(Event event, UserDetails principal) { // some logic here }   public boolean canSave2(Event event) {   // get principal from Security Context Object principal = Read more about @PreAuthorize with use Spring Expression Language (SpEL) and service method call[…]

JSON -> List

import org.springframework.core.io.Resource; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper;   …   @Value("classpath:/strings.json") private Resource jsonFile;   @Autowired private ObjectMapper mapper;   …   List<String> listOfStrings = Arrays.asList( mapper.readValue(jsonFile.getInputStream(), String[].class) );import org.springframework.core.io.Resource; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; … @Value("classpath:/strings.json") private Resource jsonFile; @Autowired private ObjectMapper mapper; … List<String> listOfStrings = Arrays.asList( mapper.readValue(jsonFile.getInputStream(), String[].class) );