Can you completely disable CORS support in Spring?

For newer versions of spring boot: @Configuration public class WebConfiguration implements WebMvcConfigurer { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping(“/**”).allowedMethods(“*”); } } The Kotlin way @Configuration class WebConfiguration : WebMvcConfigurer { override fun addCorsMappings(registry: CorsRegistry) { registry.addMapping(“/**”).allowedMethods(“*”) } }

Unable to autowire the service inside my authentication filter in Spring

You cannot use dependency injection from a filter out of the box. Although you are using GenericFilterBean your Servlet Filter is not managed by spring. As noted by the javadocs This generic filter base class has no dependency on the Spring org.springframework.context.ApplicationContext concept. Filters usually don’t load their own context but rather access service beans … Read more

How to unit test a Spring MVC controller using @PathVariable?

I’d call what you’re after an integration test based on the terminology in the Spring reference manual. How about doing something like: import static org.springframework.test.web.ModelAndViewAssert.*; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({/* include live config here e.g. “file:web/WEB-INF/application-context.xml”, “file:web/WEB-INF/dispatcher-servlet.xml” */}) public class MyControllerIntegrationTest { @Inject private ApplicationContext applicationContext; private MockHttpServletRequest request; private MockHttpServletResponse response; private HandlerAdapter handlerAdapter; private MyController controller; … Read more

getServletConfigClasses() vs getRootConfigClasses() when extending AbstractAnnotationConfigDispatcherServletInitializer

A Bit on ApplicationContext Hierarchies Spring’s ApplicationContext provides the capability of loading multiple (hierarchical) contexts, allowing each to be focused on one particular layer, such as the web layer of an application or middle-tier services. One of the canonical examples of using hierarchical ApplicationContext is when we have multiple DispatcherServlets in a web application and … Read more

Spring Bean Scopes

From the spring specs, there are five types of bean scopes supported : 1. singleton(default*) Scopes a single bean definition to a single object instance per Spring IoC container. 2. prototype Scopes a single bean definition to any number of object instances. 3. request Scopes a single bean definition to the lifecycle of a single … Read more

How to distinguish between null and not provided values for partial updates in Spring Rest Controller

Another option is to use java.util.Optional. import com.fasterxml.jackson.annotation.JsonInclude; import java.util.Optional; @JsonInclude(JsonInclude.Include.NON_NULL) private class PersonDTO { private Optional<String> firstName; private Optional<String> lastName; /* getters and setters … */ } If firstName is not set, the value is null, and would be ignored by the @JsonInclude annotation. Otherwise, if implicitly set in the request object, firstName would … Read more