Does supply a sample that could run it successfully is so difficult or just spring is so difficult to use naturally? - spring-boot

I git cloned spring-boot and wanted to start spring-boot-sample-web-ui, after importing it to intellij idea and run SampleWebUiApplication, first I met this error
Caused by: java.lang.ClassNotFoundException: org.apache.juli.logging.LogFactory
Then added this dependency
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-juli</artifactId>
</dependency>
Run it again have another error again
Caused by: java.lang.ClassNotFoundException: org.springframework.http.client.support.BasicAuthorizationInterceptor
Does supply a sample that could start it successfully is so difficult or just spring is so difficult to use naturally?

The recommended approach if you are new to Spring / Spring Boot is to look at the Getting Started guides. I haven't had an issue yet with one of those and having them work properly. Based on the sample you listed I am guessing the Serving Web Content with Spring MVC would be a good one to look at.
I did try the spring-boot-sample-web-ui project in my local environment using Eclipse. I didn't run into any issues with it so it looks like it might be an issue with the way you imported / loaded the project in IntelliJ.

I resolved it by change parent in pom
<parent>
- <!-- Your own application should inherit from spring-boot-starter-parent -->
<groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-samples</artifactId>
- <version>1.4.0.BUILD-SNAPSHOT</version>
+ <artifactId>spring-boot-starter-parent</artifactId>
+ <version>1.4.0.RELEASE</version>
</parent>

Related

Invalid web-app-versionType after Spring Upgrade

To support JUnit 5, I recently upgraded a Spring Boot application to version 2.4.7.
Local development uses an embedded Tomcat server, while all other environments run on a Weblogic server 12.1.3.
Everything runs locally, but using the Weblogic Server results in the following exception:
Caused By: weblogic.descriptor.DescriptorException: VALIDATION PROBLEMS WERE FOUND
/weblogic.utils.classloaders.GenericClassLoader#700d06bb finder: weblogic.utils.classloaders.CodeGenClassFinder#40ce7cdd annotation: APP#/WEB-INF/lib/tomcat-embed-websocket-9.0.46.jar!/META-INF/web-fragment.xml:18:3:18:3: problem: cvc-enumeration-valid: string value '4.0' is not a valid enumeration value for web-app-versionType in namespace http://xmlns.jcp.org/xml/ns/javaee
at weblogic.descriptor.internal.MarshallerFactory$1.evaluateResults(MarshallerFactory.java:249)
Other questions on the topic led me to check web.xml, but it contains version="3.0".
I don't know how to proceed because I don't understand where this comes from.
Do you have this in your pom.xml to exclude packaging of the tomcat jar files when creating the war file?
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
The spring-boot-starter-tomcat dependencies must have provided scope.

thymeleaf caching with spring boot

I have found many, many posts about disabling template caching with Thymeleaf for development, but I can't get it to work. My stack:
Spring Boot 1.5.7
spring-boot-devtools
Thymeleaf
IntelliJ IDEA Ultimate 2017.2
I don't need to auto-rebuild: I've mapped the "rebuild" command in IntelliJ to ⌘-S and this works great for hot-swapping Java classes. However, css and html templates still only show changes if I restart my server.
My application.properties:
spring.thymeleaf.prefix=/WEB-INF/templates/
spring.thymeleaf.cache=false
I've tried:
• moving the templates folder into webapp/resources (and adjusting the templates root)
• wiring up different kinds of template resolvers (e.g. FileTemplateResolver)
• configuring the template resolver in a #Configuration file instead of letting Spring Boot use defaults and application.properties values
Here are the relevant entries in my pom.xml file:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
Is there something I'm missing here?
In case anyone else runs into this problem, it is specific to IntelliJ:
I was using a "Tomcat" template under Launch Configurations. Once I changed it to a "Spring Boot" configuration template, it behaves as expected.

are maven dependency exclusions necessary when using spring (mvc) + hadoop + hive?

I have a web app working great. Tried to connect to hadoop using hive. Tests work fine, but I can't run the web app. I get an error from transitive maven dependencies on hadoop-core bringing in j2ee jars that override Tomcat and mess up when trying to run the web app (specifically in loading the context).
Foolishly I thought maybe if I just use Spring Data built for CDH5 they would have covered all that. No such luck. I was following their docs here: https://github.com/spring-projects/spring-hadoop/wiki/Build-with-Cloudera-CDH5
Here's my current POM:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-hadoop</artifactId>
<version>2.0.4.RELEASE-cdh5</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>${hive.version}</version>
<scope>runtime</scope>
</dependency>
Here is the error:
SEVERE: Servlet.service() for servlet [jsp] in context with path [] threw exception [java.lang.AbstractMethodError: javax.servlet.jsp.JspFactory.getJspApplicationContext(Ljavax/servlet/ServletContext;)Ljavax/servlet/jsp/JspApplicationContext;] with root cause
java.lang.AbstractMethodError: javax.servlet.jsp.JspFactory.getJspApplicationContext(Ljavax/servlet/ServletContext;)Ljavax/servlet/jsp/JspApplicationContext;
at org.apache.jasper.compiler.Validator$ValidateVisitor.<init>(Validator.java:515)
at org.apache.jasper.compiler.Validator.validateExDirectives(Validator.java:1817)
at org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:217)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:373)
I also got this error when building direct from cloudera's repos
I could start stuffing exclusions in there, but that feels hacky, and I'm paranoid about other transitive dependency errors cropping up that I may not know about.
I've pored over the docs and the sample code and pom files here: https://github.com/spring-projects/spring-hadoop-samples/blob/master/hive/pom.xml
They don't seem to have exclusions in their POM files. However, I've seen other people do it, such as here: Spring + Maven + Hadoop
Is that the accepted way to work with these technologies? This is my first time so am seeking some confirmation here. Perhaps I'm missing something?
Is it canonical to simply have exclusions

How is a war file created for spring boot with maven?

I'm trying to follow the guide for converting a spring project to a war.
http://spring.io/guides/gs/convert-jar-to-war/
It starts out using maven and gradle and then right after the jar portion it completely forgets about maven and only has gradle updates.
There are two main changes that you need to make in the pom. The first is to change the project's packaging type to war:
<groupId>org.springframework</groupId>
<artifactId>gs-convert-jar-to-war</artifactId>
<version>0.1.0</version>
<packaging>war</packaging>
The second is to add a dependency on spring-boot-starter-tomcat and mark it as provided:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
mvn package will now produce a war file that can be run using java -jar or deployed to a separate servlet container.
There is an official guide at spring:
http://spring.io/guides/gs/convert-jar-to-war-maven/
Pay attention to "Initialize the servlet" section.
It explains an important point of adding a class that substitutes web.xml. Without it (or without proper web.xml) you will get a war file but when deployed nothing will be accessible in browser as nothing will be registered as your request dispatcher.
Also note that it is best to run this example on Tomcat 8 as it supports latest servlet specs. I have spent number of hours trying to figure out why it does not work on my Tomcat 7.

spring boot fails to run - IllegalAccessError on startup

I have a strange issue and that I have not been able to resolve. I am trying to use the sample JPA sprint boot (v0.5.0-M6) project as a starting point for an application I am writing. I grabbed the JPA sample and got that to run locally. I then proceeded to add my code into that project. I imported into eclipse and run as spring-boot. Then I get this error:
Exception in thread "main" java.lang.IllegalAccessError: tried to access class org.springframework.core.io.DefaultResourceLoader$ClassPathContextResource from class org.springframework.boot.context.embedded.EmbeddedWebApplicationContext
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getResourceByPath(EmbeddedWebApplicationContext.java:386)
at org.springframework.core.io.DefaultResourceLoader.getResource(DefaultResourceLoader.java:100)
at org.springframework.context.support.GenericApplicationContext.getResource(GenericApplicationContext.java:211)
at org.springframework.boot.context.initializer.ConfigFileApplicationContextInitializer.load(ConfigFileApplicationContextInitializer.java:192)
at org.springframework.boot.context.initializer.ConfigFileApplicationContextInitializer.load(ConfigFileApplicationContextInitializer.java:134)
at org.springframework.boot.context.initializer.ConfigFileApplicationContextInitializer.initialize(ConfigFileApplicationContextInitializer.java:121)
at org.springframework.boot.SpringApplication.applyInitializers(SpringApplication.java:403)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:287)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:749)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:738)
From what I can tell, this is the wrong application context, since I am not using XML configuration but annotations to drive the configuration. Spring boot is automatically selecting this one and I need to tell it not to use the above. At least that is what I think I need to do.
I did search here and in the spring.io forums but no one seems to have the same issue.
Question: What drives the selection of an application context with the auto configuration?
What should I be looking at to resolve the above issue? What else do I need to provide to here help debug the auto configuration issue?
TIA,
Scott
I got the same problem.
if you use maven check your pom.xml
remove conflict version in Spring Lib.
<properties>
<hibernate.version>4.2.0.Final</hibernate.version>
<mysql.connector.version>5.1.21</mysql.connector.version>
<spring.version>3.2.2.RELEASE</spring.version>
</properties>
i remove this line
<spring.version>3.2.2.RELEASE</spring.version>
and in maven dependency just
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
Hope this help.
I was facing the same problem, and solved fixing the referencing to the boot-starter-parent pom.
At the pom.xml file I used:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>0.5.0.M6</version>
</parent>
I have some urgent issues to fix now, so I didn't inspected this parent pom to see what's so important here, but I hope this can help you - don't forget to verify the version you're using!

Resources