An old project of the company defines interfaces for other applications to access. The way of definition is webservice.
My environment is springboot.
First of all Introduce dependent jar
Declare a server. The name in the @WebSerevice annotation is the service exposed to the outside.
The method annotated by @WebMethod is the method exposed to the 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";
}
}
Configure 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;
}
}
For client testing, replace the port with your own 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 Object[0];
try {
// invoke("method name", parameter 1, parameter 2 ,Parameter 3...);
objects = client.invoke("getWaybillYT", "111111","222222222","3333333333");
System.out.println("Return data:" + objects[0]);
} catch (java.lang.Exception e) {
e.printStackTrace();
}
}
}
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";
}
}
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;
}
}
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 Object[0];
try {
// invoke("method name", parameter 1, parameter 2 ,Parameter 3...);
objects = client.invoke("getWaybillYT", "111111","222222222","3333333333");
System.out.println("Return data:" + objects[0]);
} catch (java.lang.Exception e) {
e.printStackTrace();
}
}
}