Multiple scenarios @RequestMapping produces JSON/XML together with Accept or ResponseEntity

Using Accept header is really easy to get the format json or xml from the REST service. This is my Controller, take a look produces section. @RequestMapping(value = “properties”, produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE}, method = RequestMethod.GET) public UIProperty getProperties() { return uiProperty; } In order to consume the REST service we can use the code … Read more

Benefits of JavaConfig over XML configurations in Spring?

There are some advantages Java is type safe. Compiler will report issues if you are configuring right bean class qualifiers. XML based on configuration can quickly grow big. [Yes we can split and import but still] Search is much simpler, refactoring will be bliss. Finding a bean definition will be far easier. There are still … Read more

Encoded slash (%2F) with Spring RequestMapping path param gives HTTP 400

for spring-boot, the following did the trick @SpringBootApplication public class Application extends WebMvcConfigurerAdapter { public static void main(String[] args) throws Exception { System.setProperty(“org.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH”, “true”); SpringApplication.run(Application.class, args); } @Override public void configurePathMatch(PathMatchConfigurer configurer) { UrlPathHelper urlPathHelper = new UrlPathHelper(); urlPathHelper.setUrlDecode(false); configurer.setUrlPathHelper(urlPathHelper); } }

When use AbstractAnnotationConfigDispatcherServletInitializer and WebApplicationInitializer?

With the release of the Servlet 3.0 spec it became possible to configure your Servlet Container with (almost) no xml. For this there is the ServletContainerInitializer in the Servlet specification. In this class you can register filters, listeners, servlets etc. as you would traditionally do in a web.xml. Spring provides a an implementation the SpringServletContainerInitializer … Read more

Add attributes to the model of all controllers in Spring 3

You could write an org.springframework.web.servlet.HandlerInterceptor. (or its convenience subclass HandlerInterceptorAdapter) @See: Spring Reference chapter: 15.4.1 Intercepting requests – the HandlerInterceptor interface It has the method: void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception; This method is invoked after the controller is done and before the view is rendered. So you can use … Read more

How to set ‘Content-Disposition’ and ‘Filename’ when using FileSystemResource to force a file download file?

In addition to the accepted answer, Spring has the class ContentDisposition specific for this purpose. I believe it deals with the file name sanitization. ContentDisposition contentDisposition = ContentDisposition.builder(“inline”) .filename(“Filename”) .build(); HttpHeaders headers = new HttpHeaders(); headers.setContentDisposition(contentDisposition);

How to register Spring @Configuration annotated class instead of applicationContext.xml file in web.xml?

In web.xml you need to bootstrap the context with AnnotationConfigWebApplicationContext: <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextClass</param-name> <param-value> org.springframework.web.context.support.AnnotationConfigWebApplicationContext </param-value> </init-param> <init-param> <param-name>contextConfigLocation</param-name> <param-value> org.package.YouConfigurationAnnotatedClass </param-value> </init-param> </servlet> And don’t forget to use @EnableWebMvc for your MVC annotations to kick in. further reading: Spring 3.1 MVC Enhancements Spring 3.1 MVC Namespace Enhancements And Configuration EDIT as a … Read more

How to get a method’s annotation value from a ProceedingJoinPoint?

You can get the Signature from a ProceedingJoinPoint and in case of a method invocation just cast it to a MethodSignature. @Around(“execution(public * *(..)) && @annotation(com.mycompany.MyAnnotation)”) public Object procede(ProceedingJoinPoint call) throws Throwable { MethodSignature signature = (MethodSignature) call.getSignature(); Method method = signature.getMethod(); MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class); } But you should first add an annotation attribute. … Read more

What’s the difference between and in servlet?

<context:annotation-config> declares support for general annotations such as @Required, @Autowired, @PostConstruct, and so on. <mvc:annotation-driven /> declares explicit support for annotation-driven MVC controllers (i.e. @RequestMapping, @Controller, although support for those is the default behaviour), as well as adding support for declarative validation via @Valid and message body marshalling with @RequestBody/ResponseBody.