Issue
I am new to Spring Boot.
I am trying to write a rest api which should take data from database and display it to consumer. I don't know how to tackle with this issue.
Please help to run the application with JDBC datasource on Liberty Server. 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 :
<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
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
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
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
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
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
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
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 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
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
Solution
I have resolved the issues which i was facing. Please find below steps which I've done.
- Added all spring related(spring-core,spring-beans,spring-jdbc, etc.) jar to WEB-INF/lib folder.
- Added @Component Annotations wherever I missed, because application was giving bean not defined for @Autowired.
- Removed 'isolationLevel="TRANSACTION_READ_UNCOMMITTED"' from server.xml. This was earlier set and because of this I was getting 'could not get JDBC connection exception'.
- Added feature 'jndi-1.0','jdbc-4.1','jpa-2.1' in server.xml
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.
Answered By - afulz29