I am still very new to using Apache Camel and am trying to adapt an example provided by someone (https://www.javainuse.com/camel/camel-consume-rest) to essentially do the same thing the original application does, except by using a combination of Spring Boot and Camel with XML route configuration.
I downloaded the original application code (both the Camel consumer and the HTTP server) and got those to run and execute properly very easily.
Now, I want to modify the application a little so that Spring Boot is used to do all the behind-the-scenes configuration, and use the XML DSL to configure the Camel routes. The original Camel consumer application does not use Spring at all.
I made the changes that seemed to make sense, but cannot get the application to run. I get the following exception:
Exception in thread "main" java.lang.IllegalAccessError: tried to access method org.springframework.core.io.support.SpringFactoriesLoader.loadFactoryNames(Ljava/lang/Class;Ljava/lang/ClassLoader;)Ljava/util/List; from class org.springframework.boot.SpringApplication
at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:418)
at org.springframework.boot.SpringApplication.getSpringFactoriesInstances(SpringApplication.java:412)
at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:268)
at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:249)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202)
at org.test.ApplicationMain.main(ApplicationMain.java:12)
I do not understand what it's complaining about. I'm including resources below so that people can look at what I'm doing. I apologize in advance for the amount of code.
ApplicationMain.java:
#SpringBootApplication
#ImportResource("classpath:camel-context.xml")
public class ApplicationMain {
public static void main(String[] args) {
SpringApplication.run(ApplicationMain.class, args);
}
}
Configurer.java:
#Configuration
#ComponentScan("org.test")
public class Configurer {
#Bean
public MyProcessor myProcessor() {
return new MyProcessor();
}
}
application.yml:
---
camel:
springboot:
name: CamelConsumer
main-run-controller: true
camel-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<import resource="classpath:routes/routes.xml" />
<camelContext id="camel-context" xmlns="http://camel.apache.org/schema/spring">
<routeContextRef ref="routes" />
</camelContext>
</beans>
routes/routes.xml:
Note: I do not know whether I'm using the correct syntax for the elements in the route definition below. I'm trying to adapt it from the original, which was done in Java DSL. I'm also not sure whether or not I need the processor bean definition, since I also have the bean defined in the Configurer above.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<bean id="myProcessor" class="org.test.processor.MyProcessor" />
<routeContext id="routes" xmlns="http://camel.apache.org/schema/spring">
<route id="httpTest">
<from uri="file:C:/inboxREST?noop=true" />
<setHeader headerName="HTTP_METHOD">
<simple>GET</simple>
</setHeader>
<to uri="http://localhost:8080/employee?id=5" />
<process ref="myProcessor" />
</route>
</routeContext>
</beans>
MyProcessor.java:
public class MyProcessor implements Processor {
public MyProcessor() {
}
public void process(Exchange exchange) throws Exception {
System.out.println(exchange.getIn().getBody(String.class));
}
}
pom.xml:
Note: You may notice a mix of Spring Boot Camel starters as well as Camel dependencies for Camel components (like HTTP). It's not clear to me which of these I actually need. Do the Spring Boot starters remove the need to include Camel component dependencies?
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.javainuse</groupId>
<artifactId>apache-camel-consume</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath />
</parent>
<properties>
<java.version>1.8</java.version>
<apache-camel-springboot.version>3.1.0</apache-camel-springboot.version>
<apache-camel.version>3.1.0</apache-camel.version>
</properties>
<dependencies>
<!-- Camel BOM -->
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-dependencies</artifactId>
<version>${apache-camel-springboot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Camel Starter -->
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>${apache-camel-springboot.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot</artifactId>
<version>${apache-camel-springboot.version}</version>
</dependency>
<!-- Camel HTTP -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-http</artifactId>
<version>${apache-camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-http-starter</artifactId>
<version>${apache-camel-springboot.version}</version>
</dependency>
<!-- Camel Jackson -->
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-jackson-starter</artifactId>
<version>${apache-camel-springboot.version}</version>
</dependency>
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<version>2.4.0</version>
</plugin>
</plugins>
</build>
</project>
Argh. Very tricky and hard to find problem.
It turns out that there were dependencies in my maven repo (.m2/repository) that I needed to clean out. I think they were brought in by the original project indicated at the start of the post.
Then, I needed to edit the eclipse project properties to remove the old (and completely unneeded) jars. Once this was done and a clean build completed, the application started as it should.
Related
I tried to use #configurable on my project to have #autowired service in object that are outside of spring context or something like that but didn't manage to make it work. The service is always null.
(I compared 4 different tutorials but nothing worked)
So, by despair I tried to download working examples directly to compare them but they didn't worked as well. (but I had to change them a bit, see in "Notes")
Here are the two examples I tried to download but gave me a nullPointerException too :
https://github.com/kenyattaclark/Spring-Configurable-Example
https://www.baeldung.com/spring-inject-bean-into-unmanaged-objects (the link is at the end of the page)
So, are these examples working on your side? Did I miss something really important?
Notes :
I'm using the correto version of java(but I suppose it's unlikely that the problem come from here)
I had to make some changes in the two examples ot make them work so maybe that's why :
for the first one I added a <pluginManagement> in the pom because if I didn't, eclipse was giving me an error "Plugin execution not covered by lifecycle configuration" and if I tried to compile I had the error : "error can't determine superclass of missing type java.lang.Object
when batch building BuildConfig[null] #Files=3 AopXmls=#0
[Xlint:cantFindType]"
for the second one I didn't use <pluginManagement>. But I didn't download the parent folder so I changed that in the pom and also forced the java version to 11 or maven was resetting it to 1.5 and had to update the junit test to junit5.
So there is room for me screwing all this up, but I'm tired of looking everywhere on internet and couldn't even get one working example.
So, if someone know what's wrong or how to make one example of #configurable work please tell me.
I looked at the first example. It uses very old versions of Spring, AspectJ and AspectJ Maven Plugin. I upgraded the POM to use
Spring 5.3.16,
AspectJ 1.9.9 (supports up to Java 18)
AspectJ.dev AspectJ Maven Plugin 1.13.1 (better and more up to date than Mojohaus, also supports Java 18 and can support any more recent version in the future by simply upgrading the aspectjtools plugin dependency, no plugin upgrade necessary).
Please use JDK 11+ on your build system. More recent AspectJ compiler versions need it. You can still compile to Java 8 byte code, though. (But why would you?)
Here is the Maven POM. I also added a separate dependency management section in order to globally manage dependency versions and exclusions. Then I used mvn dependency:analyze and mvn dependency:tree in order to clean up used undeclared and delcared unused dependencies. If your project needs another set of dependencies, you need to adjust it to your needs, of course. I configured it just for what is used in this tiny sample program. In doing so, I also cleaned out a few unused XML name-space and schema declarations. If you need them in your program, just add them again.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.brightdome</groupId>
<artifactId>spring-configurable-sample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Spring Configurable Sample</name>
<description>
Sample project to show how to work with Spring's #Configurable capability
to inject dependencies into classes not instantiated by Spring.
</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<aspectj.version>1.9.9</aspectj.version>
<spring.version>5.3.16</spring.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<groupId>dev.aspectj</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.13.1</version>
<dependencies>
<!-- Override older AspectJ compiler in plugin, use latest one -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
<exclusions>
<!-- Only needed for load-time weaving, not compile-time -->
<exclusion>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<!-- Needed by spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<!-- Needed by compile-time aspects during runtime -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
</dependencies>
</project>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
"
>
<context:spring-configured/>
<context:component-scan base-package="com.brightdome"/>
<context:annotation-config/>
</beans>
package com.brightdome.sample.spring;
import org.springframework.stereotype.Service;
#Service
public class HelloWorldService {
public void sayHello() {
System.out.println("Hello world!");
}
}
package com.brightdome.sample.spring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
#Configurable
public class HelloWorldClient {
#Autowired
private HelloWorldService service;
public void sayHello() {
// Used injected instance of service
service.sayHello();
}
}
package com.brightdome.sample.spring;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloWorld {
public static void main(String[] args) {
// Initialize Spring Context
new ClassPathXmlApplicationContext("/META-INF/applicationContext.xml");
// Instantiate class by new'ing it up. i.e., Do not obtain from Spring context
HelloWorldClient client = new HelloWorldClient();
client.sayHello();
}
}
This compiles and runs just fine in both my IDE (IntelliJ IDEA) and with Maven. On the console, it should simply print "Hello world!" and then exit.
I am trying a simple spinoff of the MinimalWordCount example project for Apache Beam. I started a new project rather than downloading their archetpye. I'm running into an apparently common problem, that is not being solved by the common solution:
Exception in thread "main" java.lang.IllegalArgumentException: No Runner was specified and the DirectRunner was not found on the classpath.
Specify a runner by either:
Explicitly specifying a runner by providing the 'runner' property
Adding the DirectRunner to the classpath
Calling 'PipelineOptions.setRunner(PipelineRunner)' directly
The common solution here on SO is to add the correct dependencies, which I have done. Here is my very simple POM:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>test_pipeline</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.beam/beam-runners-direct-java -->
<dependency>
<groupId>org.apache.beam</groupId>
<artifactId>beam-runners-direct-java</artifactId>
<version>2.33.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.beam/beam-sdks-java-core -->
<dependency>
<groupId>org.apache.beam</groupId>
<artifactId>beam-sdks-java-core</artifactId>
<version>2.33.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.beam/beam-sdks-java-extensions-json-jackson -->
<dependency>
<groupId>org.apache.beam</groupId>
<artifactId>beam-sdks-java-extensions-json-jackson</artifactId>
<version>2.33.0</version>
</dependency>
</dependencies>
</project>
So how do I add the direct runner to my classpath (or solve this issue in any other way)?
Try removing the
<scope>test</scope>
from the beam-runners-direct-java dependency.
We have file payment.jar that developed with Spring Framework 3.1.2 . We need to use it as a dependency on a new project that its version is Spring Framework 5.x .
Problem: When payment.jar used as a dependency, the payment.jar transitive dependencies add to project class-path so in cause conflicting Spring Framework 3 and Spring Framework 5.
I tried solutions:
big fat jar:
create-an-executable-jar-with-dependencies-using-maven
there is no success, the fat JAR, adds Spring Framework 3 to project. (what I am doing wrong!)
excluding payment Spring Framework 3.1.2 dependency in new project:
because of using spring-ibatis implementation in payment.jar it is not worked. Spring Framework new versions do not support ibatis.
Error:
Caused by: java.lang.ClassNotFoundException:
org.springframework.orm.ibatis.support.SqlMapClientDaoSupport
my requirement:
I need to use payment.jar (with Spring Framework 3.1.2) as a dependency on new project with Spring Framework 5.x without problem mentioned above.
If the Spring versions are as incompatible as it sounds as though they are, you're probably going to have to recompile either the jar or the project so that they're using the same version of Spring.
If that's impossible (e.g. if you don't have source for the jar), you might be able to wrap the payment jar up in a microservice so that it's running in a different process, and talk to it via XML-RPC or SOAP.
Your wish
I need to use payment.jar (with Spring Framework 3.1.2) as a
dependency on new project with Spring Framework 5.x without problem
mentioned above.
You cannot do it. Because the mismatching of version of dependencies. It is not backward compatible between many things, many version of iBatis, Spring Framework, payment.jar and other dependencies.
You must upgrade it manually.
The best option for this is to upgrade this manually.
Even if you will do this with Spring 3 jar, you will land with a lot of issues while compiling and running.
The ideal solution would be to update everything to Spring 5 or deploy payment.jar separately as a microservice as others mentioned. But this is not the core part of your question, maybe you have some unavoidable requirements. So as #tomer-shahar said the solution is to use maven shade plugin.
This kind of conflicts happen a lot when you use open source libraries sharing the same libraries but with different versions.
The maven shade plugin allows renaming/recolocating the packages selected avoiding conflicts within versions. The drawback is that some classes will be duplicated (so it is far from ideal). See the example below:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>spring321-payment-shaded</finalName>
<relocations>
<relocation>
<pattern>spring.package</pattern>
<shadedPattern>shaded.spring.package</shadedPattern>
</relocation>
</relocations>
<artifactSet>
<includes>
<include>*:*</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
And then add as dependency to the Spring 5 project this shaded library with all the renaming/rellocation done. I used this interesting article where all the details are fully described.
I suggest you to convert your jar in Spring 5 and MyBatis. I think it's the better solution in terms of mantenibility and compatibility. Migrate to iBatis to MyBatis does not a big problem, I think.
I had a working project that uses Spring 5 and My-Batis 3. I encountered some problem to configure it, but in the end, I solved everything. Just check the POM configuration and the Spring configuration.
POM configuration
In the following lines of code, you can read the project pom (I removed useless part of the file for your problem).
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>it.dummy</groupId>
<artifactId>dummy</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Dummy App</name>
<url>http://maven.apache.org</url>
<properties>
<java-source.version>1.8</java-source.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.skip.tests>true</maven.skip.tests>
<servlet-api.version>3.1.0</servlet-api.version>
<spring.version>5.0.7.RELEASE</spring.version>
<swagger.version>2.9.2</swagger.version>
<jackson.version>2.9.6</jackson.version>
<mybatis-spring>1.3.2</mybatis-spring>
<junit.version>4.12</junit.version>
<frontend-maven-plugin.version>1.4</frontend-maven-plugin.version>
<mapstruct.version>1.2.0.Final</mapstruct.version>
</properties>
<dependencies>
<!-- -->
<!-- https://mvnrepository.com/artifact/org.mapstruct/mapstruct -->
<!-- https://github.com/mapstruct/mapstruct -->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>${mapstruct.version}</version>
</dependency>
<!-- cucumber -->
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.14.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-java -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-junit -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
<!-- test -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet-api.version}</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-rest-core -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-core</artifactId>
<version>3.0.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring Security Artifacts - START -->
<!-- http://mvnrepository.com/artifact/org.springframework.security/spring-security-web%20 -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- http://mvnrepository.com/artifact/org.springframework.security/spring-security-config%20 -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring Security Artifacts - END -->
<!-- Swagger - BEGIN -->
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<!-- Swagger - END -->
<!-- json -->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9</version>
</dependency>
</dependencies>
</project>
Just remember to check always the dependencies tree to avoid to have indesiderate Spring jars.
Spring configuration
I have different file for Spring configuration. I attach only the interesting one.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource" destroy-method="close">
<property name="URL" value="jdbc:oracle:thin:#xxx:yyy:zzz" />
<property name="user" value="aaa"/>
<property name="password" value="bbb"/>
<property name="connectionCachingEnabled" value="true" />
</bean>
<bean id="transactionManagerNG" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sqlSessionFactoryNG" class="org.mybatis.spring.SqlSessionFactoryBean" name="SqlSessionFactoryOrarioStandardDao">
<property name="dataSource" ref="dataSource" />
<!-- 1. Where is the xml for query's definitions -->
<property name="mapperLocations" value="classpath:com/dummy/persistence/dao/*.xml" />
<!-- 2. My-batis configuration -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 3. My-batis type alias package -->
<property name="typeAliasesPackage" value="com.dummy.persistence.model" />
</bean>
<!-- 4. needed to parse Java DAO interfaces -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="it.insiel.sanita.farmacieng.persistence.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryNG"/>
</bean>
<tx:annotation-driven/>
</beans>
In the above Spring file you will find:
How I defined the location where put XML for SQL queries (comment 1)
How I use My-batis configuration (comment 2)
How I defined the type alias package (comment 3)
How I defined the DAO interfaces
Last but not least the My-batis configuration file mybatis-config.xml, that I put in the same folder of Spring configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!-- http://mybatis.org/dtd/ -->
<configuration>
<settings>
<setting name="logImpl" value="LOG4J"/>
<setting name="cacheEnabled" value="true"/>
<setting name="defaultStatementTimeout" value="3000"/>
<setting name="mapUnderscoreToCamelCase" value="true"/>
<setting name="jdbcTypeForNull" value="NULL" />
</settings>
</configuration>
Though I have not done this with spring specifically, the general concept of what you are trying to accomplish is called shading.
https://softwareengineering.stackexchange.com/questions/297276/what-is-a-shaded-java-dependency
The reason the fat jar didn't work is because all jars were loaded to the classpath, and the wrong one will be chosen for at least on of the dependencies.
What you need to do is change the package name in both a dependency and the depending jar (in your case spring 3 and payment jar), so you basically end up with two pairs of dependencies that don't overlap.
This is relatively simple to do in maven and similar tools. See the link for more details.
I am using the H2 database in a Spring boot application. But unable to open it in the browser at http://localhost:8080/console. My pom.xml is as below:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.192</version>
</dependency>
Spring boot Configuration :
Springboot configuration file
#Configuration
public class WebConfiguration {
#Bean
ServletRegistrationBean h2servletRegistration(){
ServletRegistrationBean registrationBean = new ServletRegistrationBean( new WebServlet());
registrationBean.addUrlMappings("/console/*");
return registrationBean;
}
}
to use the H2 console you need to configure it in your .properties file
spring.h2.console.enabled=true
spring.h2.console.path=/h2console/
where /h2console/ is the path you want to use on the browser so you can change it to anything. Also if you have security enabled you might want to add it to the permitted paths
also add this to your HttpSecurity configuration http.headers().frameOptions().disable();
Edit
change your security configuration i'm pretty sure you might have spring security in your pom so use this instead, if not it should work
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
#Configuration
public class WebConfiguration extends WebSecurityConfigurerAdapter {
#Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity.authorizeRequests().antMatchers("/").permitAll().and()
.authorizeRequests().antMatchers("/console/**").permitAll();
httpSecurity.csrf().disable();
httpSecurity.headers().frameOptions().disable();
}
}
If have included spring-boot-starter-security artifact in your pom then by default basic authentication is enabled. Hence, to access your console either you disable the basic authentication by adding security.basic.enabled=false in your application.properties or allow the access in your configure method as below:
#Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
#Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity.csrf().disable().authorizeRequests().antMatchers("/").permitAll().and().authorizeRequests()
.antMatchers("/console/**").permitAll();
httpSecurity.headers().frameOptions().disable();
}
}
Thank you all for your generous help.The application class (Springboot) was in a separate package and it was not scanning other packages.Also I modified my Pom.xml a bit which finally helped me to access the console.Attached is my new Pom.xml.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.spring.app</groupId>
<artifactId>Demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringBootApp</name>
<description>Generator of statistics </description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.4.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--WebJars -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.4</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring AOP + AspectJ -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>
<!-- JavaConfig need this library -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<!-- Jackson JSON Mapper -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
go the POM file and add the dependency :
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
rebuild your project
this might help folks , all the configuration above is correct
Note - if you are not using any security then adding spring security is not required
Actualy problem is - when you open this url in chrome
http://localhost:8080/h2 chrome makes it --> https://localhost:8080/h2
To get rid of this issue - Below reference will help -
Google Chrome redirecting localhost to https
You might have face 2 situation including following errors:
localhost refused to connect
Double check the URL. Chrome automatically try to change http:// to https://
Check spring.h2.console.path (Path at witch the console avilible) to get your URL:
Default: /h2-console --> URL: http://localhost:8080/h2-console/
If you running IDE (e.g. IntelliJ Idea), make sure your app is running witch means your H2 data base is running!
You face 404 Error:
In this case your H2 Data base is correctly running on Port 8080 and you already have the connection with it.
Check spring.h2.console.path (Path at witch the console avilible) to get your URL:
Default: /h2-console --> URL: http://localhost:8080/h2-console/
Enable H2 Console
spring.h2.console.enabled=true
The issue might be also be caused by adding server.servlet.context-path to properties. The new url will be composed of server.servlet.context-path plus spring.h2.console.path
If you have renamed the h2 path in the application.properties to "console" you've to add it in your antMatcher like .antMatcher("/console/**") with two asterisks after the "console" because there are much more appendings.
If none of the above solution works, try below one, this worked for me :
Add below property in your application.propertiesfollowing
spring.data.jpa.repositories.bootstrap-mode=default
Open console in browser using this URL : http://localhost:8080/h2-console
On a login page make sure that you use jdbc:h2:mem:testdb as JDBC URL.
As h2 database console is mapped to "h2-console".
Use this:
http.csrf().disable().authorizeRequests()
.antMatchers("/h2-console/**").permitAll()
.anyRequest().authenticated();
// disable frame options
http.headers().frameOptions().disable();
`
You don't need permit root access:
.antMatchers("/") * NOT NEEDED *
I have been fighting with getting resources to work for quite sometime now, and I know there are thousands of similar questions.. Anyway, my condition is this
I will start with my pom.. I might have missed something here?
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ruruapps</groupId>
<artifactId>spring-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-demo</name>
<description>Spring-Demo Application</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.thymeleaf-version>2.1.4</org.thymeleaf-version>
<webjars-bootstrap.version>3.3.5</webjars-bootstrap.version>
<webjars-jquery-ui.version>2.1.1</webjars-jquery-ui.version>
<webjars-jquery.version>2.0.3-1</webjars-jquery.version>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- Thymeleaf -->
<!--<dependency>-->
<!--<groupId>org.thymeleaf</groupId>-->
<!--<artifactId>thymeleaf</artifactId>-->
<!--<version>2.1.4.RELEASE</version>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- Webjars (static dependencies distributed as WAR files) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.5</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>2.1.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Here are the errors I am dealing with
And in the terminal I see this
Looking up handler method for path /css/bootstrap/css/bootstrap.min.css
Did not find handler method for [/css/bootstrap/css/bootstrap.min.css]
Request method 'GET' not supported
What does this mean?
I have a correct structure and doing it by the convention
But no luck what so ever. I tried changing to public/ and tried using webjars. All in vain. What are the norms for this and how do I get this set up so it wont be breaking! Thanks!
EDIT:: Configuration Files
Here is my application file that is provided with the spring boot
#SpringBootApplication
#EnableAutoConfiguration(exclude = {
org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration.class,
org.springframework.boot.actuate.autoconfigure.ManagementSecurityAutoConfiguration.class})
#ComponentScan(basePackages = {"webapp"})
#EnableJpaRepositories
public class SpringDemoApplication {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(SpringDemoApplication.class, args);
}
}
And I have a WebConfig file which is mostly empty, but I tried adding the #Overide on addResourceHandlers, with no improvements
#Configuration
#EnableWebMvc
#ComponentScan(basePackages = {"webapp"})
public class WebConfig extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/resources/static/").setCachePeriod(31556926);
}
}
EDIT
After further examining the application, I have found this
It says that the request it allows is PUT, but why? From what I can understand is that something is blocking my request to the resource files, is that right?
Desperate to get this fixed, it's really bothering to configure Spring, even with Spring Boot!
Found a problem that I was facing with not being able to access static resources.
For those who experience the same:
This behaviour (405 error) signifies that something is blocking the path to your static resources. This doesn't have to be /static or /public. It wont do anything.
In my case the problem was that a controller was blocking the /css path. To fix this a #RequestMapping(value = "/Somepath") annotation on the class lvl is needed to release the path for the static resources.
This worked for me and now I can access my static resources without any issues.
This can be a very annoying problem and someone can spend hours and days trying to figure this out. Hope this helps others to save their time
If there are alternative or other methods of preventing this behaviour that please mention in the comments
If you are trying to configure resource mapping through configuration class as you mentioned in comment, then it should be like this:
#EnableWebMvc
#ComponentScan(basePackages = {"org.company.yourpackage"})
#Configuration
public class AppConfig extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("classpath:/resources/static/").setCachePeriod(31556926);
}
}
this post also may help.
I was having the exact same issue, What helped me was adding:
enter code here
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
OR
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/js/*</url-pattern>
</servlet-mapping>
from this post
servlet for serving static content here