I am completely a beginner at Spring (you can see that in my code :) ). I just wanted to test the class RestTemplate but I got a ClassNotFoundException.
So the code is:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.client.RestTemplate;
public class RestClient {
private RestTemplate restTemplate;
public String getJiraIssueAsJson(){
Object o = restTemplate.getForObject(..., Object.class);
System.out.println("..."+o.getClass());
return null;
}
public void setRestTemplate(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("rest-client-context.xml");
RestClient restClient = context.getBean("restClient", RestClient.class);
restClient.getJiraIssueAsJson();
}
}
context.xml
<beans ...>
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
</list>
</property>
</bean>
<bean id="restClient" class="org.googlecode.happymarvin.jiraexplorer.RestClient">
<property name="restTemplate" ref="restTemplate"/>
</bean>
</beans>
pom.xml
<project ...>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.googlecode.happymarvin</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>jiraminer</artifactId>
<name>Happy Marvin JIRA Miner</name>
<packaging>jar</packaging>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jackson-version>1.9.13</jackson-version>
</properties>
</project>
parent pom.xml
<project ...>
<modelVersion>4.0.0</modelVersion>
<groupId>com.googlecode.happymarvin</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Happy Marvin parent project</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<org.springframework.version>4.0.0.RELEASE</org.springframework.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
</dependencies>
</project>
Exception
Jan 07, 2014 10:18:24 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext#730eb2f0: startup date [Tue Jan 07 10:18:24 GMT 2014]; root of context hierarchy
Jan 07, 2014 10:18:24 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [rest-client-context.xml]
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restTemplate' defined in class path resource [rest-client-context.xml]: Cannot create inner bean 'org.springframework.http.converter.json.MappingJacksonHttpMessageConverter#77624896' of type [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter] while setting bean property 'messageConverters' with key [0]; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter] for bean with name 'org.springframework.http.converter.json.MappingJacksonHttpMessageConverter#77624896' defined in class path resource [rest-client-context.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.http.converter.json.MappingJacksonHttpMessageConverter
...
Caused by: java.lang.ClassNotFoundException: org.springframework.http.converter.json.MappingJacksonHttpMessageConverter
I have this exception when I try to run the main method from eclipse.
I can think of something like the spring jars cannot be seen but I don't know why...
Can you please help me?
The first major version of Jackson is no longer supported in Spring 4. The class you want to use is now
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter. Make sure that you have com.fasterxml.jackson.core/jackson-core/2.x.x on your classpath.
I faced same issue. Fixed using org.springframework.http.converter.json.MappingJackson2HttpMessageConverter instead of org.springframework.http.converter.json.MappingJacksonHttpMessageConverter
i tried to copy paste your spring beans to a project but something strange is wrong with
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
this line, specifically there seems to be some invisible characters before the last r in Converter try typing that classname again manually.
if this is the case then its the craziest thing i have seen for sometime :D
Also MappingJacksonHttpMessageConverter is deprecated in 4.0.0 there is something newer. And you will need to add dependencies for jackson as well to get things working. This should be helpful.
You need to add the following to your pom.xml (not parent pom.xml)
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
</dependencies>
I had the issue, got it fixed by adding following dependencies in pom.xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>
</dependency>
Found it here!
Programmaticaly you can do your configuration like this:
public class AppConfiguration {
...
#Bean
public HttpMessageConverters customConverters() {
HttpMessageConverter<?> jacksonMessageConverter = new MappingJackson2HttpMessageConverter();
// HttpMessageConverter<?> another = ...
return new HttpMessageConverters(jacksonMessageConverter);
}
}
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>
</dependency>
it's worked for me
Related
I am trying to start my Spring server but keep getting this error:
Description:
Parameter 0 of constructor in com.rm.awsimageupload.filestore.FileStore required a bean of type 'com.amazonaws.services.s3.AmazonS3' that could not be found.
Action:
Consider defining a bean of type 'com.amazonaws.services.s3.AmazonS3' in your configuration.
I have tried:
pasting in the keys as strings right into the BasicAWSCredentials
putting the keys into application.properties and then using #Value to access them. With this approach, I get this error:
Error creating bean with name 'amazonConfig': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'local.AWS_ACCESS_KEY_ID' in value "${local.AWS_ACCESS_KEY_ID}"
application.properties
AWS_ACCESS_KEY_ID=NOTAREALKEY
AWS_SECRET_ACCESS_KEY=NOTAREALSECRET
FileStore.java
#Configuration
public class AmazonConfig {
#Value("${application.AWS_ACCESS_KEY_ID}")
private String AWS_ACCESS_KEY_ID;
#Value("${application.AWS_SECRET_ACCESS_KEY}")
private String AWS_SECRET_ACCESS_KEY;
public AmazonS3 S3() {
AWSCredentials awsCredentials = new BasicAWSCredentials(
AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY
);
return AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCredentials)).withRegion(Regions.SA_EAST_1).build();
}
}
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.rm</groupId>
<artifactId>aws-image-upload</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>aws-image-upload</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.11.787</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Not exactly sure what to do... I am following a tutorial on how to do this and my config pretty much matches this instructor's. Any suggestions?
Your are missing #Bean annotation on public AmazonS3 S3()
#Bean
public AmazonS3 S3() {
AWSCredentials awsCredentials = new BasicAWSCredentials(
AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY
);
return AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCredentials)).withRegion(Regions.SA_EAST_1).build();
}
Regarding the 2nd issue for properties values, not sure if this the typo here
local.AWS_ACCESS_KEY_ID in logs vs application.AWS_ACCESS_KEY_ID in code sample.
You need to use the same keys name as in your properties file
So you config class should be having
#Value("${AWS_ACCESS_KEY_ID}")
private String AWS_ACCESS_KEY_ID;
#Value("${AWS_SECRET_ACCESS_KEY}")
private String AWS_SECRET_ACCESS_KEY;
I am trying to inject a groovy class as a bean but getting a java.io.FileNotFoundException exception.
Reference: Spring in Action, 2ed
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'coconut' defined in class path resource [aspects.xml]: Cannot resolve reference to bean 'lime' while setting bean property 'lime'; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'lime': BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'lime': Could not determine scripted object type for GroovyScriptFactory: script source locator [classpath:band.Lime.groovyy]; nested exception is java.io.FileNotFoundException: class path resource [band.Lime.groovy] cannot be opened because it does not exist
My Groovy File - Lime.groovy:
class Lime implements band.Lime{
void drink(){
print "You drink Lime!"}
}
My pom.xml file:
<?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>
<properties></properties>
<groupId>com.gmail#qwertygoog</groupId>
<artifactId>RockWithAspects</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.codehaus.groovy/groovy-all -->
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>3.0.0</version>
<type>pom</type>
</dependency>
</dependencies>
</project>
So, any advice?
upd.-here is class of Coconut
public class Coconut {
private Lime lime;
public Coconut() {
}
public void drinkFast(){
System.out.println("You drink coconut ...");
lime.drink();
}
#Autowired
public void setLime(Lime lime) {
this.lime = lime;
}
}
And my XML is like:
<?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:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/lang
http://www.springframework.org/schema/lang/spring-lang.xsd
"
>
<bean id="coconut" class="band.Coconut">
<!-- <property name="lime" ref="lime"/>-->
</bean>
<lang:groovy id="lime"
script-source="classpath:Lime.groovy"
/>
</beans>
My project is using MySQL, JavaFX, Spring Boot, Spring Data JP and Hibernate frameworks/technologies.
This is my POM file.
<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.nubeclick</groupId>
<artifactId>pos</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>POSNubeClick</name>
<description>Sistema de punto de venta (Point Of Sale).</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<slf4j.version>1.7.12</slf4j.version>
<log4j.version>1.2.17</log4j.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.miglayout</groupId>
<artifactId>miglayout-javafx</artifactId>
<version>5.0</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!-- <dependency> -->
<!-- <groupId>org.slf4j</groupId> -->
<!-- <artifactId>slf4j-api</artifactId> -->
<!-- <version>${slf4j.version}</version> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!-- <groupId>org.slf4j</groupId> -->
<!-- <artifactId>jcl-over-slf4j</artifactId> -->
<!-- <version>${slf4j.version}</version> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!-- <groupId>org.slf4j</groupId> -->
<!-- <artifactId>slf4j-log4j12</artifactId> -->
<!-- <version>${slf4j.version}</version> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!-- <groupId>log4j</groupId> -->
<!-- <artifactId>log4j</artifactId> -->
<!-- <version>${log4j.version}</version> -->
<!-- </dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.jfoenix</groupId>
<artifactId>jfoenix</artifactId>
<version>1.10.0</version>
</dependency>
<dependency>
<groupId>de.jensd</groupId>
<artifactId>fontawesomefx</artifactId>
<version>8.9</version>
</dependency>
<dependency>
<groupId>org.controlsfx</groupId>
<artifactId>controlsfx</artifactId>
<version>8.40.13</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
This is my .properties configuration.
spring.main.banner-mode=off
# Datasource connection properties
spring.datasource.url=jdbc:mysql://localhost/posnubeclick
spring.datasource.username=nubeclick
spring.datasource.password=nubeclick
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# JPA Properties
spring.jpa.database=posnubeclick
# Hibernate Configuration Properties
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.current_session_context_class=thread
spring.jpa.properties.hibernate.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
# Naming strategy
spring.jpa.hibernate.naming-strategy =org.hibernate.cfg.ImprovedNamingStrategy
#Turn Statistics on
spring.jpa.properties.hibernate.generate_statistics=true
# logging
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n
logging.level.org.hibernate.stat=debug
logging.level.org.hibernate.type=trace
logging.level.org.hibernate.SQL=debug
#logging.level.org.hibernate.type.descriptor.sql=trace
logging.level.=error
This is my main class
#SpringBootApplication(scanBasePackages = { "com.nubeclick.pos" })
public class MainApp extends Application {
private static final Logger log = LoggerFactory.getLogger(MainApp.class);
public static void main(String[] args) throws Exception {
SpringApplication.run(MainApp.class, args);
// launch(args);
}
#Override
public void start(Stage stage) throws Exception {
try {
log.info("Starting Hello JavaFX and Maven demonstration application");
String fxmlFile = "/fxml/Main.fxml";
log.debug("Loading FXML for main view from: {}", fxmlFile);
FXMLLoader loader = new FXMLLoader();
Parent rootNode = (Parent) loader.load(getClass().getResourceAsStream(fxmlFile));
log.debug("Showing JFX scene");
Scene scene = new Scene(rootNode);
scene.getStylesheets().add("/styles/styles.css");
stage.setTitle("NubeClick - Point of Sales");
stage.setScene(scene);
stage.show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
This is the stacktrace:
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
The Spring message is this:
Description:
Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified and no embedded datasource could be auto-configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
I have referred to this other post and this other one
I don't know what else to do, maybe start all over again from zero but I want to adapt those frameworks to my current project.
What can I do to solve this?
Crazy thing, I decided to upload the project to another repository, did a whole clean to the project folder (deleted .project, .settings, .classpath, bin, target) and reimported project into eclipse, did configure -> add maven nature, and now the error is gone, at least this error, got some other errors but now it loads everything from the properties file, so, why did this happen?
Links to docs would be appreciated, so I can understand what I have to do.
Maybe Spring Boot is not locating your application.properties file at all. Ensure you have this file in the root of the resources folder (in a typical Maven project configuration, it should by located in the src/main/resources folder root).
I suggest you to enable the DEBUG logging level to check if Spring Boot is reading the correct application.properties file.
Make sure that you have mysql dependency in your pom.xml or build.gradle file:
compile 'mysql:mysql-connector-java'
or
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
Then you can comment out your driver-class-name property, because spring boot will autoconfigure it for you.
One more thing: It seems that you should connect to concrete port to mysql (by default it is 3306), so make sure that it looks similar to this:
spring.datasource.url=jdbc:mysql://localhost:3306/posnubeclick
This is what I would do if we were sitting next to each other, comment out a couple lines:
# Datasource connection properties
spring.datasource.url=jdbc:mysql://localhost/posnubeclick
spring.datasource.username=nubeclick
spring.datasource.password=nubeclick
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
# JPA Properties
#spring.jpa.database=posnubeclick
Also is your .properties file called application.properties OR bootstrap.properties?
I decided to upload the project to another repository, did a whole clean to the project folder (deleted .project, .settings, .classpath, bin, target) and reimported project into eclipse, did configure -> add maven nature, and now the error is gone, at least this error, got some other errors but now it loads everything from the properties file, so, Why did this happened?
I'm learning Spring from this tutorial:
http://courses.caveofprogramming.com/courses/the-java-spring-tutorial/lectures/38024
In this tutorial, instructor downloads spring dependencies (spring-beans, spring context, spring-core) in version 3.2.3.RELEASE.
and then writes this code:
package com.caveofprogramming.spring.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class App {
public static void main(String[] args) {
ApplicationContext context = new FileSystemXmlApplicationContext("beans.xml");
Person person = (Person)context.getBean("person");
person.speak();
}
}
When I use: spring-context, spring-beans and spring-core in last version 4.3.3.RELEASE then ApplicationContext import doesn't work. It works when I change it to the old version. "Doesn't work" means that eclips doesn't know what I should import when I write "ApplicationContext context = new FileSystemXmlApplicationContext("beans.xml");" and when I write "import org.springframework.context.ApplicationContext" by myself it's underline.
What should I do to import ApplicationContext with newest version dependencies?
Edit:
This is the error:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
ApplicationContext cannot be resolved to a type
FileSystemXmlApplicationContext cannot be resolved to a type
at com.caveofprogramming.spring.test.App.main(App.java:10)
and this is my pom file:
<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.caveofprogramming.spring.test</groupId>
<artifactId>spring-tutorial-5</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.3.RELEASE</version>
</dependency>
</dependencies>
</project>
Edit 2: I also saw that program accepts 4.1.1.RELEASE version. Mabye the newest version of dependencies isn't necessary? I'm just starting with Spring and everyone says that I should work on the newest version.
Edit 3;
The only solution which I found is using spring-context 4.1.1.RELEASE
Either add these jars in your class path org.springframework.context.support-3.1.0.RELEASE.jar and org.springframework.context-3.1.0.RELEASE.jar
Or add this dependency if you are using maven and update the project.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.x.x.RELEASE</version>
</dependency>
You have to add dependency
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.x.x</version>
</dependency>
This might be a problem with eclipse, especially if you are using an older version. Eclipse used to have issues with Maven projects, when you added new dependencies.
You can force Eclipse to update it's Maven dependencies by right clicking your project and selecting Maven->Update project
After that your project should compile just fine:
PS: Check the current documentation for up-to-date setup of XML-based application context setup
I added this in my pom.xml file inside the dependencies tag.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.3.7</version>
<type>pom</type>
</dependency>
What it does is that it downloads the spring-context-5.3.7.jar file from which ApplicationContext class can be imported.
Hope it works for u too.
Here is my pom.xml file for better reference.
<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.Sharma</groupId>
<artifactId>NachoVarga</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>NachoVarga</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.7</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.3.7</version>
<type>pom</type>
</dependency>
</dependencies>
</project>
I generated a project on start.spring.io with the following project dependencies:
Jersey (JAX-RS)
JPA
PostgreSQL
Web
When I try to access localhost:8080/homeroom/webapi/test I get a page with the following error:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
I get this error no matter what url I try to access.
In my console info I can see it print Mapping servlet: 'jerseyServlet' to [/webapi/*] so I know my config class is being registered. When I change #ApplicationPath("/webapi") to #ApplicationPath("/"), a GET on localhost:8080/homeroom/test or localhost:8080/homeroom/ returns a blank page instead, with no text or error.
Why can't I access my resource?
This is my 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.homeroomed</groupId>
<artifactId>homeroom</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>HomeRoom</name>
<description>HomeRoom REST API</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.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-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1201-jdbc41</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I'm trying to do a descriptor-less deploy so I have:
#Component
#ApplicationPath("/webapi")
public class MyJerseyConfig extends ResourceConfig{
public MyJerseyConfig() {
// String packageName =TestJerseyResource.class.getPackage().getName();
// this.packages(packageName);
this.register(TestJerseyResource.class);
// System.out.println("The package is:"+packageName);
}
}
And I have the following resource:
#Component
#Path("/test")
public class TestJerseyResource {
#GET
#Produces(MediaType.APPLICATION_JSON)
public String getTest()
{
return "Hi!";
}
}
I'm running the project from:
#SpringBootApplication
public class HomeRoomApplication {
public static void main(String[] args) {
SpringApplication.run(HomeRoomApplication.class, args);
}
}
UPDATE:
So 2 things:
I had to use spring annotations #Controller and #RequestMapping instead of Jersey's #PATH and #GET, and had to GET /webapi/test instead of /homeroom/webapi/test
The reason you can't access your resource from looking at the information you provided is you there is no /homeroom anywhere I saw in your code.
This is a valid URL for your project:
http://localhost:8080/webapi/test
If you wanted homeroom to be in the URL you could change the application path value to homeroom instead of webapi.
Well, quoting from #ApplicationPath JavaDoc:
May only be applied to a subclass of Application.
https://docs.oracle.com/javaee/6/api/javax/ws/rs/ApplicationPath.html
That might be part of the problem...