Record webservice

The company's an old project, defines the interface for accessing other applications. Defined way is webservice.

My side of the environment is springboot.

First introduced dependence jar

    

 

Declare a server. @WebSerevice annotation name is exposed to the external service.

@WebMethod be annotated method, the method is exposed outside webservice.

import com.alibaba.fastjson.JSON;
import com.prologint.waybill.api.ParameterEntity.GetWaybillYTRequest;
import com.prologint.waybill.api.ParameterEntity.GetWaybillYTResponse;
import com.prologint.waybill.service.GetWaybillYTService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService(name = "WaybillYT")
@Component
public class WaybillYTService {


  @WebMethod public String getWaybillYT(String billNo, String branchId, String consignor) { return "ok"; } }

Configuration webservice.

import javax.xml.ws.Endpoint;

import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class WaybillYTServiceConfig {
    @Autowired
    private Bus bus;

    @Autowired
    private WaybillYTService waybillYTService;

    /** JAX-WS **/
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus, waybillYTService);
        endpoint.publish("/WaybillYT");
        return endpoint;
    }
}

Client test, the port can be replaced for your server port

Import org.apache.cxf.endpoint.Client;
 Import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; 

public  class cilent {
     public  static  void main (String [] args) {
         // create a dynamic client 
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance (); 
        Client Client = dcf.createClient ( "HTTP: // localhost:? 80 / Services / WaybillYT WSDL" ); 
        Object [] Objects = new new Object [0 ];
         the try {
             // Invoke ( "method name "parameter 1, parameter 2, parameter 3 ....);
            objects = client.invoke("getWaybillYT", "111111","222222222","3333333333");
            System.out.println("返回数据:" + objects[0]);
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/zumengjie/p/11497177.html
Recommended