Spring – @Transactional – What happens in background?

This is a big topic. The Spring reference doc devotes multiple chapters to it. I recommend reading the ones on Aspect-Oriented Programming and Transactions, as Spring’s declarative transaction support uses AOP at its foundation. But at a very high level, Spring creates proxies for classes that declare @Transactional on the class itself or on members. … Read more

Downloading a file from spring controllers

@RequestMapping(value = “/files/{file_name}”, method = RequestMethod.GET) public void getFile( @PathVariable(“file_name”) String fileName, HttpServletResponse response) { try { // get your file as InputStream InputStream is = …; // copy it to response’s OutputStream org.apache.commons.io.IOUtils.copy(is, response.getOutputStream()); response.flushBuffer(); } catch (IOException ex) { log.info(“Error writing file to output stream. Filename was ‘{}'”, fileName, ex); throw new RuntimeException(“IOError … Read more

How to respond with an HTTP 400 error in a Spring MVC @ResponseBody method returning String

Change your return type to ResponseEntity<>, and then you can use the below for 400: return new ResponseEntity<>(HttpStatus.BAD_REQUEST); And for a correct request: return new ResponseEntity<>(json,HttpStatus.OK); After Spring 4.1 there are helper methods in ResponseEntity which could be used as: return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null); and return ResponseEntity.ok(json);

How to solve the “failed to lazily initialize a collection of role” Hibernate exception

If you know that you’ll want to see all Comments every time you retrieve a Topic then change your field mapping for comments to: @OneToMany(fetch = FetchType.EAGER, mappedBy = “topic”, cascade = CascadeType.ALL) private Collection<Comment> comments = new LinkedHashSet<Comment>(); Collections are lazy-loaded by default, take a look at this if you want to know more.

JsonMappingException: No suitable constructor found for type [simple type, class ]: can not instantiate from JSON object

So, finally I realized what the problem is. It is not a Jackson configuration issue as I doubted. Actually the problem was in ApplesDO Class: public class ApplesDO { private String apple; public String getApple() { return apple; } public void setApple(String apple) { this.apple = apple; } public ApplesDO(CustomType custom) { //constructor Code } … Read more

What in the world are Spring beans?

The Spring core technologies reference documentation describes what beans are. Per the Introduction to the Spring IoC Container and Beans section (where “IoC” means “inversion of control”): In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object … Read more

Spring @Transactional – isolation, propagation

Good question, although not a trivial one to answer. Propagation Defines how transactions relate to each other. Common options: REQUIRED: Code will always run in a transaction. Creates a new transaction or reuses one if available. REQUIRES_NEW: Code will always run in a new transaction. Suspends the current transaction if one exists. The default value … Read more