Create and call WebService in Intellij IDEA

Article directory

Create and call WebServices in Intellij IDEA

Zero. Environmental preparation

   the experimental demonstration environment is:

  • Windows 10
  • IntelliJ IDEA 2019.2.4 (Ultimate Edition)
  • Apache Tomcat/8.5.35
  • java 1.8.0_211

1, Create WebServices

1. Create a new WebServices project

Open the Intellij IDEA development tool, and click File → New → Project ]

   Next, select the corresponding configuration in sequence according to the sequence in the figure, and finally click [Next]

   enter the project name, and click Finish. Intellij will automatically download the corresponding dependent package.

After downloading the dependency package, the project structure directory is as follows.

2. Program preparation

                        .

3. Dependency configuration

3.1 configure Tomcat

Click Add Configuration in the upper right corner of Intellij ]

   click Add Tomcat in sequence

   modify the Tomcat name and click [Fix] to set the deployment

                         

3.2 add project dependency

Click File > Project Structure ], enter Project Structure configuration.

                     ], select the first [Add Yes.

   the creation configuration for this WebService is complete.

4. Operation test

Click to run Tomcat

                        .

   click [Hello World (wsdl)] to see the details.

5. generate wsdl

Click Tools > WebServices > Generate Wsdl From Java Code on the top of Intellij ]

Click OK

After    generation, you can see that there are more [HelloWorld.wsdl] files in the directory.

2, Call WebServices

1. Create client package

                   

2. Generate java code from wsdl file

   first click the HelloWorld.wsdl file, and then click Tools → WebServices → generate java code from WSDL ]

                        .

After     success, you can see that many files are generated in the client directory.

3. Write test cases

   create a new test.java in the client directory, and add the following test code.

package client;

import javax.xml.rpc.ServiceException;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;

public class test {
    public static void main(String[] args){
        // 1. Specify the URL to call WebService. Here is the URL to click HelloWorld after publishing
        String url = "http://localhost:8080/webService_demo/services/HelloWorld?wsdl";
        try {
            // 2. Create a Locator object, which is equivalent to a class or library of traditional services
            HelloWorldServiceLocator helloWorldServiceLocator = new HelloWorldServiceLocator();
            // 3. Get port, which is equivalent to the method interface of traditional class or library
            HelloWorld_PortType service = helloWorldServiceLocator.getHelloWorld(new URL(url));
            // 4. Call service through port
            String result = service.sayHelloWorldFrom("summer");
            System.out.println(result);
        }catch (ServiceException | RemoteException | MalformedURLException ex){
            ex.printStackTrace();
        }
    }
}

4. Run test cases

                        .

This completes the call to   WebServices.

3, Bug s encountered and Solutions

1. Run test case report 404

                      .

//Original test code
package client;

import javax.xml.rpc.ServiceException;
import java.rmi.RemoteException;

public class test {
    public static void main(String[] args){
        try {
            // 1. Create a Locator object, which is equivalent to a class or library of a traditional service
            HelloWorldServiceLocator helloWorldServiceLocator = new HelloWorldServiceLocator();
            // 2. Get port, which is equivalent to the method interface of traditional class or library
            HelloWorld_PortType service = helloWorldServiceLocator.getHelloWorld();
            // 3. Call the service through port
            String result = service.sayHelloWorldFrom("summer");
            System.out.println(result);
        }catch (ServiceException | RemoteException ex){
            ex.printStackTrace();
        }
    }
}

2.log4j warning message

   log4j warning is always displayed when running the project

   create a new file [log4j.properties] in the src directory, and add the following code.

log4j.rootLogger=debug, stdout, R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%5p - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=firestorm.log

log4j.appender.R.MaxFileSize=100KB
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

log4j.logger.com.codefutures=DEBUG

Reference article: [1] WebService -- IDEA creates WebServices

     [2]idea builds WebService HelloWorld program

     [3]An example of developing WebService by Intellij IDEA

     [4]intellij develops Web Service

     [5]Developing WebService with IntelliJ IDEA java

     [6]An example of developing WebService by Intellij IDEA

     [5]Developing WebService with IntelliJ IDEA java

     [6]An example of developing WebService by Intellij IDEA

     [7]Develop a simple java web service and related knowledge

Published 193 original articles, won praise 108, visited 80000+
Private letter follow

Tags: log4j Java IntelliJ IDEA Apache

Posted on Sat, 11 Jan 2020 22:40:48 -0800 by kosmidd