Generating web service service service and dynamically invoking web service service samples

preface

In the actual process, we often need to remotely access the web services provided by the third-party program or actively provide a web service for the third-party system to call, so as to achieve the communication between heterogeneous systems and ensure the effective exchange of data.

1, What is a web service service?

Web service is a development mode that can be used to solve the problem of cross network application integration. The purpose is to ensure that the application services of different platforms can interoperate. So easy, what do we need to know?
1. SOAP: a lightweight protocol for remote procedure call. It is an XML based protocol that allows any type of object or code to communicate with each other in any language on any platform.
2. WSDL: XML language used to describe the communication between Web services and Web services. Tell the user the details of how to call the service.

For a WebService call, we need to know several core parameters of the call (call address, namespace, call method, input parameter type, output parameter type...)

  1. The address of the call is as follows: http://localhost:8080/esb/services/ts?wsdl
    Or http://localhost:8080/esb/services/ts (you need to use "wsdl" or. CLS "wsdl" on the back splice)
    There may be slight differences due to the way of publishing

  2. The namespace is shown in the following figure

  3. There may be multiple called methods and input and output parameters, as shown in the figure below. There are two, getCharge and getCharge1

2, Use steps

1. Introduce dependency (both generate service and invoke service use this dependency)

The code is as follows

  <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-frontend-jaxws</artifactId>
      <version>3.1.12</version>
  </dependency>
  <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http</artifactId>
      <version>3.1.12</version>
  </dependency>
  <dependency>
      <groupId>wsdl4j</groupId>
      <artifactId>wsdl4j</artifactId>
  </dependency>

Generate service (note that the following are multiple files, which I pasted in the same code fragment)

/Entity class
package cn.tx.webservice.demoservice.pojo;

public class User {
    private   String name;
    private  String age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
}


/Interface
package cn.tx.webservice.demoservice;
import cn.tx.webservice.demoservice.pojo.User;

import javax.jws.WebService;

@WebService(name = "ChargeWs", // Exposure service name
        targetNamespace = "www.jiac.com"// Namespace
)

public interface WS {

    //Mark this method as a method exposed by webservice for external publication. The modified method is webservice method
    User getCharge(String msg);
    
    String getCharge1(String msg);

}



//Implementation interface
package cn.tx.webservice.demoservice.impl;

import cn.tx.webservice.demoservice.WS;
import cn.tx.webservice.demoservice.pojo.User;
import org.springframework.stereotype.Component;
import javax.jws.WebService;

/**
 * @ClassName GetChargeImpl
 * @Description TODO
 * @Author jiac
 * @Date 2021/11/10 14:19
 * @Version 1.0
 **/

@WebService(serviceName = "ChargeWs",
        targetNamespace = "www.jiac.com", //Consistent with targetNamespace in the interface
        endpointInterface = "cn.tx.webservice.demoservice.WS")
@Component

public class GetChargeImpl implements WS {
    
    @Override
    public User getCharge(String msg) {
       User user =new User();
       user.setName("jiac");
       user.setAge("26");
       return user;
    }

    @Override
    public String getCharge1(String msg) {
        return "Receive the message";

    }


}

/Configuration class
package cn.tx.webservice.demoservice.config;

import cn.tx.webservice.demoservice.WS;
import cn.tx.webservice.demoservice.impl.GetChargeImpl;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.xml.ws.Endpoint;



/**
 * @ClassName CxfConfig
 * @Description TODO
 * @Author jiac
 * @Date 2021/11/10 14:23
 * @Version 1.0
 **/


@Configuration
public class CxfConfig {

    @Bean("dispatcherServletCxf")
    public ServletRegistrationBean wsDispatcherServlet(){
        return new ServletRegistrationBean(new CXFServlet(),"/esb/services/*");//Publishing service name
    }

    @Bean(name = Bus.DEFAULT_BUS_ID)
    public SpringBus springBus() {
        return new SpringBus();
    }
    
    @Bean
    public WS webService() {
        return new GetChargeImpl();
    }

    //Terminal path
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(springBus(),webService());
        endpoint.publish("/ts");
        return endpoint;
    }

    @Bean
    public Endpoint endpoint1() {
        EndpointImpl endpoint = new EndpointImpl(springBus(),webService());
        endpoint.publish("/tst");
        return endpoint;
    }
    
}

2. Call service

The code is as follows:

package cn.tx.webservice;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import javax.xml.namespace.QName;
import java.io.BufferedReader;
import java.io.FileReader;


/**
 * @ClassName TestCXF
 * @Description TODO
 * @Author jiac
 * @Date 2021/11/10 21:21
 * @Version 1.0
 **/

public class TestCXF {
    public static void main(String[] args) throws Exception {

        StringBuffer sbf = new StringBuffer();
        BufferedReader bufferedReader=new BufferedReader(new FileReader("D:\\MAVEN\\tx_boot\\springboot_first\\src\\AdviceInfomationAdd.xml"));
        String  xml="";
        while ( (xml=(bufferedReader.readLine()))!=null){
            sbf.append(xml);
        }

        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://localhost:8080/esb/services/ts?wsdl");
        System.out.println(sbf);

        QName qName=new QName("www.jiac.com","getCharge1");
        long t1 = System.currentTimeMillis();
        Object[] invoke =client.invoke(qName,sbf.toString());
        long t2 = System.currentTimeMillis();
        String string = invoke[0].toString();
        System.out.println(string);
        System.out.println(t2-t1);  //Calculate the communication duration
    }
}

The effect is as follows

summary

Generally summarize and record the basic process of my study. I hope it will be helpful to you. Mainly understand the related meaning of webservice, the dependent packages required when generating and invoking services, the configuration classes and related attributes need to be loaded when generating services, and the file description of wsdl after generating services. Generally, the file description is read up below, or the address is loaded directly through the test tool (soapui) for dynamic invocation, You need to specify the calling service address, namespace, and specific method.

Tags: Java xml server

Posted on Fri, 12 Nov 2021 01:18:09 -0500 by ruddernz