How to enable request scope in async task executor

We ran into the same problem – needed to execute code in the background using @Async, so it was unable to use any Session- or RequestScope beans. We solved it the following way: Create a custom TaskPoolExecutor that stores scoped information with the tasks Create a special Callable (or Runnable) that uses the information to … Read more

Passing multiple variables in @RequestBody to a Spring MVC controller using Ajax

While it’s true that @RequestBody must map to a single object, that object can be a Map, so this gets you a good way to what you are attempting to achieve (no need to write a one off backing object): @RequestMapping(value = “/Test”, method = RequestMethod.POST) @ResponseBody public boolean getTest(@RequestBody Map<String, String> json) { //json.get(“str1”) … Read more

How to define a List bean in Spring?

Import the spring util namespace. Then you can define a list bean as follows: <?xml version=”1.0″ encoding=”UTF-8″?> <beans xmlns=”http://www.springframework.org/schema/beans” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:util=”http://www.springframework.org/schema/util” xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd”> <util:list id=”myList” value-type=”java.lang.String”> <value>foo</value> <value>bar</value> </util:list> The value-type is the generics type to be used, and is optional. You can also specify the list implementation class using the attribute list-class.

Add context path to Spring Boot application

Why are you trying to roll your own solution. Spring-boot already supports that. If you don’t already have one, add an application.properties file to src\main\resources. In that properties file, add 2 properties: server.contextPath=/mainstay server.port=12378 UPDATE (Spring Boot 2.0) As of Spring Boot 2.0 (due to the support of both Spring MVC and Spring WebFlux) the … Read more

Spring JSON request getting 406 (not Acceptable)

406 Not Acceptable The resource identified by the request is only capable of generating response entities which have content characteristics not acceptable according to the accept headers sent in the request. So, your request accept header is application/json and your controller is not able to return that. This happens when the correct HTTPMessageConverter can not … Read more

Spring Data Rest and Cors

Indeed, before Spring Data REST 2.6 (Ingalls) only HandlerMapping instances created by Spring MVC WebMvcConfigurationSupport and controllers annotated with @CrossOrigin were CORS aware. But now that DATAREST-573 has been fixed, RepositoryRestConfiguration now exposes a getCorsRegistry() for global setup and @CrossOrigin annotations on repositories are also recognized so this is the recommended approach. See https://stackoverflow.com/a/42403956/1092077 answer … Read more

Spring @PropertySource using YAML

Spring-boot has a helper for this, just add @ContextConfiguration(initializers = ConfigFileApplicationContextInitializer.class) at the top of your test classes or an abstract test superclass. Edit: I wrote this answer five years ago. It doesn’t work with recent versions of Spring Boot. This is what I do now (please translate the Kotlin to Java if necessary): @TestPropertySource(locations=[“classpath:application.yml”]) … Read more

Spring Data JPA map the native query result to Non-Entity POJO

I think the easiest way to do that is to use so called projection. It can map query results to interfaces. Using SqlResultSetMapping is inconvienient and makes your code ugly :). An example right from spring data JPA source code: public interface UserRepository extends JpaRepository<User, Integer> { @Query(value = “SELECT firstname, lastname FROM SD_User WHERE … Read more

Spring MVC Multipart Request with JSON

This is how I implemented Spring MVC Multipart Request with JSON Data. Multipart Request with JSON Data (also called Mixed Multipart): Based on RESTful service in Spring 4.0.2 Release, HTTP request with the first part as XML or JSON formatted data and the second part as a file can be achieved with @RequestPart. Below is … Read more