我有一个Spring Rest WS,然后需要实现myBatis到这个。我试过但是得到了错误。
以下是我的源代码。
从web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>SpringServiceJsonSample</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>rest</servlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/ApplicationContext.xml
</param-value>
</context-param>
</web-app>
下一个ApplicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="myurl" />
<property name="username" value="myacc" />
<property name="password" value="myacc" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="/WEB-INF/mybatis-config.xml" />
</bean>
<bean id="cardValidationService" class="com.programmingfree.springservice.dao.CardValidationService">
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
</beans>
下一步,mybatis-config.xml
<configuration>
<typeAliases>
<typeAlias alias="CardNo" type ="com/programmingfree/springservice/domain/CardNo" />
</typeAliases>
<mappers>
<mapper resource="com/programmingfree/springservice/domain/CardNoMapper.xml"/>
</mappers>
</configuration>
然后在域名CardNo.java中(删除getter和setter以使帖子更短)
public String cardNo;
public String email;
Date dob;
然后CardNoMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com/programmingfree/springservice/domain/CardNo">
<resultMap id="cardNoMap" type="CardNo">
<id property="cardNo" column="CARD_NO" javaType="String" jdbcType="VARCHAR"/>
<result property="email" column="EMAIL" javaType="String" jdbcType="VARCHAR"/>
<result property="dob" column="DOB" javaType="java.util.Date" jdbcType="DATE"/>
</resultMap>
<select id="getCardNoBy" parameterType="map" resultMap="cardNoMap">
select * from user where CARD_NO=#{cardNo}
</select>
</mapper>
在控制器中:
@RestController
@RequestMapping("/service/cardValidate/")
public class CardValidationController {
@Autowired CardValidationService cardService;
@RequestMapping(value = "/{cardNumber}", method = RequestMethod.GET, headers = "Accept=application/json")
public String getCard(@PathVariable String cardNumber) {
String card = cardService.getCardNoBy(cardNumber);
return card;
}
}
最后一个是CardValidationService
:
@Service
public class CardValidationService {
private SqlSessionFactory sqlSessionFactory = null;
public CardValidationService() {
}
public CardValidationService(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
}
public String getCardNoBy(String cardNo) {
SqlSession session = sqlSessionFactory.openSession();
System.out.println(cardNo);
String name = null;
try {
name = (String) session
.selectOne(
"com.programmingfree.springservice.domain.CardNo.getCardNoBy",
cardNo);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
} finally {
session.close();
}
return name;
}
}
然后我跑了,我收到了一堆错误:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:943)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:822)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:807)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
我搜索了这些错误,他们建议在
中添加@Autowired
@Autowired CardValidationService cardService = new CardValidationService();
然后得到另一个错误:
javax.servlet.ServletException: Servlet.init() for servlet rest threw exception
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2463)
org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2452)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:745)
更新2
我不确定是否将xml文件放在正确的文件夹中。 wex.xml, ApplicationContext.xml, mybatis-config.xml, rest-servlet.xml
位于同一WEB-INF
dir
答案 0 :(得分:0)
请试试这个:
a)在控制器类
上@Autowired
CardValidationService cardValidationService;
b)在服务类
上@Service
public class CardValidationService {
c)对于惯例,请将类CardNo的所有实例变量设为私有。
您获得NullPointerException的原因是您在控制器类中创建了一个简单的CardValidationService Java bean,它没有注入任何依赖项,因为您还没有使用Spring创建的bean。你还没有在你的spring配置中将你的CardValidationService声明为bean,所以即使spring也不会将该类创建为spring-bean。