java Spring启动无法启动rest服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28988374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring boot failing to start rest service
提问by Sivasakthi Jayaraman
I tried to simulate the RESTful Web Service from the spring home page "https://spring.io/guides/gs/rest-service/". I am able to get the expected output for the given example, so i did a little enhancement i.e tried to fetch some count from the oracle database and display as part of response but it didn't work.
我尝试从 spring 主页“ https://spring.io/guides/gs/rest-service/”模拟 RESTful Web Service 。我能够获得给定示例的预期输出,所以我做了一些改进,即尝试从 oracle 数据库中获取一些计数并作为响应的一部分显示,但它没有工作。
Maven compilation works fine but when i run the spring boot i am getting the below error, i don't know what could be the reason. I am new to spring, can someone help me?
Maven 编译工作正常,但是当我运行 Spring Boot 时出现以下错误,我不知道可能是什么原因。我是春天的新手,有人可以帮助我吗?
My configuration details are
Mac 10.9
EclipseLuna 4.4.1
Java-8
maven3.2.1
spring 3.6
我的配置细节是
Mac 10.9
EclipseLuna 4.4.1
Java-8
maven3.2.1
spring 3.6
Spring Boot Error:
春季启动错误:
:: Spring Boot :: (v1.2.2.RELEASE)
2015-03-11 18:36:25.862 INFO 7298 --- [ main]
com.test.Application : Starting Application on LM-MAA-00668611 with PID 7298 (/Users/sijayaraman/Documents/workspace/luna/CopyofSpringRest/target/classes started by sijayaraman in /Users/sijayaraman/Documents/workspace/luna/CopyofSpringRest)
2015-03-11 18:36:25.900 INFO 7298 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5427c60c: startup date [Wed Mar 11 18:36:25 IST 2015]; root of context hierarchy
2015-03-11 18:36:26.366 INFO 7298 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'beanNameViewResolver': replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2015-03-11 18:36:26.405 INFO 7298 --- [ main] .b.l.ClasspathLoggingApplicationListener : Application failed to start with classpath: [file:/Users/sijayaraman/Documents/workspace/luna/CopyofSpringRest/target/classes/, file:/Users/sijayaraman/.m2/maven.repo/org/springframework/boot/spring-boot-starter-web/1.2.2.RELEASE/spring-boot-starter-web-1.2.2.RELEASE.jar, file:/Users/sijayaraman/.m2/maven.repo/org/springframework/boot/spring-boot-starter/1.2.2.RELEASE/spring-boot-starter-1.2.2.RELEASE.jar, file:/Users/sijayaraman/.m2/maven.repo/org/springframework/boot/spring-boot/1.2.2.RELEASE/spring-boot-1.2.2.RELEASE.jar, file:/Users/sijayaraman/.m2/maven.repo/org/springframework/boot/spring-boot-autoconfigure/1.2.2.RELEASE/spring-boot-autoconfigure-1.2.2.RELEASE.jar, file:/Users/sijayaraman/.m2/maven.repo/org/springframework/boot/spring-boot-starter-logging/1.2.2.RELEASE/spring-boot-starter-logging-1.2.2.RELEASE.jar, file:/Users/sijayaraman/.m2/maven.repo/org/slf4j/jcl-over-slf4j/1.7.10/jcl-over-slf4j-1.7.10.jar, file:/Users/sijayaraman/.m2/maven.repo/org/slf4j/slf4j-api/1.7.10/slf4j-api-1.7.10.jar, file:/Users/sijayaraman/.m2/maven.repo/org/slf4j/jul-to-slf4j/1.7.10/jul-to-slf4j-1.7.10.jar, file:/Users/sijayaraman/.m2/maven.repo/org/slf4j/log4j-over-slf4j/1.7.10/log4j-over-slf4j-1.7.10.jar, file:/Users/sijayaraman/.m2/maven.repo/ch/qos/logback/logback-classic/1.1.2/logback-classic-1.1.2.jar, file:/Users/sijayaraman/.m2/maven.repo/ch/qos/logback/logback-core/1.1.2/logback-core-1.1.2.jar, file:/Users/sijayaraman/.m2/maven.repo/org/yaml/snakeyaml/1.14/snakeyaml-1.14.jar, file:/Users/sijayaraman/.m2/maven.repo/org/springframework/boot/spring-boot-starter-tomcat/1.2.2.RELEASE/spring-boot-starter-tomcat-1.2.2.RELEASE.jar, file:/Users/sijayaraman/.m2/maven.repo/org/apache/tomcat/embed/tomcat-embed-core/8.0.20/tomcat-embed-core-8.0.20.jar, file:/Users/sijayaraman/.m2/maven.repo/org/apache/tomcat/embed/tomcat-embed-el/8.0.20/tomcat-embed-el-8.0.20.jar, file:/Users/sijayaraman/.m2/maven.repo/org/apache/tomcat/embed/tomcat-embed-logging-juli/8.0.20/tomcat-embed-logging-juli-8.0.20.jar, file:/Users/sijayaraman/.m2/maven.repo/org/apache/tomcat/embed/tomcat-embed-websocket/8.0.20/tomcat-embed-websocket-8.0.20.jar, file:/Users/sijayaraman/.m2/maven.repo/com/fasterxml/Hymanson/core/Hymanson-databind/2.4.5/Hymanson-databind-2.4.5.jar, file:/Users/sijayaraman/.m2/maven.repo/com/fasterxml/Hymanson/core/Hymanson-annotations/2.4.5/Hymanson-annotations-2.4.5.jar, file:/Users/sijayaraman/.m2/maven.repo/com/fasterxml/Hymanson/core/Hymanson-core/2.4.5/Hymanson-core-2.4.5.jar, file:/Users/sijayaraman/.m2/maven.repo/org/hibernate/hibernate-validator/5.1.3.Final/hibernate-validator-5.1.3.Final.jar, file:/Users/sijayaraman/.m2/maven.repo/javax/validation/validation-api/1.1.0.Final/validation-api-1.1.0.Final.jar, file:/Users/sijayaraman/.m2/maven.repo/org/jboss/logging/jboss-logging/3.1.3.GA/jboss-logging-3.1.3.GA.jar, file:/Users/sijayaraman/.m2/maven.repo/com/fasterxml/classmate/1.0.0/classmate-1.0.0.jar, file:/Users/sijayaraman/.m2/maven.repo/org/springframework/spring-core/4.1.5.RELEASE/spring-core-4.1.5.RELEASE.jar, file:/Users/sijayaraman/.m2/maven.repo/org/springframework/spring-web/4.1.5.RELEASE/spring-web-4.1.5.RELEASE.jar, file:/Users/sijayaraman/.m2/maven.repo/org/springframework/spring-aop/4.1.5.RELEASE/spring-aop-4.1.5.RELEASE.jar, file:/Users/sijayaraman/.m2/maven.repo/aopalliance/aopalliance/1.0/aopalliance-1.0.jar, file:/Users/sijayaraman/.m2/maven.repo/org/springframework/spring-context/4.1.5.RELEASE/spring-context-4.1.5.RELEASE.jar, file:/Users/sijayaraman/.m2/maven.repo/org/springframework/spring-webmvc/4.1.5.RELEASE/spring-webmvc-4.1.5.RELEASE.jar, file:/Users/sijayaraman/.m2/maven.repo/org/springframework/spring-expression/4.1.5.RELEASE/spring-expression-4.1.5.RELEASE.jar, file:/Users/sijayaraman/.m2/maven.repo/org/springframework/spring-jdbc/4.1.5.RELEASE/spring-jdbc-4.1.5.RELEASE.jar, file:/Users/sijayaraman/.m2/maven.repo/org/springframework/spring-beans/4.1.5.RELEASE/spring-beans-4.1.5.RELEASE.jar, file:/Users/sijayaraman/.m2/maven.repo/org/springframework/spring-tx/4.1.5.RELEASE/spring-tx-4.1.5.RELEASE.jar, file:/Users/sijayaraman/.m2/maven.repo/com/oracle/ojdbc5/11.2.0.1.0/ojdbc5-11.2.0.1.0.jar]
2015-03-11 18:36:26.411 ERROR 7298 --- [ main] o.s.boot.SpringApplication : Application startup failed
java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration#transactionManager due to internal class not found. This can happen if you are @ComponentScanning a springframework package (e.g. if you put a @ComponentScan in the default package by mistake)
at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:51)
at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:102)
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod(ConfigurationClassBeanDefinitionReader.java:194)
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:148)
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:124)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:318)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:239)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:254)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:94)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:606)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:462)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at com.test.Application.main(Application.java:11)
Caused by: java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2688)
at java.lang.Class.getDeclaredMethods(Class.java:1962)
at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:571)
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:490)
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:474)
at org.springframework.util.ReflectionUtils.getUniqueDeclaredMethods(ReflectionUtils.java:534)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.java:677)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:621)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:591)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1397)
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:968)
at org.springframework.boot.autoconfigure.condition.BeanTypeRegistry$OptimizedBeanTypeRegistry.addBeanTypeForNonAliasDefinition(BeanTypeRegistry.java:257)
at org.springframework.boot.autoconfigure.condition.BeanTypeRegistry$OptimizedBeanTypeRegistry.addBeanType(BeanTypeRegistry.java:246)
at org.springframework.boot.autoconfigure.condition.BeanTypeRegistry$OptimizedBeanTypeRegistry.getNamesForType(BeanTypeRegistry.java:227)
at org.springframework.boot.autoconfigure.condition.OnBeanCondition.collectBeanNamesForType(OnBeanCondition.java:158)
at org.springframework.boot.autoconfigure.condition.OnBeanCondition.getBeanNamesForType(OnBeanCondition.java:147)
at org.springframework.boot.autoconfigure.condition.OnBeanCondition.getMatchingBeans(OnBeanCondition.java:119)
at org.springframework.boot.autoconfigure.condition.OnBeanCondition.getMatchOutcome(OnBeanCondition.java:83)
at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:45)
... 16 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.aopalliance.aop.Advice
at java.net.URLClassLoader.run(URLClassLoader.java:372)
at java.net.URLClassLoader.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 36 common frames omitted
2015-03-11 18:36:26.422 INFO 7298 --- [ main] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5427c60c: startup date [Wed Mar 11 18:36:25 IST 2015]; root of context hierarchy
2015-03-11 18:36:26.424 WARN 7298 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception thrown from ApplicationListener handling ContextClosedEvent
java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5427c60c: startup date [Wed Mar 11 18:36:25 IST 2015]; root of context hierarchy
at org.springframework.context.support.AbstractApplicationContext.getApplicationEventMulticaster(AbstractApplicationContext.java:344)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:331)
at org.springframework.context.support.AbstractApplicationContext.doClose(AbstractApplicationContext.java:869)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.doClose(EmbeddedWebApplicationContext.java:150)
at org.springframework.context.support.AbstractApplicationContext.close(AbstractApplicationContext.java:836)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:342)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at com.test.Application.main(Application.java:11)
2015-03-11 18:36:26.424 WARN 7298 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception thrown from LifecycleProcessor on context close
Application.java
应用程序.java
package com.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Employee.java
雇员.java
package com.test;
public class Employee {
private final int count;
private final String name;
public Employee(int count,String name) {
this.count = count;
this.name = name;
}
public int getCount() {
return count;
}
public String getName() {
return name;
}
}
EmployeeController.java
员工控制器.java
package com.test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class EmployeeController {
@Autowired
private EmployeeDao dao;
@RequestMapping("/employee")
public Employee employee(@RequestParam(value="name") String name) {
System.out.println("Count="+dao.getTotalCount());
return new Employee(dao.getTotalCount(),name);
}
}
EmployeeDao.java
员工道.java
package com.test;
public interface EmployeeDao {
public int getTotalCount();
}
EmployeeDaoImpl.java
EmployeeDaoImpl.java
package com.test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
@Component
public class EmployeeDaoImpl implements EmployeeDao{
@Autowired
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public int getTotalCount(){
@SuppressWarnings("deprecation")
int numOfTrades = jdbcTemplate.queryForInt("select count(*) from employee");
return numOfTrades;
}
}
application.properties
应用程序属性
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@testserver.com:2126:QADBA9FU
spring.datasource.username=test
spring.datasource.password=password123
pom.xml
pom.xml
<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>org.springframework</groupId>
<artifactId>gs-rest-service</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc5</artifactId>
<version>11.2.0.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>
回答by Eddú Meléndez
If you are using spring-boot, add the dependency below in order to use autoconfiguration
如果您使用的是 spring-boot,请添加以下依赖项以使用自动配置
pom.xml
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
In your application.properties
在您的 application.properties 中
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@testserver.com:2126:QADBA9FU
spring.datasource.username=test
spring.datasource.password=password123
EDIT 1
编辑 1
Remove this in yout EmployeeController
在你的 EmployeeController 中删除它
ApplicationContext ctx = new FileSystemXmlApplicationContext("/Users/sijayaraman/Documents/workspace/luna/CopyofSpringRest/sbeans.xml");
EmployeeDao dao=(EmployeeDaoImpl)ctx.getBean("employeedao");
instead use
而是使用
@Autowired
private EmployeeDao dao;
Also, annotate class
另外,注释类
@Component
public class EmployeeDaoImpl ...
EDIT 02
编辑 02
Download the ojdbc6.jar from http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.htmland install in your maven local repository
从http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html下载 ojdbc6.jar并安装在您的 maven 本地存储库中
mvn install:install-file -Dfile={Path/to/your/ojdbc.jar} -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0 -Dpackaging=jar
Then change your oracle dependency by
然后通过以下方式更改您的 oracle 依赖项
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
</dependency>
回答by jny
It might not be the root of your problem, but you are not accessing your dao correctly. The main principle of Spring is Dependency Injection, which means Controller
should not be getting it's DAO. Instead it should be either autowired or set in the config file.
它可能不是您问题的根源,但您没有正确访问您的 dao。Spring 的主要原则是依赖注入,这意味着Controller
不应该获取它的 DAO。相反,它应该自动装配或在配置文件中设置。
Then you Controller
would look like this:
然后你Controller
看起来像这样:
@RestController
public class EmployeeController {
@Autowired
EmployeeDao dao;
@RequestMapping("/employee")
public Employee employee(@RequestParam(value="name") String name) {
System.out.println("Count="+dao.getTotalCount());
return new Employee(dao.getTotalCount(),name);
}
}
You will need to add the sbeans.xml
to the configuration. Try adding
@ImportXml("classpath:sbeans.xml")
to Application.java
:
您需要sbeans.xml
在配置中添加。尝试添加
@ImportXml("classpath:sbeans.xml")
到Application.java
:
@SpringBootApplication
@ImportXml("classpath:sbeans.xml")
public class Application
or better yet rewrite sbeans.xml
in Java.
或者更好的是sbeans.xml
用 Java重写。
回答by farrellmr
You are getting -
你得到 -
Caused by: java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice
引起:java.lang.NoClassDefFoundError:org/aopalliance/aop/Advice
So fix this first with -
所以首先解决这个问题 -
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>