使用webservice

时间:2016-01-28 13:19:24

标签: java spring web-services

我正在创建webservice,我必须在其中使用服务。但是自动装配不起作用,我为此尝试了很多东西。

我的目录结构是:

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它对我不起作用。请帮助我,我在那里缺少什么,是否有任何目录结构错误?

提前致谢!!

1 个答案:

答案 0 :(得分:3)

@Autowired(就像@Value,@ Inject等)仅适用于Spring托管bean。但是您手动创建ItemQueryRqSoapImpl的实例。

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

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

  3. 你应该得到这样的东西:

        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);
    
    }