【问题标题】:Is the JAX-WS needs special dependency for insert spring @autowiredJAX-WS 是否需要特殊依赖才能插入弹簧 @autowired
【发布时间】:2018-07-24 04:05:01
【问题描述】:

为我的项目编写了一个 JAX-WS Web 服务。我使用 @autowired 注入一个类...然后 Web 服务返回空指针异常...我进入调试模式并验证空指针在@autowired cord... 在 JAX-RS 中使用 @autowired 需要特殊注解吗..

这是我的网络服务类

package lk.slsi.webService;

import java.util.List;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.ws.rs.PathParam;
import lk.slsi.domain.CustomsPermit;
import lk.slsi.services.permitServices;
import org.springframework.beans.factory.annotation.Autowired;


@WebService(serviceName = "customsPermit",name = "permitRelease",portName = "nswPort",targetNamespace = "https://nationalsinglewindow.gov.lk/SLSIonNationalSingleWindow/")
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT)
public class webServiceforCustoms {

    @Autowired
    private permitServices permitServices;


    @WebMethod
    public List<CustomsPermit> getXmlbyDate(@WebParam(name = "123") String dtIssue) {
        List<CustomsPermit> permitRelease = permitServices.getPermitByDate(dtIssue);
        return permitRelease;
    }

    @WebMethod
    public CustomsPermit getXmlbyEntryNo(String SNumber) {
        CustomsPermit permitRelease = permitServices.getPermitBySNumber(SNumber);
        return permitRelease;
    }

    @WebMethod
    public List<CustomsPermit> getXmlbyVATNo(String importerVAT) {
        List<CustomsPermit> permitRelease = permitServices.getPermitByImporterVAT(importerVAT);
        return permitRelease;
    }
}

这是我的服务类

package lk.slsi.services;

import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.util.List;
import javax.inject.Inject;
import lk.slsi.domain.CustomsPermit;
import lk.slsi.repository.PermitRepository;
import lk.slsi.security.domain.AuthenticatedUser;
import org.springframework.security.core.context.SecurityContextHolder;

@Service
@Scope("session")
public class permitServices{

    private static final Logger serviceLogger = LogManager.getLogger(permitServices.class);

    @Autowired
    private PermitRepository permitRepository;

    public boolean registerPermit(@Valid CustomsPermit customsPermit) {
        serviceLogger.info("Starting to register new Agent. agent : [{}]", customsPermit);
        try {
            Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            long userId = 0;
            String agency = "";
            String units = "";
            List<String> roles;
            if (principal != null && principal instanceof AuthenticatedUser) {
                AuthenticatedUser auth = (AuthenticatedUser) principal;
                userId = auth.getUserId();
                agency = auth.getAgency();
                roles = auth.getUserRoles();

                String pattern = "yyyy-MM-dd HH:mm:ss";
                String pattern2 = "yyyy-MM-dd";
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
                SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat(pattern2);
                String date = simpleDateFormat.format(new Date());
                String date2 = simpleDateFormat2.format(new Date());
                String userandTime = "UserID " + Long.toString(userId) + " in " + date;
                customsPermit.setAppPostdate(userandTime);
                customsPermit.setUserID(Long.toString(userId));
                customsPermit.setDtIssue(date2);
                permitRepository.save(customsPermit);
                serviceLogger.info("Successfully saved the agent in db");
                return true;
            }
        } catch (Exception e) {
            serviceLogger.error("Error occurred while registering agent. [{}]", e);
        }
        return false;
    }

    public CustomsPermit getFromId(Long id) {
        serviceLogger.info("Fetching the agent by nic number : [{}]", id);
        try {
            return permitRepository.getFromId(id);
        } catch (Exception e) {
            serviceLogger.error("Error while retrieving the permit for id number [{}], [{}]", id, e);
            return null;
        }
    }

    public List<CustomsPermit> findAll() {
        serviceLogger.info("Fetching Permit List");
        return permitRepository.findAll();
    }

    public List<CustomsPermit> getPermitByImporterVAT(String importerVAT) {
        serviceLogger.info("Fetching Permit List");
        return permitRepository.getPermitByImporterVAT(importerVAT);
    }

    public List<CustomsPermit> getPermitByDate(String dtIssue) {
        serviceLogger.info("Fetching Permit List");
        return permitRepository.getPermitByDate(dtIssue);
    }

    public CustomsPermit getPermitBySNumber(String SNumber) {
        return permitRepository.getPermitBySNumber(SNumber);
    }
}

这是我的存储库类

package lk.slsi.repository;

import lk.slsi.domain.Agent;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;
import lk.slsi.domain.CustomsPermit;

public interface PermitRepository extends CrudRepository<CustomsPermit, Long> {

    @Override
    CustomsPermit save(CustomsPermit customsPermit);

    @Override
    CustomsPermit findOne(Long id);

    @Override
    List<CustomsPermit> findAll();


    @Query("select a from CustomsPermit a where a.id = :id")
    CustomsPermit getFromId(@Param("id") Long id);

    @Query("select a from CustomsPermit a where a.SNumber = :SNumber")
    CustomsPermit getPermitBySNumber(@Param("SNumber") String SNumber);

    @Query("select a from CustomsPermit a where a.importerVAT = :importerVAT")
    List<CustomsPermit> getPermitByImporterVAT(@Param("importerVAT") String importerVAT);

    @Query("select a from CustomsPermit a where a.dtIssue = :dtIssue")
    List<CustomsPermit> getPermitByDate(@Param("dtIssue") String dtIssue);
}

这是我的 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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>lk.slsi</groupId>
    <artifactId>SLSIonNationalSingleWindow</artifactId>
    <packaging>war</packaging>
    <version>slsi-1.1.0-SNAPSHOT</version>
    <name>SLSIonNationalSingleWindow Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <scm>
        <connection>scm:git:git@bitbucket.org:mof_SriLanka/SLSIonNationalSingleWindow.git</connection>
        <url>scm:git:git@bitbucket.org:mof_SriLanka/SLSIonNationalSingleWindow.git</url>
        <developerConnection>scm:git:git@bitbucket.org:mof_SriLanka/SLSIonNationalSingleWindow.git</developerConnection>
        <tag>1.0.0</tag>
    </scm>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
    </parent>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <sonar.exclusions>**/public/**/*</sonar.exclusions>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--<dependency>-->
        <!--<groupId>org.springframework.boot</groupId>-->
        <!--<artifactId>spring-boot-starter-tomcat</artifactId>-->
        <!--<scope>provided</scope>-->
        <!--</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>      
        </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>

        <!--jasper-->
        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>3.7.6</version>
            <scope>compile</scope>
        </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>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>javax.mail-api</artifactId>
            <version>1.5.5</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.bundles</groupId>
            <artifactId>jaxrs-ri</artifactId>
            <version>2.25</version>
        </dependency>
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.0.1</version>
            <type>jar</type>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-moxy -->
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
            <version>2.25</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.ext/jersey-spring3 -->
        <dependency>
            <groupId>org.glassfish.jersey.ext</groupId>
            <artifactId>jersey-spring4</artifactId>
            <version>2.26</version>
        </dependency>


        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib-nodep</artifactId>
            <version>2.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.sun.xml.ws/jaxws-ri-bom-ext -->
        <dependency>
            <groupId>com.sun.xml.ws</groupId>
            <artifactId>jaxws-ri-bom-ext</artifactId>
            <version>2.2.10</version>
            <type>pom</type>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.xml.ws/jaxws-api -->
        <dependency>
            <groupId>javax.xml.ws</groupId>
            <artifactId>jaxws-api</artifactId>
            <version>2.2.10</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.glassfish.jersey.bundles.repackaged/jersey-guava -->
        <dependency>
            <groupId>org.glassfish.jersey.bundles.repackaged</groupId>
            <artifactId>jersey-guava</artifactId>
            <version>2.26-b03</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>         
        <!-- https://mvnrepository.com/artifact/javax.activation/activation -->
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.metro</groupId>
            <artifactId>webservices-rt</artifactId>
            <version>2.3</version>
            <scope>compile</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.github.jsr-330/core -->
        <dependency>
            <groupId>com.github.jsr-330</groupId>
            <artifactId>core</artifactId>
            <version>1.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.ws</groupId>
            <artifactId>jaxws-rt</artifactId>
            <version>2.2.8</version>
        </dependency>
        <dependency>
            <groupId>org.jvnet.jax-ws-commons.spring</groupId>
            <artifactId>jaxws-spring</artifactId>
            <version>1.9</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-core</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-context</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.xml.stream.buffer</groupId>
                    <artifactId>streambuffer</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.jvnet.staxex</groupId>
                    <artifactId>stax-ex</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
    <build>
        <finalName>SLSIonNationalSingleWindow</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <useSystemClassLoader>false</useSystemClassLoader>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
                <version>2.5.2</version>
                <configuration>
                    <tagNameFormat>@{project.version}</tagNameFormat>
                    <autoVersionSubmodules>true</autoVersionSubmodules>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

这是我的配置类

package lk.slsi;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import javax.annotation.PreDestroy;

/**
 * Created by ignotus on 1/22/2017.
 */
@SpringBootApplication
@ComponentScan(basePackages = "lk.slsi")
@EnableWebMvc
@ImportResource(locations = "classpath:slsi-servlet-config.xml")
public class SLSIStarter extends SpringBootServletInitializer {

    private static final Logger slsiLogger = LogManager.getLogger(SLSIStarter.class);

    private static ConfigurableApplicationContext context;

    public static void main(String[] args) {
        slsiLogger.info("Starting application");
        SpringApplication application = new SpringApplication(SLSIStarter.class);
        context = application.run(args);
        application.setRegisterShutdownHook(true);
    }

    @PreDestroy
    private static void closeAppContext(){
        context.close();
    }

    @Override
    protected final SpringApplicationBuilder configure(final SpringApplicationBuilder application) {
        return application.sources(SLSIStarter.class);
    }
}

我的电源线出了什么问题。

【问题讨论】:

  • 错误来了,因为 1. PermitRepository 是一个接口。 2.你没有将它声明为spring bean,通过使用Service或Component注解,考虑扩展PermitRepository并将该类标记为@Service,你的代码应该可以正常工作。
  • @bestwishes 我该怎么做...我是春天的新手...请修改电源线

标签: web-services spring-mvc spring-boot jax-ws


【解决方案1】:

创建另一个这样的类

@Service
public class PermitRepositoryImpl implements PermitRepository {

    @Override
    CustomsPermit save(CustomsPermit customsPermit){
      //implement the code, returning null for now
      return null;
    }

    @Override
    CustomsPermit findOne(Long id){
      //implement the code, returning null for now
      return null;
    }

    @Override
    List<CustomsPermit> findAll(){
      //implement the code, returning null for now
      return null;
    }


    @Query("select a from CustomsPermit a where a.id = :id")
    CustomsPermit getFromId(@Param("id") Long id){
      //implement the code, returning null for now
      return null;
    }

    @Query("select a from CustomsPermit a where a.SNumber = :SNumber")
    CustomsPermit getPermitBySNumber(@Param("SNumber") String SNumber){
      //implement the code, returning null for now
      return null;
    }

    @Query("select a from CustomsPermit a where a.importerVAT = :importerVAT")
    List<CustomsPermit> getPermitByImporterVAT(@Param("importerVAT") String importerVAT){
      //implement the code, returning null for now
      return null;
    }

    @Query("select a from CustomsPermit a where a.dtIssue = :dtIssue")
    List<CustomsPermit> getPermitByDate(@Param("dtIssue") String dtIssue){
      //implement the code, returning null for now
      return null;
    }
}

添加此类应该可以解决您的问题。

【讨论】:

  • 我放了这个类..然后它返回错误消息调用实现所有抽象方法..我放了它,它给出了很大的错误。
  • 错误是什么?,你实现了所有的方法吗?
  • 是的,我实现了所有方法......但是当我导入它时,它也提供了删除方法......我的存储库没有删除方法
  • 你不必实现所有这些,只要实现你能做的就可以避免错误。
  • 我的意思是实现返回null,不填//implement the code, returning null for now就可以了
猜你喜欢
  • 1970-01-01
  • 2011-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-02
相关资源
最近更新 更多