@EnableTransactionManagement annotation with 2 transaction managers

In your configuration class, use @EnableTransactionManagement annotation. Define a transaction manager in this class as: @Bean(name=”txName”) public HibernateTransactionManager txName() throws IOException{ HibernateTransactionManager txName= new HibernateTransactionManager(); txName.setSessionFactory(…); txName.setDataSource(…); return txName; } There on, in your class/method that executes transactional job(s), annotate as follows: @Transactional(“txName”) or @Transactional(value = “txName”) This is how you would tie a name … Read more

org.hibernate.HibernateException: No Session found for current thread

getCurrentSession() only makes sense inside a scope of transaction. You need to declare an appropriate transaction manager, demarcate boundaries of transaction and perform data access inside it. For example, as follows: <bean id = “transactionManager” class = “org.springframework.orm.hibernate4.HibernateTransactionManager”> <property name = “sessionFactory” ref = “sessionFactory” /> </bean> . PlatformTransactionManager ptm = context.getBean(PlatformTransactionManager.class); TransactionTemplate tx = … Read more

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

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);

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