Call WebService interface

Last level we talked about generating maven project.
In this episode, we talk about generating JavaBeans and String loaded Bean tool classes.
1. Add test class dependency

		<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

2. Before I thought about writing a Bean class according to the return interface of the interface, but because the return interface is too complex, I don't want to write it (in fact, I usually give up when I write it, which is too difficult for me)...
Normally, I need to write one manually, but in line with the principle that I would rather eat ash than soil, I Baidu created a jar package to generate javaBean class in reverse (don't ask why baidu, because it's my principle that Baidu won't be the only one).
Get it yourself:
Link: https://pan.baidu.com/s/1cgHyk1opJMJVpJI0UD47ew
Extraction code: vdck

3. After getting the jar package, prepare the XML file. Write a test class.

URL url = new URL("http://www.webxml.com.cn/webservices/ChinaStockSmallImageWS.asmx?wsdl");
        QName qName = new QName("http://WebXml.com.cn/", "ChinaStockSmallImageWS");
        Service service = Service.create(url, qName);
        ChinaStockSmallImageWSSoap passengerInfoWebServiceSoap = service.getPort(ChinaStockSmallImageWSSoap.class);
        String xmlString = passengerInfoWebServiceSoap.getSmallImage("Interface parameters",);

Parameter resolution:
URL: the WSDL address provided by the other party
NameSpaceURI: following screenshot description
LocalPart: it can be consistent with the one before. asmx (personal understanding)
NameSpaceURI:

Get the xml file through Debugger in the code. You can format it online, and then paste it into the text to save it as a. xml file.

4,cmd
Command: Java jar tang.jar tour.xml tour.xsd


5. After generating the XSD file, use xjc to parse the java class.
Command: xjc tour.xsd -p tour.java


Just put these java classes into your project.
6. The above steps are to generate JavaBeans. Next, parse the String string.
The resolved tool class code is as follows:

public class XmlToJavaBeanUtils {

    @SuppressWarnings("unchecked")
    public static Object xmlToBean1(String xmlString, Class<?> load) {
        Object object = null;

        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(load);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
            object = unmarshaller.unmarshal(new StringReader(xmlString));
        } catch (JAXBException e) {
            e.printStackTrace();
        }

        return object;
    }

    @SuppressWarnings("unchecked")
    public static <T> T XmlToBean2(String xmlString,Class<T> load) throws JAXBException {

        JAXBContext jaxbContext = JAXBContext.newInstance(load);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        return (T) unmarshaller.unmarshal(new StringReader(xmlString));

    }
}

7. Use this utility class to parse XML strings.

Result result = XmlToJavaBeanUtils.XmlToBean2(xmlString, Result.class);

The final overall code is as follows:

    @Test
    public void test() throws MalformedURLException, JAXBException {
        URL url = new URL("http://jq.shanghai12301.com/LYJWebService/PassengerInfo/PassengerInfoWebService.asmx?wsdl");
        QName qName = new QName("http://tempuri.org/", "PassengerInfoWebService");
        Service service = Service.create(url, qName);
        ChinaStockSmallImageWSSoap passengerInfoWebServiceSoap = service.getPort(ChinaStockSmallImageWSSoap.class);
        String xmlString = passengerInfoWebServiceSoap.getSmallImage("sh000001",sh000001);

        Result result = XmlToJavaBeanUtils.XmlToBean2(xmlString, Result.class);
    }

Take two steps for the rest of the way and have a look.

Tags: xml Java Junit Maven

Posted on Sun, 28 Jun 2020 01:59:41 -0400 by duncanmaclean