In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will give you a detailed explanation on how to use JDK to develop WebService. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
I. the development means of WebService
You can use the following two development tools when developing WebService with Java
1. Develop with JDK (version 1.6 or above)
2. Develop using CXF framework (in work)
Second, use JDK to develop WebService2.1 and WebService server
1. Define an interface, annotate the API with @ WebService, and annotate all the methods defined in the API with @ WebMethod, as shown below:
1 package me.gacl.ws; 2 3 import javax.jws.WebMethod; 4 import javax.jws.WebService; 5 6 / * * 7 * @ author gacl 8 * define SEI (WebService EndPoint Interface (terminal)) 9 * / 10 / use @ WebService annotation to label WebServiceI interface 11 @ WebService12 public interface WebServiceI {13 14 / / use @ WebMethod annotation to label method 15 @ WebMethod16 String sayHello (String name) in WebServiceI interface 17 18 @ WebMethod19 String save (String name,String pwd); 20}
2. Write the implementation class of interface, annotate the implementation class with @ WebService annotation, and implement all the methods defined in the interface, as shown below:
1 package me.gacl.ws; 2 3 import javax.jws.WebService; 4 5 / * * 6 * @ author gacl 7 * SEI implementation 8 * / 9 / / use @ WebService annotation to mark the implementation class of the WebServiceI interface WebServiceImpl10 @ WebService11 public class WebServiceImpl implements WebServiceI {12 13 @ Override14 public String sayHello (String name) {15 System.out.println ("WebService sayHello" + name); 16 return "sayHello" + name 17} 18 19 @ Override20 public String save (String name, String pwd) {21 System.out.println ("WebService save" + name+ "," + pwd); 22 return "save Success"; 23} 24}
3. Use Endpoint (terminal) class to publish webservice. The code is as follows:
1 package me.gacl.ws.test; 2 3 import javax.xml.ws.Endpoint; 4 5 import me.gacl.ws.WebServiceImpl 6 7 / * * 8 * @ author gacl 9 * 10 * publish Web Service11 * / 12 public class WebServicePublish {13 14 public static void main (String [] args) {15 / / define the WebService publishing address, which is the URL address provided to the outside world to access the Webervice. The URL address format is: http://ip: port number / xxxx16 / / String address = "http://192.168.1.100:8989/";" The WebService publishing address is written as legal 17 / / String address = "http://192.168.1.100:8989/Webservice"; the WebService publishing address is legal 18 String address =" http://192.168.1.100:8989/WS_Server/Webservice"; 19 / / publish WebService using the publish method provided by the Endpoint class to ensure that the port number used is not occupied by other applications for 20 Endpoint.publish (address, new WebServiceImpl ()); 21 System.out.println ("publish webservice successfully!"); 22} 23}
Run the WebServicePublish class to publish the written WebService. The access URL for WebService is: http://192.168.1.100:8989/WS_Server/Webservice, as shown in the following figure:
Here we write a WebServicePublish class to publish WebService. If it is a Web project, we can use listeners or Servlet to publish WebService, as follows:
1. Use ServletContextListener listener to publish WebService
1 package me.gacl.listener; 2 3 import javax.servlet.ServletContextEvent; 4 import javax.servlet.ServletContextListener; 5 import javax.servlet.annotation.WebListener; 6 import javax.xml.ws.Endpoint; 7 import me.gacl.ws.WebServiceImpl 8 9 / * 10 * @ author gacl11 * listeners for publishing WebService 12 * / 13 / / use the @ WebListener annotation provided by Servlet3.0 to label the WebServicePublishListener class that implements the ServletContextListener interface as a Listener14 @ WebListener15 public class WebServicePublishListener implements ServletContextListener {16 17 @ Override18 public void contextDestroyed (ServletContextEvent sce) {19 20} 21 22 @ Override23 public void contextInitialized (ServletContextEvent sce) {24 / / WebService publish address 25 String address = "http://192.168.1.100:8080/WS_Server/WebService"; 26 / publish WebService,WebServiceImpl class is the concrete implementation class of WebServie interface 27 Endpoint.publish (address, new WebServiceImpl ()); 28 System.out.println ("publish webservice successfully using WebServicePublishListener!"); 29} 30}
When the Web application is deployed to the server runtime, the WebService is released when the Web application context is initialized.
Then we can access the WebService using the published URL address, as shown in the following figure:
2. Use Servlet to publish WebService
1 package me.gacl.web.controller; 2 3 import javax.servlet.ServletException; 4 import javax.servlet.annotation.WebServlet; 5 import javax.servlet.http.HttpServlet; 6 import javax.xml.ws.Endpoint; 7 import me.gacl.ws.WebServiceImpl 8 9 / * 10 * @ author gacl11 * Servlet12 for publishing WebService * / 13 / / use the @ WebServlet annotation provided by Servlet3.0 to mark the normal Java class that inherits the HttpServlet class as a Servlet14 / / set the value property to an empty string In this way, WebServicePublishServlet does not provide the path for external access 15 / / loadOnStartup property to set the initialization time of WebServicePublishServlet 16 @ WebServlet (value= "" LoadOnStartup=0) 17 public class WebServicePublishServlet extends HttpServlet {18 19 / * (non-Javadoc) 20 * @ see javax.servlet.GenericServlet#init () 21 * release WebService22 * / 23 public void init () throws ServletException {24 / / WebService at WebServicePublishServlet initialization 25 String address = "http://192.168.1.100:8888/WebService";" 26 / publish WebService,WebServiceImpl class is the concrete implementation class of WebServie interface 27 Endpoint.publish (address, new WebServiceImpl ()); 28 System.out.println ("publish webservice successfully using WebServicePublishServlet!"); 29} 30}
When the Web application is deployed to the server runtime, the WebService is released when the WebServicePublishServlet is initialized. As shown in the following figure:
Then we can access the WebService using the published URL address, as shown in the following figure:
The release WebService is mainly released through the static method publish provided by the javax.xml.ws.Endpoint class, if it is an ordinary java project, then you can write a special class for publishing WebService, if it is a Web project, then you can use ServletContextListener or Servlet to publish.
2.2.Development of WebService client
1. Generate the client code with the wsimort.exe tool of jdk. The wsimort.exe tool is located in the bin directory of Jdk, as shown below:
Execute the command: wsimport-keep url (url is the path to the wsdl file) generates client code.
Create a WebService client test project, as shown in the following figure:
Open a command line window, change to the src directory, and execute the wsimport-keep http://192.168.1.100:8888/WebService?wsdl" generation client code, as shown in the following figure:
If no errors occur during the execution of the command, the code is generated successfully. Refresh the src directory and you can see the generated code, as shown in the following figure:
2. Call the methods provided by WebService with the help of the generated code
The wsimport tool helps us generate several java classes, but all we need to care about is the use of the WebServiceImplService class and the WebServiceImpl interface, as follows:
1 package me.gacl.ws.client; 2 3 import me.gacl.ws.WebServiceImpl; 4 import me.gacl.ws.WebServiceImplService; 5 6 / * * 7 * @ author gacl 8 * client 9 * / 10 public class WSClient {11 12 public static void main (String [] args) {13 / / create a factory for generating WebServiceImpl instances. The WebServiceImplService class is 14 WebServiceImplService factory = new WebServiceImplService () generated by the wsimport tool. 15 / / generate a WebServiceImpl instance through the factory. WebServiceImpl is 16 WebServiceImpl wsImpl = factory.getWebServiceImplPort () generated by the wsimport tool; 17 / / call the sayHello method of WebService 18 String resResult = wsImpl.sayHello ("lonely wolf"); 19 System.out.println ("the result returned by calling the sayHello method of WebService is:" + resResult) 20 System.out.println ("- -"); 21 / call WebService's save method 22 resResult = wsImpl.save ("Lonely Wolf", "123") 23 System.out.println ("the result returned by calling the save method of WebService is:" + resResult); 24} 25}
The result of the client calling the server-side WebService method is as follows:
The result returned from the call shows that the client code generated with the wsimport tool has successfully called the method in WebService. Above is the relevant content of using JDK to develop WebService.
This is the end of this article on "how to use JDK to develop WebService". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.