SOA and Web Service Technology Experimental Report [Complete Collection]

The textbook used: "Java Web Service Construction and Operation" If this article is useful to you, please like and follow me!

6-hour experiment topics: ① Web Service environment construction ; ② REST service as a HttpServlet class ; ③ JAX-RS Web Service ; ④ JAX-RS Client ; ⑤ JAX-RS client ; ⑥ WSDL service contract

All the code required for this experiment (organized clearly): https://download.csdn.net/download/qq_41587612/12517176

The above requires points, if you don't have points, you can download it on Github: https://github.com/PigeonIsSmart/SOA-WebService

● 常见报错:Several ports (8005, 8080, 8009) required by Tomcat v7.0 Server at localhost are already in use?

   Solution: https://blog.csdn.net/xiaopingga/article/details/84988526

FAQ: Chinese garbled characters in Windows console cmd?

   Permanent solution: https://blog.csdn.net/qq_35038153/article/details/78430359

● Common Http response status codes?

Experiment 1 Web Service environment construction (non-noob can skip, briefly put the picture) 

1. The purpose of the experiment

  1. Master the installation of the Java environment.
  2. Master the basic usage of eclipse.
  3. Learn to create a Java web project.

2. Experimental content

      1. Installation and configuration of Java environment.

           1) Java download address

                http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

                 Download the version suitable for your system according to the version on the website.

          2) Java installation

          3) JAVA_HOME placement.

                i. Open Advanced System Settings

               ii. Click Environment Variables

              iii. Create an environment variable named JAVA_HOME in the system variable, the variable value is the installation directory of JAVA.

          4) Find the path in the system variable, add a semicolon ";" at the end of the variable value, then copy the path + bin where you installed java, and click OK.

          5) Check if the installation is successful.

                i. Enter java -version in CMD, and the Java version number appears to prove that the installation was successful.

      2. Create a new Java Web project using eclipse.

          1) Configure tomcat

                i. Open the preferences under the window tab

               ii. Configure tomcat

          2) Create a new java_web application

                i. Select Dynamic Web Project

               ii. Take the project name and select tomcat, click next

              iii. Select Generate and click finish.

      3. Run the program and print the output.

          1) Create a new index.jsp in WebContext

          2) Change the jsp, right-click the project and select run as server (requires to display the student's own name and student number)

3. Experimental requirements

  1. Students are required to be proficient in the installation process of the Java environment.
  2. Learn to build tomcat on eclisp.
  3. Learn to use eclipse to create a web project.
  4. Learn to use tomcat server to run Java web programs.

4. Experimental results

Experiment 2 RESTful service as an HttpServlet class

1. The purpose of the experiment

  1. Get familiar with RESTful services as an HttpServlet class.

2. Experimental content

      1. Create a new predictions2 project.

      2. Create a new predictions2 package and create JerseyClient, Prediction, Predictions, and PredictionsServlet classes. (The resource pack will be given below!)

      3. Import the required Jar package and import the data file.

      4. Change web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
  <servlet>
    <servlet-name>predictor</servlet-name>
    <servlet-class>predictions2.PredictionsServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>predictor</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>

      5. Run the project

3. Experimental requirements

  1. Master the construction of predictions2-style servlet services.

Fourth, the experimental process and screenshots

Difference: There is no text description on the first line of the running result on Eclipse, but there is on the browser.

Experiment 3 JAX-RS Web Service

You can refer to http://www.jquerycn.cn/a_17530 Although the typesetting is unbearable, some codes have detailed Chinese comments. The code in the information package I gave is English comments.

1. The purpose of the experiment

  1. Learn how to implement a RESTful service framework using JAX-RS.
  2. Learn to send HTTP requests with curl.

2. Experimental content

      1. JAX-RS stands for Java API for RESTful Web Services, a Java API that makes the development of Java Restful services quick and easy. This API provides an annotation-based model for describing distributed resources. Annotations are used to provide the location of resources, the representation of resources, and a pluggable data binding architecture.

      2. curl download and configuration

          1) Download address: https://curl.haxx.se/download.html Choose the curl that suits your system version

          2) Installation and configuration of curl (win64)

                i. Unzip the installation package

               ii. Configure the curl environment variable (the path is the file path of curl after decompression)

 

Additional instructions for Xiaobai: Right-click "This Computer" on the desktop, select "Properties", select "Advanced System Settings" on the left side of the pop-up window, select "Environment Variables" at the bottom of the pop-up window, and then select "System Variables" in the pop-up window. ", ① create a new "CURL_HOME", the variable value is "curl decompression path"; ② edit "Path", press the "end" button on the right side of the keyboard (if invalid, first press "num lock / scr lock" [abbreviated as Don't know "num lk", hello] and then press "end") to return to the end of "variable value", and add "curl's bin path" after the semicolon.

      3. Use JAX-RS to build a RESTful service framework.

          1)新建Prediction、PredictionsList、PredictonsRS、RestfulPrediction类。

          2) Create a new web project

          3) Import the jar package and data file 

          4) Modify web.xml 

<?xml version="1.0" encoding="UTF-8"?>  
<web-app>  
  <servlet>  
    <servlet-name>jersey</servlet-name>  
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>  
    <load-on-startup>1</load-on-startup>
  </servlet>      
</web-app>

          5) Run the project (Note: The access to the path written in the readme file in the data package will be 404, but it can be accessed by adding /xml or /json after the path)

      4. Use of Curl

          1) Enter the following commands in cmd in turn to view the output results.

                i.  curl http://localhost:8080/JAX-RS/resourcesP/xml

               ii.  curl http://localhost:8080/JAX-RS/resourcesP/json

          2) View PredictionsRS.java, use curl to output the content with the id of 1, and the format is xml.

3. Experimental requirements

  1. Students are required to master a simple RESTful service framework using JAX-RS.
  2. Learn to install and configure curl and use it.

Fourth, the experimental process and screenshots

Experiment 4 JAX-RS Client

1. Purpose of the experiment

  1. Master the construction of JAX-RS Client.
  2. Create a client for a predictions2 based RESTful service.
  3. Understand the interaction between client and server.

2. Experimental content

     1. Create a new predictions2 Service.

    2. Right-click the project Run on Server

     3. Create a new JerseyClient class under the predictions2 package and import the required jar package.

     4. Right-click the JerseyClient class and select run as JavaApplication. View the console running results.

     5. Change JerseyClient.java, request to output and delete the data with id = 10, and the output format is JSON. Changed code▼

package predictions2;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import javax.ws.rs.core.MediaType;
import com.sun.jersey.api.representation.Form;

public class JerseyClient {
	private static final String baseUrl = "http://localhost:8080/predictions2";

	public static void main(String[] args) {
		new JerseyClient().demo();
	}

	private void demo() {
		Client client = Client.create();
		client.setFollowRedirects(true); // in case the service redirects

		WebResource resource = client.resource(baseUrl);
		getAllDemo(resource);
		postDemo(resource); // same resource but different verb

		String url = baseUrl + "?id=10";
		resource = client.resource(url);
		getOneDemo(resource);
		deleteDemo(resource); // delete id = 10
	}

	private void getAllDemo(WebResource resource) {
		String response = resource.accept(MediaType.APPLICATION_JSON_TYPE).get(String.class);
		report("GET all in JSON:\n", response);
	}

	private void getOneDemo(WebResource resource) {
		String response = resource.accept(MediaType.APPLICATION_JSON_TYPE).get(String.class);
		report("GET one in JSON:\n", response);
	}

	private void postDemo(WebResource resource) {
		Form form = new Form(); // HTTP body, a simple hash
		form.add("who", "William Butler Yeats");
		form.add("what", "I know that I shall meet my fate");

		String response = resource.type(MediaType.APPLICATION_FORM_URLENCODED_TYPE).accept
				(MediaType.TEXT_PLAIN_TYPE).post(String.class, form);
		report("POST:\n", response);
	}

	private void deleteDemo(WebResource resource) {
		String response = resource.accept(MediaType.TEXT_PLAIN_TYPE).delete(String.class);
		report("DELETE:\n", response);
	}

	private void report(String msg, String response) {
		System.out.println("\n" + msg + response);
	}
}

3. Experimental requirements

  1. Students are required to master the configuration of the JAX-RS client.
  2. The program was successfully tuned and changed.

Fourth, the experimental process and screenshots

The following pictures are the results of executing JerseyClient.java, the content is very long, and some parts are cut to help understanding!

 

The above three pictures are continuous, counting from the 0th item, it can be seen that the program originally deleted the 31st item (the last item) prediction (prediction) and added a prediction ("I know that I shall meet my fate" becomes New 31 predictions) [HTTP body, a simple Hash], now we want to change it to "output and delete the data with id = 10, the output format is JSON".

You just need to look at JerseyClient.java (59 lines of code in total), it's very easy to change/read, don't be afraid!

The result of running (continuous) after the modification is as follows:

 

If you have the following situations, please re-Run on Server, and then Run as JavaApplication! ! !

JerseyClient.java has been run once, and it will be like this if you repeat Run as JavaApplication

 Experiment 5 JAX-RS client

1. The purpose of the experiment

  1. Master the construction of REST Client.
  2. Create a client for the predictions3 service.
  3. Become familiar with the interaction between client and server.

2. Experimental content

     1. Create a new predictions3 project.

 

     2. Right-click the project and run on server.

     3. Create a client package, create a Java class in it, and import the jar package.

     4. Right-click the JerseyClient class and select run as JavaApplication. View the console running results.

3. Experimental requirements

  1. Students are required to master the configuration of RESTful clients.
  2. The program was successfully tuned and changed.

4. Experimental results

Ignore warnings on a daily basis. . . Create a client package (Package), import the class and jar package, right-click the Predictions3Client class, Run as JavaApplication

 Experiment 6 WSDL service contract

1. The purpose of the experiment

  1. Learn to use java code to publish endpoints.
  2. Learn what WSDL does.

2. Experimental content:

  1. create a new project
  2. Create new RandPublisher and RandService classes.

  3. Import jar package and xml

  4. change web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:web="http://xmlns.jcp.org/xml/ns/javaee">
      <listener>
        <listener-class>
          com.sun.xml.ws.transport.http.servlet.WSServletContextListener
        </listener-class>
      </listener>
      <servlet>
        <servlet-name>jaxws</servlet-name>
        <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>jaxws</servlet-name>
        <url-pattern>/*</url-pattern>
      </servlet-mapping>
    </web-app>
  5. run the project
  6. Run the RandPublisher class and enter http://localhost:8888/ws in the browser .

3. Experimental requirements

  1. Learn to use java code to publish endpoints.
  2. Master the role of WSDL.
  3. The code was successfully debugged.

4. Operation results

先 Run on JavaApplication,再 Run as Server!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324174095&siteId=291194637