Explain several ways java can develop webservice

  • 2020-05-17 05:29:35
  • OfStack

webservice has become more and more widely used. Here are some ways to develop webservice in the Java system, which is equivalent to keeping a record.

1.Axis2

Axis is the next open source webservice development component of apache, which is relatively early and mature. This article mainly introduces Axis+eclipse to develop webservice. Of course, webservice can be developed and released without eclipse, but eclipse is more convenient.

(1) download eclipse Java EE version. / / www ofstack. com softs / 239903. # html down

(2) download axis2 http: / / axis apache. org/axis2 / java/core/download cgi

(3) download the axis2 plug-in for eclipse

Axis2_Codegen_Wizard
Axis2_Service_Archiver
http://axis.apache.org/axis2/java/core/tools/index.html
Version 1.3 is recommended

(4) install the axis2 plug-in on eclipse

1) create a new Axis2 folder in any directory, eclipse directory, plugins directory and features directory in eclipse directory, such as: D:\programSoftware\ eclipse-SVN \Axis2\eclipse;

2) unzip the downloaded axis2 plug-in and put the unzipped file into the plugins directory of the newly created eclipse;

3) create a new links directory under %eclipse_home% and a new axis2.link file under % links, path=D:\programSoftware\ eclipse-SVN \Axis2;

4) restart eclipse and click · file-new-other. If you see Axis2 Wizards, the installation of the plug-in is successful.

(5) install axis2

Download Axis2 WAR Distribution and decompression, the axis2. war package placed under the % TOMCAT_HOME % / webapps, start tomcat, access http: / / localhost: port/axis2, Axis2 installation is successful.

(6) use eclipse to create a new web project and create a common java class, including at least one method.

(7) release webservice

1) click File-New-other on eclipse, open Axis2 Wizards, select Axis2 Service Archiver, then Next;

2) select Class File Location, which is the path to store class files. Note: only select classes directory, do not include package folder, and then Next;

3) select Skip WSDL, then Next

4) route 1 Next to Select the file be in included the Service Service automatically, check Generate theservice xml automatically;

5) Service Name- fill in your service name, Class Name- fill in the class name, including the package name, then click load, then click Finish, then webservice will be published successfully;

6) and then to the % TOMCAT_HOME % / webapps/axis2 / WEB INF/services see if more than one. aar file;

7) access http: / / localhost: 8085 / axis2 / services/class name & # 63; wsdl will see the generated wsdl file.
Note: the above method is to publish axis2.war, you can also generate aar file copy to your actual application, at the same time, you can also use eclipse create webservice function to publish your webservice, select axis2 to generate your webservice, so webservice will be deployed to your application.

2.Apche CXF

CXF development webservice is also relatively convenient and simple, and its integration with spring is very good. Take an example of CXF developing webservice.

1) create a new web project in eclipse and import the dependency package.

2) write an interface, such as:


public String test(@WebParam(name="value", targetNamespace = "http://service.cxf.zcl.com/", mode = WebParam.Mode.IN)String value);

Note: webservice CXF development, the interface method of parameters in this way, much or when the client calls CXF server will receive less than the value of the parameter, name: parameter name, don't write (write) advice, targetNamespace: namespace, 1 need to fill in, is the default package name in turn order, mode: parameter types, input IN said.

3) write an implementation class to implement the interface method;

4) integration with spring, write one bean file, such as: cxf-beans. xml, which reads as follows:


<?xml version="1.0" encoding="UTF-8" ?>  
<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:jaxws="http://cxf.apache.org/jaxws" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">  
 
  <import resource="classpath:META-INF/cxf/cxf.xml" />  
  <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
  <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
    
  <jaxws:endpoint id="vote" implementor="com.zcl.cxf.service.VoteImpl" address="/Vote" />  
</beans> 

This document is easier to understand than to explain.

5) configure CXFServlet

Configure CXFServlet in the web.xml file and load the cxf-beans.xml file as follows:


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
  id="WebApp_ID" version="2.5">  
 
  <context-param>  
    <param-name>contextConfigLocation</param-name>  
    <param-value>WEB-INF/cxf-beans.xml</param-value>  
  </context-param>  
    
  <listener>  
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  </listener>  
    
  <servlet>  
    <servlet-name>cxf</servlet-name>  
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
    <load-on-startup>1</load-on-startup>  
  </servlet>  
  <servlet-mapping>  
    <servlet-name>cxf</servlet-name>  
    <url-pattern>/services/*</url-pattern>  
  </servlet-mapping>  
</web-app> 

When you deploy the project to middleware, such as tomcat, you can access that webservice.

3. JDK webservice development

1) write one Java class, as follows:


package demo;  
 
import javax.jws.WebParam;  
import javax.jws.WebService;  
import javax.xml.ws.Endpoint;  
 
@WebService  
public class JdkWebService {  
 
  public String doSomething(@WebParam(name="value", targetNamespace = "http://demo/", mode = WebParam.Mode.IN)String value) {  
    return "Just do it," + value + "!";  
  }  
    
  public static void main(String[] args) {  
    Endpoint.publish("http://localhost:8080/jdkwsdemo/demo.JdkWebService", new JdkWebService());  
  }  
} 

2) run the java class and you can access the webservice in a browser.

Note: when developing the web project, this approach was not very friendly. We can write one servlet class and publish webservice in the initialization method of servlet class, so that our middleware server will automatically webservice when it starts.

3) xfire

There are many frameworks for developing WebService, and each one has its own advantages. Recently, I have been practicing developing WebService with xfire. Here is a small example of developing WebService

1. Create a new java web project named TestWebService, add xfire related jar package to lib directory, write interface class and implementation class


package com.lamp.service;  
 
public interface MessageService {  
  public String getName(String name);  
} 

package com.lamp.service; 
 
public interface MessageService { 
  public String getName(String name); 
}

The implementation class


package com.lamp.service.impl;  
 
import com.lamp.service.MessageService;  
 
public class MessageServiceImpl implements MessageService {  
 
  public String getName(String name) {  
    return "hellow " + name + ", welcome to WebService world";  
  }  
 
} 

package com.lamp.service.impl; 
 
import com.lamp.service.MessageService; 
 
public class MessageServiceImpl implements MessageService { 
 
  public String getName(String name) { 
    return "hellow " + name + ", welcome to WebService world"; 
  } 
 
} 

Create a new folder META-INF in the src directory, then create a new folder xfire under it, and a new configuration file services.xml in the xfire directory


<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://xfire.codehaus.org/config/1.0"> 
 <service> 
  <name>MessageService</name> 
  <serviceClass>com.lamp.service.MessageService</serviceClass> 
  <implementationClass>com.lamp.service.impl.MessageServiceImpl</implementationClass> 
 </service> 
</beans> 
<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://xfire.codehaus.org/config/1.0"> 
 <service> 
  <name>MessageService</name> 
  <serviceClass>com.lamp.service.MessageService</serviceClass> 
  <implementationClass>com.lamp.service.impl.MessageServiceImpl</implementationClass> 
 </service> 
</beans> 

Finally, servlet of xfire is configured in web.xml


<servlet> 
    <servlet-name>XFireServlet</servlet-name> 
    <servlet-class> 
      org.codehaus.xfire.transport.http.XFireConfigurableServlet  
    </servlet-class> 
  </servlet> 
 
  <servlet-mapping> 
    <servlet-name>XFireServlet</servlet-name> 
    <url-pattern>/servlet/XFireServlet/*</url-pattern> 
  </servlet-mapping> 
 
  <servlet-mapping> 
    <servlet-name>XFireServlet</servlet-name> 
    <url-pattern>/services/*</url-pattern> 
  </servlet-mapping> 
<servlet> 
    <servlet-name>XFireServlet</servlet-name> 
    <servlet-class> 
      org.codehaus.xfire.transport.http.XFireConfigurableServlet 
    </servlet-class> 
  </servlet> 
 
  <servlet-mapping> 
    <servlet-name>XFireServlet</servlet-name> 
    <url-pattern>/servlet/XFireServlet/*</url-pattern> 
  </servlet-mapping> 
 
  <servlet-mapping> 
    <servlet-name>XFireServlet</servlet-name> 
    <url-pattern>/services/*</url-pattern> 
  </servlet-mapping> 

After project deployment in the browser through http: / / localhost: 8080 / TestWebService/services visit to see the remote access interface, and get a wsdl http: / / localhost: 8080 / TestWebService/services/MessageService & # 63; wsdl

This completes the server side development and now starts the client side development

Creating a new java project also introduces xfire related jar. I use ant to generate proxy objects on the client side, and create build.xml under the project path. The code is


<?xml version="1.0" encoding="UTF-8" ?>  
<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:jaxws="http://cxf.apache.org/jaxws" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">  
 
  <import resource="classpath:META-INF/cxf/cxf.xml" />  
  <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
  <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
    
  <jaxws:endpoint id="vote" implementor="com.zcl.cxf.service.VoteImpl" address="/Vote" />  
</beans> 
0

<?xml version="1.0" encoding="UTF-8" ?>  
<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:jaxws="http://cxf.apache.org/jaxws" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">  
 
  <import resource="classpath:META-INF/cxf/cxf.xml" />  
  <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
  <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
    
  <jaxws:endpoint id="vote" implementor="com.zcl.cxf.service.VoteImpl" address="/Vote" />  
</beans> 
1

The build.properties file it introduced is also under the project path


<?xml version="1.0" encoding="UTF-8" ?>  
<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:jaxws="http://cxf.apache.org/jaxws" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">  
 
  <import resource="classpath:META-INF/cxf/cxf.xml" />  
  <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
  <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
    
  <jaxws:endpoint id="vote" implementor="com.zcl.cxf.service.VoteImpl" address="/Vote" />  
</beans> 
2

Where lib.jar is the path where I store xfire and run ant to get the proxy object

Write a test class


package com.lamp.test;  
 
import com.lamp.ws.client.MessageServiceClient;  
import com.lamp.ws.client.MessageServicePortType;  
 
public class TestGetName {  
 
  public static void main(String[] args) {  
    MessageServiceClient msg = new MessageServiceClient();  
    MessageServicePortType portType = msg.getMessageServiceHttpPort();  
    String result = portType.getName(" zhang 3");  
    System.out.println(result);  
  }  
 
} 

<?xml version="1.0" encoding="UTF-8" ?>  
<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:jaxws="http://cxf.apache.org/jaxws" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">  
 
  <import resource="classpath:META-INF/cxf/cxf.xml" />  
  <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
  <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
    
  <jaxws:endpoint id="vote" implementor="com.zcl.cxf.service.VoteImpl" address="/Vote" />  
</beans> 
4

Run on the console and see hellow sheet 3, welcome to WebService world so that a simple WebService is developed


Related articles: