Posts

Showing posts from April, 2019

Starting Spring Boot Application in Debug mode

Start the Spring boot application with the following jvm argument. mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"

Maven attach source code for external Jar

For Java project that uses Maven for dependency and building we can attach the third party library source. By default the source is not available, using the following commands we can down load the associated source and java doc from maven repository by reading the project pom file. Maven will not be able to download sources and docs if those were not published for a particular library. mvn dependency:sources mvn dependency:resolve -Dclassifier=javadoc //to download source for only one library mvn dependency:sources -DincludeArtifactIds=spring-boot Now after executing the above two commands, maven will download the respective sources and java docs. Now we can attach the source in the IDE. For eclipse specially it is better to use the Maven Eclipse plugin mvn eclipse:eclipse -DdownloadSources=true mvn eclipse:eclipse -DdownloadSources=true -DdownloadJavadocs=false Executing the above commands will also download the sources and it also updates the .classpath file. Now refr

Failed to instantiate [java.util.List]: Specified class is an interface in HTTP controller handler?

Problem Use case: Following SpringBoot Rest Controller is not able to map the request payload @Controller @RequestMapping("/api") public class BlogController { @RequestMapping(value = "/blogstories", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<List<BlogStoryObject>> addUpdateblog(List<BlogStoryObject> blogItems) { List<BlogStoryObject> result = new ArrayList<BlogStoryObject>(); //TODO:: Call Service to update/save objects return new ResponseEntity<List<RootBlogStoryObject>>(result, HttpStatus.OK); } } Exception Details [Truncated]: And the error thrown at runtime is Failed to instantiate [java.util.List]: Specified class is an interface. Solution Solution to the problem is to @RequestBody annotation to the method definition, so the following will work. public ResponseEntity<List<RootBlogStoryObject>> addUpdateblog

Spring Boot Error : Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory

Problem Use case: While starting a Spring Boot Application using mvn spring-boot:run The Application fails to start Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean. Exception Details [Truncated]: [ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.9.RELEASE:run (default-cli) on project Javaexp-springboot-rest: An exception occurred while running. null: InvocationTargetException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean. -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1

Spring Boot CommandLineRunner

CommandLineRunner is an interface which allows us to write code that would be executed when the SpringBoot Application is initialized and before the startup is finished. Technology used Java 8 Spring boot 1.5.9 Logback 1.1.1.1 Maven 3.5.0 Implementation This project uses Maven to build the project.  It shows a very simple example of a Spring boot Application and two Classes implementing CommandLineRunner. High level steps as described bellow with sample code Define the pom.xml file Write Spring boot app class Write Spring boot CommandLineRunner class Change the order with Order annotation For simplicity the pom file is not provided here, but it can be found on the git repo here  https://github.com/siddharthagit/spring-boot-references/blob/master/spingboot-resttemplate/pom.xml SpringBoot Application Class ResttemplateApplication.java @SpringBootApplication public class ResttemplateApplication { private static final Logger log = LoggerFactory.getLogge