Prabjot Singh
Prabjot Singh

Reputation: 4767

Spring autowired issue with webservice

I am creating webservice and i have to use service in it. but there autowired is not working,i tried lot of things for it.

My directory structure is:

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

}

and interface is

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

}

main class is:

package 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());

    }
}

And my user service is

@Service
public class UserService{

}

I have also tried this link JBoss 6 + Spring 3.0.5 + JAX-WS/CXF it doesn't work for me. Please help me what i am missing there,is there any directory structure wrong ?

Thanks in advance!!

Upvotes: 0

Views: 980

Answers (1)

@Autowired (as like as @Value, @Inject, etc.) works only in Spring managed beans. But you create an instance of ItemQueryRqSoapImpl manually.

  1. Add @Component or @Service to ItemQueryRqSoapImpl class declaration, or create a method returning instance of ItemQueryRqSoapImpl and annotated by @Bean.

  2. Get created bean using ApplicationContext.getBean() and pass it to Endpoint.publish()

You shoud get something like this:

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

}

Upvotes: 3

Related Questions