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

Spring MVC 3, Interceptor on all excluding some defined paths

Since Spring 3.2 they added that feature with the tag mvc:exclude-mapping See this example from the Spring documentation: <mvc:interceptors> <bean class=”org.springframework.web.servlet.i18n.LocaleChangeInterceptor” /> <mvc:interceptor> <mvc:mapping path=”/**”/> <mvc:exclude-mapping path=”/admin/**”/> <bean class=”org.springframework.web.servlet.theme.ThemeChangeInterceptor” /> </mvc:interceptor> <mvc:interceptor> <mvc:mapping path=”/secure/*”/> <bean class=”org.example.SecurityInterceptor” /> </mvc:interceptor> Here’s the link to the doc

Return JSON for ResponseEntity

The root of the problem is that Spring (via ResponseEntity, RestController, and/or ResponseBody) will use the contents of the string as the raw response value, rather than treating the string as JSON value to be encoded. This is true even when the controller method uses produces = MediaType.APPLICATION_JSON_VALUE, as in the question here. It’s essentially … Read more

Sending Multipart File as POST parameters with RestTemplate requests

A way to solve this without needing to use a FileSystemResource that requires a file on disk, is to use a ByteArrayResource, that way you can send a byte array in your post (this code works with Spring 3.2.3): MultiValueMap<String, Object> map = new LinkedMultiValueMap<String, Object>(); final String filename=”somefile.txt”; map.add(“name”, filename); map.add(“filename”, filename); ByteArrayResource contentsAsResource … Read more

Spring MVC : read file from src/main/resources

Resource resource = new ClassPathResource(fileLocationInClasspath); InputStream resourceInputStream = resource.getInputStream(); using ClassPathResource and interface resource. But make sure you are copying the resources directory correctly (using maven), and its not missing, for example if running tests as part of test context.

Spring MVC – Project structure – best practices [closed]

I’d suggest you take a look at Spring’s Project Sagan. It’s the source code for their current website (http://spring.io). While they used a multi-module approach, it wasn’t divided as you are suggesting. They really just pulled out some client work and kept the rest in a single module. This site was written by the Spring … Read more

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