How to develop WebService in the Java system

Source: Internet
Author: User

The application of WebService has become more and more extensive, and the following describes several ways to develop WebService in the Java system, which is equivalent to making a record.
1.axis2
Axis is Apache's next open source WebService development component, which appears to be relatively early, but also more mature. Here the main introduction axis+eclipse development WebService, of course, without eclipse can also develop and release webservice, only with eclipse will be more convenient.

(1) Download the Java EE version of Eclipse

(2) Download Axis2

(3) Download Eclipse's Axis2 plugin
Axis2_codegen_wizard
Axis2_service_archiver

Recommended version of 1.3

(4) Eclipse installation Axis2 Plugin
1) Create a new Axis2 folder under any directory, create a new Eclipse directory under this folder, create a new plugins directory and features directory in the Eclipse directory, for example: D:\programSoftware\eclipse-SVN\ Axis2\eclipse;
2) unzip the downloaded Axis2 plugin and put the extracted files into the new Eclipse's plugins directory;
3) Create a new links directory under the%eclipse_home% directory and create a new Axis2.link file in the links directory with the content: Path=d:\programsoftware\eclipse-svn\axis2;
4) Restart Eclipse, click File-new-other, if you see Axis2 Wizards, the plugin installation is successful.

(5) Installation Axis2

(6) Create a generic Java class with at least one method using Eclipse new Web project.

(7) Release WebService
1) Click Eclipse's File-new-other, open Axis2 Wizards, select Axis2 Service archiver, then next;
2) Select the class file location, which is the classpath, note: Only choose the classes directory, do not include the package folder, and then next;
3) Select Skip WSDL, then next
4) All the way next to select the service XML file to is included in the service archive, tick generate Theservice XML automatically;
5) Service name-fill in your service name, class name-fill in the class name, to include the package name, then click Load, and then click Finish, then WebService published successfully;
6) then go to%tomcat_home%/webapps/axis2/web-inf/services to see if there is more than one. aar file;

Note: The above method is published in the Axis2.war package, you can also copy the generated. aar file to your real application, and you can also use the Eclipse's Create WebService feature to publish your WebService, Choose Axis2 to generate your webservice so that WebService will be deployed to your app.

2.Apche CXF
CXF Development WebService is also relatively convenient and simple, it and spring integration can be said to be very good. Give an example of a CXF development webservice.
1) Create a new Web project in Eclipse, import the dependent packages,

2) Write an interface, such as:

Note: CXF development of the WebService, interface in the parameters of the method must be in this way, otherwise the client calls when the CXF server will not receive the value of the parameter, name: parameter name, can not write (suggested write on), targetnamespace: namespace, Be sure to fill in the default is the package name in reverse order, mode: parameter type, in denotes input.
3) write an implementation class to implement the interface method;
4) and spring integration, write a bean file, such as: Cxf-beans.xml, the content is as follows:

Cxf-beans.xml Code
<?xml version= "1.0" encoding= "UTF-8"?>

<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 file is easier to understand and does not explain it.
5) Configuration Cxfservlet
Configure Cxfservlet in the Web. xml file to load the Cxf-beans.xml file with the following contents:

Web. XML code

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>
Deploy the project to middleware, such as Tomcat, to access the webservice.

3.JDK Development WebService
1) Write a Java class, as follows:

Jdkwebservice.java Code
Package demo;

Import Javax.jws.WebParam;
Import Javax.jws.WebService;
Import Javax.xml.ws.Endpoint;

@WebService
public class Jdkwebservice {

Return "Just do it," + Value + "!";
}

public static void Main (string[] args) {

}
}
2) Run the Java class and the WebService will be accessible on the browser.
Note: This approach is not very friendly when developing Web projects. We can write a servlet class that publishes WebService in the initialization method of the Servlet class, so that when our middleware server starts, it will automatically webservice for us.

4) Xfire
Development webservice a lot of frameworks, each frame has its own a bit, recently I use Xfire practice development WebService, below is a small example of development webservice, I hope to get some small help to get started
1. Create a new Java Web project named TestWebService, add the Xfire related jar package to the Lib directory, write the interface class and the implementation class

Java code
Package com.lamp.service;

Public interface Messageservice {
public string GetName (string name);
}
[Java] View plaincopyprint?
Package com.lamp.service;

Public interface Messageservice {
public string GetName (string name);
}

Implementation class

Java code
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";
}

}
[Java] View plaincopyprint?
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";
}

}

Create a new folder Meta-inf under the SRC directory, and then create a new folder under it Xfire, create a new profile in the Xfire directory Services.xml

XML code
<?xml version= "1.0" encoding= "UTF-8"?>

<service>
<name>MessageService</name>
<serviceClass>com.lamp.service.MessageService</serviceClass>
<implementationClass>com.lamp.service.impl.MessageServiceImpl</implementationClass>
</service>
</beans>
[XML] View plaincopyprint?
<?xml version= "1.0" encoding= "UTF-8"?>

<service>
<name>MessageService</name>
<serviceClass>com.lamp.service.MessageService</serviceClass>
<implementationClass>com.lamp.service.impl.MessageServiceImpl</implementationClass>
</service>
</beans>

Finally, configure the Xfire servlet in Web. xml

XML code
<servlet>
<servlet-name>XFireServlet</servlet-name>
<servlet-class>

</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>
[XML] View plaincopyprint?
<servlet>
<servlet-name>XFireServlet</servlet-name>
<servlet-class>

</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>

Now that the server-side development is complete, the client development begins
Creating a new Java project also introduces Xfire related jars, I use ant to generate proxy objects on the client, create a new build.xml under the project path, and the code is

XML code
<?xml version= "1.0" encoding= "UTF-8"?>

<project name= "WebService" basedir= "default=" Gen-webservice ">

<property file= "Build.properties" >
</property>

<path id= "Project-classpath" >
<fileset dir= "${lib.dir}" >
<include name= "**/*.jar"/>
</fileset>
</path>

<target name= "Gen-webservice" >
<taskdef name= "Wsgen" classname= "Org.codehaus.xfire.gen.WsGenTask" classpathref= "Project-classpath"/>

<wsgen outputdirectory= "${src.dir}"
Wsdl= "${wsdl.dir}" package= "Com.lamp.ws.client" overwrite= "true"/>

</target>

</project>
[XML] View plaincopyprint?
<?xml version= "1.0" encoding= "UTF-8"?>

<project name= "WebService" basedir= "default=" Gen-webservice ">

<property file= "Build.properties" >
</property>

<path id= "Project-classpath" >
<fileset dir= "${lib.dir}" >
<include name= "**/*.jar"/>
</fileset>
</path>

<target name= "Gen-webservice" >
<taskdef name= "Wsgen" classname= "Org.codehaus.xfire.gen.WsGenTask" classpathref= "Project-classpath"/>

<wsgen outputdirectory= "${src.dir}"
Wsdl= "${wsdl.dir}" package= "Com.lamp.ws.client" overwrite= "true"/>

</target>

</project>

How to develop WebService in the Java system

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.