[英]Spring Boot Not Application Not Working For JDBC In Liberty Server
I am new to Spring Boot. 我是Spring Boot的新手。
I am trying to write a rest api which should take data from database and display it to consumer. 我正在尝试编写一个rest api,该api应该从数据库中获取数据并将其显示给使用者。 I don't know how to tackle with this issue. 我不知道该如何解决。
Please help to run the application with JDBC datasource on Liberty Server. 请帮助在Liberty Server上使用JDBC数据源运行应用程序。 Simple spring boot rest service working perfectly fine. 简单的弹簧靴休息服务工作正常。
Please forgive me in case there so much have asked in question. 如果有太多疑问,请原谅我。 I want to learn this. 我想学习这个。
here is my pom file : 这是我的pom文件:
<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.ankit.demo</groupId>
<artifactId>test-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>test-demo</name>
<url>http://maven.apache.org</url>
<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>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</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>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<skip>true</skip>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
DemoService.java DemoService.java
package com.ankit.demo;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = { "com.ankit.demo.*" })
@SpringBootApplication(exclude = MessageSourceAutoConfiguration.class)
@EnableAutoConfiguration(exclude = { HibernateJpaAutoConfiguration.class, JpaRepositoriesAutoConfiguration.class })
public class DemoService extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DemoService.class);
}
}
OracleDatabaseConfiguration.java OracleDatabaseConfiguration.java
package com.ankit.demo.configuration;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;
@Configuration
public class OracleDatabaseConfiguration {
@Value("${spring.Oracle.datasource.jndi-name}")
private String jndiName;
@Bean(destroyMethod = "")
public DataSource OracleDataSource() {
JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
return dataSourceLookup.getDataSource(jndiName);
}
@Bean()
@Qualifier("oracleJdbcTemplate")
public JdbcTemplate jdbcTemplate() {
return new JdbcTemplate(OracleDataSource());
}
}
DemoController.java DemoController.java
package com.ankit.demo.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.ankit.demo.source.IDemoService;
import com.ankit.demo.vo.Employee;
@RestController
public class DemoController {
@Autowired
private IDemoService iDemoService;
@RequestMapping(method=RequestMethod.GET, value="/getEmployee")
public List<Employee> getEmployee() {
return iDemoService.getEmployee();
}
}
DemoDAOImpl.java DemoDAOImpl.java
package com.ankit.demo.respository;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import com.ankit.demo.rowmapper.EmployeeMapper;
import com.ankit.demo.source.IDemoDAO;
import com.ankit.demo.vo.Employee;
@Component
public class DemoDAOImpl implements IDemoDAO {
@Autowired
@Qualifier("oracleJdbcTemplate")
private JdbcTemplate jdbcTemplate;
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public List<Employee> getEmployee() {
String sql = "SELECT * FROM EMPLOYEE";
List<Employee> empLst = null;
try {
empLst = jdbcTemplate.query(sql, new EmployeeMapper());
}catch(Exception ex) {
}
return empLst;
}
}
EmployeeMapper.java EmployeeMapper.java
package com.ankit.demo.rowmapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
import com.ankit.demo.vo.Employee;
public class EmployeeMapper<T> implements RowMapper<Employee>{
@Override
public Employee mapRow(ResultSet rs, int rowNum) throws SQLException {
Employee emp = new Employee();
try {
if(rs != null) {
if(rs.next()) {
emp.setEmpId(rs.getLong("EMP_ID"));
emp.setEmpName(rs.getString("EMP_NAME"));
emp.setAccName(rs.getNString("ACCOUNT_NAME"));
emp.setJoinDate(rs.getDate("JOINING_DATE"));
}
}else {
System.out.println("Result Set is null");
}
}catch(Exception ex) {
ex.printStackTrace();
}finally {
if(rs != null) {
rs.close();
}
}
return emp;
}
}
DemoServiceImpl.java DemoServiceImpl.java
package com.ankit.demo.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.ankit.demo.source.IDemoDAO;
import com.ankit.demo.source.IDemoService;
import com.ankit.demo.vo.Employee;
public class DemoServiceImpl implements IDemoService {
@Autowired
private IDemoDAO iDemoDAO;
@Override
public List<Employee> getEmployee() {
return iDemoDAO.getEmployee();
}
}
Employee.java Employee.java
package com.ankit.demo.vo;
import java.util.Date;
public class Employee {
private long empId;
private String empName;
private String accName;
private Date joinDate;
public long getEmpId() {
return empId;
}
public void setEmpId(long empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getAccName() {
return accName;
}
public void setAccName(String accName) {
this.accName = accName;
}
public Date getJoinDate() {
return joinDate;
}
public void setJoinDate(Date joinDate) {
this.joinDate = joinDate;
}
@Override
public String toString() {
return "Employee [empId=" + empId + ", empName=" + empName + ", accName=" + accName + ", joinDate=" + joinDate
+ "]";
}
}
server.xml server.xml
<server description="new server">
<!-- Enable features -->
<featureManager>
<feature>webProfile-7.0</feature>
<feature>localConnector-1.0</feature>
<feature>jaxb-2.2</feature>
</featureManager>
<!-- To access this server from a remote client add a host attribute to
the following element, e.g. host="*" -->
<httpEndpoint httpPort="9080" httpsPort="9443"
id="defaultHttpEndpoint" />
<!-- Automatically expand WAR files and EAR files -->
<applicationManager autoExpand="true" />
<applicationMonitor updateTrigger="mbean" />
<!-- WEB APPLICATION -->
<webApplication id="test-demo" location="test-demo.war" name="test-demo" />
<!-- ORACLE DATASOURCE -->
<dataSource id="DefaultDataSource" jndiName="jdbc/oracle" connectionSharing="MatchOriginalRequest" isolationLevel="TRANSACTION_READ_UNCOMMITTED" statementCacheSize="20">
<connectionManager maxPoolSize="20" minPoolSize="5" connectionTimeout="30s" agedTimeout="30m"></connectionManager>
<jdbcDriver libraryRef="OracleLib" />
<properties.oracle URL="jdbc:oracle:thin:@//localhost:1521/xe" user="system" password="{xor}PjsyNjE=" />
</dataSource>
<!-- ORACLE JDBC DRIVER LIBRARY -->
<library id="OracleLib">
<file name="E:/LocalRepository/.m2/repository/com/oracle/ojdbc6/11.2.0.3/ojdbc6.jar" />
</library>
</server>
output console 输出控制台
[AUDIT ] CWWKT0016I: Web application available (default_host): http://localhost:9080/test-demo/
[AUDIT ] CWWKZ0003I: The application test-demo updated in 8.963 seconds.
[WARNING ] CWNEN0047W: Resource annotations on the fields of the org.springframework.web.servlet.view.tiles3.TilesConfigurer$CompositeELResolverImpl class will be ignored. The annotations could not be obtained because of the exception : java.lang.NoClassDefFoundError: org/apache/tiles/el/ScopeELResolver
[WARNING ] CWNEN0049W: Resource annotations on the methods of the org.springframework.web.servlet.view.tiles3.TilesConfigurer$CompositeELResolverImpl class will be ignored. The annotations could not be obtained because of the exception : java.lang.NoClassDefFoundError: org/apache/tiles/el/ScopeELResolver
[WARNING ] SRVE0190E: File not found: /getEmployee
I changed my spring-boot-starter-parent version to 1.3.3 and got below exception 我将spring-boot-starter-parent版本更改为1.3.3,并得到以下异常
java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration#propertySourcesPlaceholderConfigurer due to org/springframework/jdbc/core/JdbcTemplate not found. Make sure your own configuration does not rely on that class. This can also 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:55) ~[spring-boot-autoconfigure-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.context.annotation.ConditionEvaluator.shouldSkip(ConditionEvaluator.java:102) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod(ConfigurationClassBeanDefinitionReader.java:178) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:140) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:116) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:333) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:243) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:273) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:98) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:678) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:520) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) ~[spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) ~[spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) ~[spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.context.web.SpringBootServletInitializer.run(SpringBootServletInitializer.java:149) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.context.web.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:129) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.context.web.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:85) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:175) [spring-web-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at com.ibm.ws.webcontainer.webapp.WebApp.initializeServletContainerInitializers(WebApp.java:2490) [com.ibm.ws.webcontainer_1.1.18.jar:na]
at com.ibm.ws.webcontainer.webapp.WebApp.initialize(WebApp.java:1002) [com.ibm.ws.webcontainer_1.1.18.jar:na]
at com.ibm.ws.webcontainer.webapp.WebApp.initialize(WebApp.java:6550) [com.ibm.ws.webcontainer_1.1.18.jar:na]
at com.ibm.ws.webcontainer.osgi.DynamicVirtualHost.startWebApp(DynamicVirtualHost.java:469) [com.ibm.ws.webcontainer_1.1.18.jar:na]
at com.ibm.ws.webcontainer.osgi.DynamicVirtualHost.startWebApplication(DynamicVirtualHost.java:464) [com.ibm.ws.webcontainer_1.1.18.jar:na]
at com.ibm.ws.webcontainer.osgi.WebContainer.startWebApplication(WebContainer.java:1119) [com.ibm.ws.webcontainer_1.1.18.jar:na]
at com.ibm.ws.webcontainer.osgi.WebContainer.access$000(WebContainer.java:103) [com.ibm.ws.webcontainer_1.1.18.jar:na]
at com.ibm.ws.webcontainer.osgi.WebContainer$2.run(WebContainer.java:931) [com.ibm.ws.webcontainer_1.1.18.jar:na]
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [na:1.8.0_151]
at java.util.concurrent.FutureTask.run(Unknown Source) [na:1.8.0_151]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_151]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_151]
at java.lang.Thread.run(Unknown Source) [na:1.8.0_151]
Caused by: java.lang.NoClassDefFoundError: org/springframework/jdbc/core/JdbcTemplate
at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.8.0_151]
at java.lang.Class.privateGetDeclaredMethods(Unknown Source) ~[na:1.8.0_151]
at java.lang.Class.getDeclaredMethods(Unknown Source) ~[na:1.8.0_151]
at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:609) ~[spring-core-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:521) ~[spring-core-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:507) ~[spring-core-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.util.ReflectionUtils.getUniqueDeclaredMethods(ReflectionUtils.java:567) ~[spring-core-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getTypeForFactoryMethod(AbstractAutowireCapableBeanFactory.java:683) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineTargetType(AbstractAutowireCapableBeanFactory.java:627) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:597) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1445) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:975) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.boot.autoconfigure.condition.BeanTypeRegistry$OptimizedBeanTypeRegistry.addBeanTypeForNonAliasDefinition(BeanTypeRegistry.java:289) ~[spring-boot-autoconfigure-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.autoconfigure.condition.BeanTypeRegistry$OptimizedBeanTypeRegistry.addBeanType(BeanTypeRegistry.java:278) ~[spring-boot-autoconfigure-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.autoconfigure.condition.BeanTypeRegistry$OptimizedBeanTypeRegistry.getNamesForType(BeanTypeRegistry.java:259) ~[spring-boot-autoconfigure-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.autoconfigure.condition.OnBeanCondition.collectBeanNamesForType(OnBeanCondition.java:182) ~[spring-boot-autoconfigure-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.autoconfigure.condition.OnBeanCondition.getBeanNamesForType(OnBeanCondition.java:171) ~[spring-boot-autoconfigure-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.autoconfigure.condition.OnBeanCondition.getMatchingBeans(OnBeanCondition.java:139) ~[spring-boot-autoconfigure-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.autoconfigure.condition.OnBeanCondition.getMatchOutcome(OnBeanCondition.java:113) ~[spring-boot-autoconfigure-1.3.3.RELEASE.jar:1.3.3.RELEASE]
at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:47) ~[spring-boot-autoconfigure-1.3.3.RELEASE.jar:1.3.3.RELEASE]
... 31 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.springframework.jdbc.core.JdbcTemplate
at com.ibm.ws.classloading.internal.AppClassLoader.findClassCommonLibraryClassLoaders(AppClassLoader.java:504) ~[na:na]
at com.ibm.ws.classloading.internal.AppClassLoader.findClass(AppClassLoader.java:276) ~[na:na]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.8.0_151]
at com.ibm.ws.classloading.internal.AppClassLoader.findOrDelegateLoadClass(AppClassLoader.java:482) ~[na:na]
at com.ibm.ws.classloading.internal.AppClassLoader.loadClass(AppClassLoader.java:443) ~[na:na]
at java.lang.ClassLoader.loadClass(Unknown Source) ~[na:1.8.0_151]
... 51 common frames omitted
2018-04-17 23:55:39.228 INFO 5692 --- [utor-thread-104] .b.l.ClasspathLoggingApplicationListener : Application failed to start with classpath: unknown
I have resolved the issues which i was facing. 我已经解决了我面临的问题。 Please find below steps which I've done. 请找到以下我已完成的步骤。
I don't know whether this is workaround or exact solution but it really worked for me. 我不知道这是解决方法还是确切的解决方案,但它确实对我有用。
Please correct me in case this is just a workaround. 如果这只是解决方法,请纠正我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.