How to shutdown a Spring Boot Application in a correct way?

If you are using the actuator module, you can shutdown the application via JMX or HTTP if the endpoint is enabled. add to application.properties: endpoints.shutdown.enabled=true Following URL will be available: /actuator/shutdown – Allows the application to be gracefully shutdown (not enabled by default). Depending on how an endpoint is exposed, the sensitive parameter may be … Read more

What is this spring.jpa.open-in-view=true property in Spring Boot?

The OSIV Anti-Pattern Instead of letting the business layer decide how it’s best to fetch all the associations that are needed by the View layer, OSIV (Open Session in View) forces the Persistence Context to stay open so that the View layer can trigger the Proxy initialization, as illustrated by the following diagram. The OpenSessionInViewFilter … Read more

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

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

Spring 3 RequestMapping: Get path value

Non-matched part of the URL is exposed as a request attribute named HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE: @RequestMapping(“/{id}/**”) public void foo(@PathVariable(“id”) int id, HttpServletRequest request) { String restOfTheUrl = new AntPathMatcher().extractPathWithinPattern(request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE).toString(),request.getRequestURI()); … }

How to return a custom object from a Spring Data JPA GROUP BY query

Solution for JPQL queries This is supported for JPQL queries within the JPA specification. Step 1: Declare a simple bean class package com.path.to; public class SurveyAnswerStatistics { private String answer; private Long cnt; public SurveyAnswerStatistics(String answer, Long cnt) { this.answer = answer; this.count = cnt; } } Step 2: Return bean instances from the repository … 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

Spring Boot not serving static content

Not to raise the dead after more than a year, but all the previous answers miss some crucial points: @EnableWebMvc on your class will disable org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration. That’s fine if you want complete control but otherwise, it’s a problem. There’s no need to write any code to add another location for static resources in addition to … Read more