java - Spring Autowiring Web 服务的问题

标签 java spring web-services

我正在创建网络服务,并且必须在其中使用服务。但自动连线不起作用,我尝试了很多东西。

我的目录结构是:

package com.mycaptionlabs.quickbooks.ws;

import java.util.ArrayList;

import javax.jws.WebService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;

import com.mycaptionlabs.repository.UserRepository;
import com.mycaptionlabs.service.UserService;

/*
 * http://developer.intuit.com/qbsdk-current/doc/pdf/qbwc_proguide.pdf
 */

@WebService(endpointInterface = "com.mycaptionlabs.quickbooks.ws.QBWebConnectorSvcSoap")
public class ItemQueryRqSoapImpl extends SpringBeanAutowiringSupport implements QBWebConnectorSvcSoap {

    UserRepository userRepository;

    @Autowired UserService userService;

    @Override
    public ArrayOfString authenticate(String strUserName, String strPassword) {
        System.out.println(userService);

        ArrayOfString arr = new ArrayOfString();
        arr.string = new ArrayList<String>();
        arr.string.add("The first element is a token for the web connectorÕs session");
        arr.string.add(""); // To use the currently open company, specify an
                            // empty string
        return arr;
    }

    @Override
    public String closeConnection(String ticket) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String connectionError(String ticket, String hresult, String message) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String getLastError(String ticket) {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * @return A positive integer less than 100 represents the percentage of
     *         work completed. A value of 1 means one percent complete, a value
     *         of 100 means 100 percent complete--there is no more work. A
     *         negative value means an error has occurred and the Web Connector
     *         responds to this with a getLastError call. The negative value
     *         could be used as a custom error code.
     */
    @Override
    public int receiveResponseXML(String ticket, String response, String hresult, String message) {
        // TODO Auto-generated method stub
        return 100;
    }

    @Override
    public String sendRequestXML(String ticket, String strHCPResponse, String strCompanyFileName, String qbXMLCountry,
            int qbXMLMajorVers, int qbXMLMinorVers) {
        // Example qbXML to Query for an Item
        // http://www.consolibyte.com/wiki/doku.php/quickbooks_qbxml_itemquery
        String query = "<?xml version=\"1.0\" encoding=\"utf-8\"?><?qbxml version=\"7.0\"?><QBXML><QBXMLMsgsRq onError=\"stopOnError\"><ItemQueryRq requestID=\"SXRlbVF1ZXJ5fDEyMA==\"><OwnerID>0</OwnerID></ItemQueryRq></QBXMLMsgsRq></QBXML>";
        return query;
    }

}

界面是

package com.mycaptionlabs.quickbooks.ws;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;


/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.1.6 in JDK 6
 * Generated source version: 2.1
 * 
 */
@WebService(name = "QBWebConnectorSvcSoap", targetNamespace = "http://developer.intuit.com/")
@XmlSeeAlso({
    ObjectFactory.class
})
public interface QBWebConnectorSvcSoap {


    /**
     * 
     * @param strPassword
     * @param strUserName
     * @return
     *     returns com.cantero.quickbooks.ws.ArrayOfString
     */
    @WebMethod(action = "http://developer.intuit.com/authenticate")
    @WebResult(name = "authenticateResult", targetNamespace = "http://developer.intuit.com/")
    @RequestWrapper(localName = "authenticate", targetNamespace = "http://developer.intuit.com/", className = "com.cantero.quickbooks.ws.Authenticate")
    @ResponseWrapper(localName = "authenticateResponse", targetNamespace = "http://developer.intuit.com/", className = "com.cantero.quickbooks.ws.AuthenticateResponse")
    public ArrayOfString authenticate(
        @WebParam(name = "strUserName", targetNamespace = "http://developer.intuit.com/")
        String strUserName,
        @WebParam(name = "strPassword", targetNamespace = "http://developer.intuit.com/")
        String strPassword);

    /**
     * 
     * @param qbXMLMinorVers
     * @param ticket
     * @param qbXMLCountry
     * @param strCompanyFileName
     * @param qbXMLMajorVers
     * @param strHCPResponse
     * @return
     *     returns java.lang.String
     */
    @WebMethod(action = "http://developer.intuit.com/sendRequestXML")
    @WebResult(name = "sendRequestXMLResult", targetNamespace = "http://developer.intuit.com/")
    @RequestWrapper(localName = "sendRequestXML", targetNamespace = "http://developer.intuit.com/", className = "com.cantero.quickbooks.ws.SendRequestXML")
    @ResponseWrapper(localName = "sendRequestXMLResponse", targetNamespace = "http://developer.intuit.com/", className = "com.cantero.quickbooks.ws.SendRequestXMLResponse")
    public String sendRequestXML(
        @WebParam(name = "ticket", targetNamespace = "http://developer.intuit.com/")
        String ticket,
        @WebParam(name = "strHCPResponse", targetNamespace = "http://developer.intuit.com/")
        String strHCPResponse,
        @WebParam(name = "strCompanyFileName", targetNamespace = "http://developer.intuit.com/")
        String strCompanyFileName,
        @WebParam(name = "qbXMLCountry", targetNamespace = "http://developer.intuit.com/")
        String qbXMLCountry,
        @WebParam(name = "qbXMLMajorVers", targetNamespace = "http://developer.intuit.com/")
        int qbXMLMajorVers,
        @WebParam(name = "qbXMLMinorVers", targetNamespace = "http://developer.intuit.com/")
        int qbXMLMinorVers);

    /**
     * 
     * @param message
     * @param response
     * @param ticket
     * @param hresult
     * @return
     *     returns int
     */
    @WebMethod(action = "http://developer.intuit.com/receiveResponseXML")
    @WebResult(name = "receiveResponseXMLResult", targetNamespace = "http://developer.intuit.com/")
    @RequestWrapper(localName = "receiveResponseXML", targetNamespace = "http://developer.intuit.com/", className = "com.cantero.quickbooks.ws.ReceiveResponseXML")
    @ResponseWrapper(localName = "receiveResponseXMLResponse", targetNamespace = "http://developer.intuit.com/", className = "com.cantero.quickbooks.ws.ReceiveResponseXMLResponse")
    public int receiveResponseXML(
        @WebParam(name = "ticket", targetNamespace = "http://developer.intuit.com/")
        String ticket,
        @WebParam(name = "response", targetNamespace = "http://developer.intuit.com/")
        String response,
        @WebParam(name = "hresult", targetNamespace = "http://developer.intuit.com/")
        String hresult,
        @WebParam(name = "message", targetNamespace = "http://developer.intuit.com/")
        String message);

    /**
     * 
     * @param message
     * @param ticket
     * @param hresult
     * @return
     *     returns java.lang.String
     */
    @WebMethod(action = "http://developer.intuit.com/connectionError")
    @WebResult(name = "connectionErrorResult", targetNamespace = "http://developer.intuit.com/")
    @RequestWrapper(localName = "connectionError", targetNamespace = "http://developer.intuit.com/", className = "com.cantero.quickbooks.ws.ConnectionError")
    @ResponseWrapper(localName = "connectionErrorResponse", targetNamespace = "http://developer.intuit.com/", className = "com.cantero.quickbooks.ws.ConnectionErrorResponse")
    public String connectionError(
        @WebParam(name = "ticket", targetNamespace = "http://developer.intuit.com/")
        String ticket,
        @WebParam(name = "hresult", targetNamespace = "http://developer.intuit.com/")
        String hresult,
        @WebParam(name = "message", targetNamespace = "http://developer.intuit.com/")
        String message);

    /**
     * 
     * @param ticket
     * @return
     *     returns java.lang.String
     */
    @WebMethod(action = "http://developer.intuit.com/getLastError")
    @WebResult(name = "getLastErrorResult", targetNamespace = "http://developer.intuit.com/")
    @RequestWrapper(localName = "getLastError", targetNamespace = "http://developer.intuit.com/", className = "com.cantero.quickbooks.ws.GetLastError")
    @ResponseWrapper(localName = "getLastErrorResponse", targetNamespace = "http://developer.intuit.com/", className = "com.cantero.quickbooks.ws.GetLastErrorResponse")
    public String getLastError(
        @WebParam(name = "ticket", targetNamespace = "http://developer.intuit.com/")
        String ticket);

    /**
     * 
     * @param ticket
     * @return
     *     returns java.lang.String
     */
    @WebMethod(action = "http://developer.intuit.com/closeConnection")
    @WebResult(name = "closeConnectionResult", targetNamespace = "http://developer.intuit.com/")
    @RequestWrapper(localName = "closeConnection", targetNamespace = "http://developer.intuit.com/", className = "com.cantero.quickbooks.ws.CloseConnection")
    @ResponseWrapper(localName = "closeConnectionResponse", targetNamespace = "http://developer.intuit.com/", className = "com.cantero.quickbooks.ws.CloseConnectionResponse")
    public String closeConnection(
        @WebParam(name = "ticket", targetNamespace = "http://developer.intuit.com/")
        String ticket);

}

主类是:

包 com.mycaptionlabs;

import javax.xml.ws.Endpoint;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.PropertySources;
import org.springframework.scheduling.annotation.EnableScheduling;

import com.mycaptionlabs.quickbooks.ws.ItemQueryRqSoapImpl;

@SpringBootApplication
@EnableScheduling
@PropertySources({
    @PropertySource("classpath:datasource.properties"),
    @PropertySource("classpath:mail.properties")
})
public class MyCaptionLabsApplication extends SpringBootServletInitializer{ 

    public static Logger log = LoggerFactory.getLogger(MyCaptionLabsApplication.class);
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(MyCaptionLabsApplication.class);
        }   
    public static void main(String[] args) {
        log.debug("running my Application");        
        SpringApplication.run(MyCaptionLabsApplication.class, args);
        Endpoint.publish("http://localhost:8080/QBwebService",
                new ItemQueryRqSoapImpl());

    }
}

我的用户服务是

@Service
public class UserService{

}

我也尝试过此链接JBoss 6 + Spring 3.0.5 + JAX-WS/CXF它对我不起作用。请帮我解决我所缺少的内容,目录结构是否有错误?

提前致谢!!

最佳答案

@Autowired(如@Value、@Inject等)仅适用于Spring管理的bean。但是您手动创建 ItemQueryRqSoapImpl 的实例。

  1. 将@Component或@Service添加到ItemQueryRqSoapImpl类声明中,或创建一个返回ItemQueryRqSoapImpl实例并用@Bean注释的方法。

  2. 使用ApplicationContext.getBean()获取创建的bean并将其传递给Endpoint.publish()

你应该得到这样的东西:

    public static Logger log = LoggerFactory.getLogger(MyCaptionLabsApplication.class);

    @Bean
    public ItemQueryRqSoapImpl itemQueryRqSoapImpl() {
        return new ItemQueryRqSoapImpl();
    }

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

    public static void main(String[] args) {
        log.debug("running my Application");        
        ApplicationContext applicationContext = SpringApplication.run(MyCaptionLabsApplication.class, args);
        ItemQueryRqSoapImpl itemQueryRqSoapImpl = applicationContext.getBean(ItemQueryRqSoapImpl.class);

        Endpoint.publish("http://localhost:8080/QBwebService",
            itemQueryRqSoapImpl);

}

关于java - Spring Autowiring Web 服务的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35062651/

相关文章:

Spring 4+方面

spring - 从返回 future 的服务创建 Mono/Flux 的正确方法

database - RESTful URI 是否应该公开数据库主键?

java - 使用可变参数安全吗?

java - 将 java 集合保存在数据库中

java - Spring PUT 未映射到方法

python - 在 Python 中生成 JSON 模式

windows - 如何使用 Windows 身份验证的 Web 服务避免使用 Delphi HTTPRio 调用的登录对话框

java - 如何验证 JUnit 测试中抛出的异常的详细信息?

java - java中使用正则表达式解析字符串