当我为JAX WS类添加@Autowire注释时,它在spring boot中给出了空指针异常错误
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了当我为JAX WS类添加@Autowire注释时,它在spring boot中给出了空指针异常错误相关的知识,希望对你有一定的参考价值。
我为我的Web应用程序编写了一个JAX-WS服务,当我调用Web服务时,它会给出空指针异常(Autowire无法正常工作)...我将tomcat 8.5.11作为服务器...
当我删除@WebService注释时,自动装配工作正常....但是当我添加它时它不起作用...错误是什么...它是错误变成我的依赖...
这是我的依赖关系。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
</dependency>
<!--handle servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!--<Email Dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>1.4.3.RELEASE</version>
<exclusions>
<exclusion>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--Add mysql dependency-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.1</version>
</dependency>
<!--jasper-->
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>3.7.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.xml.ws/jaxws-rt -->
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.2.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jvnet.staxex/stax-ex -->
<dependency>
<groupId>org.jvnet.staxex</groupId>
<artifactId>stax-ex</artifactId>
<version>1.7.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.xml.stream.buffer/streambuffer -->
<dependency>
<groupId>com.sun.xml.stream.buffer</groupId>
<artifactId>streambuffer</artifactId>
<version>1.5.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-libs -->
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-libs</artifactId>
<version>1.0.6</version>
</dependency>
我的模型用@Entity注释注释..我不是把它放在这里......
我的存储库是
package lk.slsi.repository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List;
import lk.slsi.domain.CustomsPermit;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
@Qualifier("PermitRepository")
public interface PermitRepository extends CrudRepository<CustomsPermit, Long> {
@Query("select a from CustomsPermit a where a.dtIssue = :dtIssue")
List<CustomsPermit> getPermitByDate(@Param("dtIssue") String dtIssue);
}
我的WS课程是
package lk.slsi.repository;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jws.WebService;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import lk.slsi.domain.CustomsPermit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
/**
*
* @author lankadeva.ghg
*/
@WebService
@Component
public class permitRepoImpl extends SpringBeanAutowiringSupport {
@Autowired
@Qualifier("PermitRepository")
private PermitRepository permitRepository;
public static void marshaling() throws JAXBException {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(CustomsPermit.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
} catch (JAXBException ex) {
Logger.getLogger(permitRepoImpl.class.getName()).log(Level.SEVERE, null, ex);
}
}
public List<CustomsPermit> getPermitByDate(String dtIssue) {
try {
System.out.println("autowire is "+ permitRepository);
marshaling();
} catch (JAXBException ex) {
Logger.getLogger(permitRepoImpl.class.getName()).log(Level.SEVERE, null, ex);
System.out.println("exception : autowire is "+ permitRepository);
return (List<CustomsPermit>) ex;
}
return permitRepository.getPermitByDate(dtIssue);
}
}
Autowire在这里不起作用......当我进入调试模式时,它将null作为值
这是我的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>SLSIonNationalSingleWindow</display-name>
<servlet-mapping>
<servlet-name>permitRepoImplService</servlet-name>
<url-pattern>/permitRepoImplService</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/view/index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>spring-web</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:slsi-servlet-config.xml</param-value>
</init-param>
</servlet>
<servlet>
<servlet-name>permitRepoImplService</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-web</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
这是我的applicationconfig.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- Scan Config -->
<context:component-scan base-package="lk.slsi" />
<!--<mvc:annotation-driven/>-->
<mvc:default-servlet-handler/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/view/" />
<property name="suffix" value=".jsp" />
</bean>
<!--<bean id = "permitRepository" class="lk.slsi.repository.PermitRepository" autowire="byName" autowire-candidate="true">
</bean> -->
</beans>
我的依赖注入中的错误是什么....
答案
你能尝试添加这个依赖:jaxws-spring库集成了两个框架:
<dependency>
<groupId>org.jvnet.jax-ws-commons.spring</groupId>
<artifactId>jaxws-spring</artifactId>
<version>1.9</version>
</dependency>
请参阅此链接以获取更多信息:https://examples.javacodegeeks.com/enterprise-java/jws/jax-ws-spring-integration-example/
以上是关于当我为JAX WS类添加@Autowire注释时,它在spring boot中给出了空指针异常错误的主要内容,如果未能解决你的问题,请参考以下文章
JAX-WS 和 CDI 不能在 WAS Liberty Profile 8.5.5.6 上一起工作