Convert Unix timestamp to Java Date, Spring RequestParam

Since timestamps aren’t a formatted date (going by Java’s SimpleDateFormat options), but more a numeric value: I would recommend making a custom data-binder for Date objects if you’re doing this more often than this single instance. See http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#portlet-ann-webdatabinder As a one-off solution you can bind them to Long parameters and create your own Date object … Read more

Is it possible to dynamically set RequestMappings in Spring MVC?

Spring MVC performs URL mappings using implementations of the HandlerMapping interface. The ones usually used out of the box are the default implementations, namely SimpleUrlHandlerMapping, BeanNameUrlHandlerMapping and DefaultAnnotationHandlerMapping. If you want to implement your own mapping mechanism, this is fairly easy to do – just implement that interface (or, perhaps more likely, extend AbstractUrlHandlerMapping), declare … Read more

How to convert byte array to MultipartFile

org.springframework.web.multipart.MultipartFile is an interface so firstly you are going to need to work with an implementation of this interface. The only implementation that I can see for that interface that you can use out-of-the-box is org.springframework.web.multipart.commons.CommonsMultipartFile. The API for that implementation can be found here Alternatively as org.springframework.web.multipart.MultipartFile is an interface, you could provide your … Read more

Load different application.yml in SpringBoot Test

One option is to work with profiles. Create a file called application-test.yml, move all properties you need for those tests to that file and then add the @ActiveProfiles annotation to your test class: @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = Application.class) @WebAppConfiguration @IntegrationTest @ActiveProfiles(“test”) // Like this public class MyIntTest{ } Be aware, it will additionally load the application-test.yml, … Read more

How to configure Spring Security to allow Swagger URL to be accessed without authentication

Adding this to your WebSecurityConfiguration class should do the trick. @Configuration public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter { @Override public void configure(WebSecurity web) throws Exception { web.ignoring().antMatchers(“/v2/api-docs”, “/configuration/ui”, “/swagger-resources/**”, “/configuration/security”, “/swagger-ui.html”, “/webjars/**”); } }

Can Spring MVC handle multivalue query parameter?

“Arrays” in @RequestParam are used for binding several parameters of the same name: phone=val1&phone=val2&phone=val3 – public String method(@RequestParam(value=”phone”) String[] phoneArray){ …. } You can then convert it into a list using Arrays.asList(..) method EDIT1: As suggested by emdadul, latest version of spring can do like below as well: public String method(@RequestParam(value=”phone”, required=false) List<String> phones){ …. … Read more

StrictHttpFirewall in spring security 4.2 vs spring MVC @MatrixVariable

You can dilute the default spring security firewall using your custom defined instance of StrictHttpFirewall (at your own risk) @Bean public HttpFirewall allowUrlEncodedSlashHttpFirewall() { StrictHttpFirewall firewall = new StrictHttpFirewall(); firewall.setAllowUrlEncodedSlash(true); firewall.setAllowSemicolon(true); return firewall; } And then use this custom firewall bean in WebSecurity (Spring boot does not need this change) @Override public void configure(WebSecurity web) … Read more