MVN building JAX-ws projects with server and client code implementations

Source: Internet
Author: User

There are many WebService frameworks in Java, such as AXIS,XFIRE,CXF, which have a JAX-WS framework in the JDK, in addition to the above framework. In contrast, JWS is lightweight and easy to use, and can be integrated with spring.

The following is a detailed description of how to build WebService server and client through MVN.

1. Primer package.

Adding a dependency package to the MVN project's pom file

<dependency>    <groupId>com.sun.xml.ws</groupId>    <artifactId>jaxws-rt</artifactId>    <version>2.2.8</version></dependency>

Remember to rebuild the project after you've finished the package, with the following command

-Dwtpversion=2.0-DdownloadSources=true
2. Write the service-side code, and the Hello service class is as follows
@WebServicepublicclass Hello {    @WebMethod    publicsayHello(@WebParam"name") String name) {// @WebParam来指定wsdl文件schema的参数名称,而不是显示为arg0,arg1等,以增加可读性        return String.format("Hello, %s", name);    }}

The above annotated @webservice indicates that this is a WebService class, where annotations @webmethod, @WebParam are not required.

3. Service Release

There are two ways to publish your own webservice

    • Way One:
publicstaticvoidmain(String[] args) {    Endpoint.publish("http://localhost/HelloService"new Hello());}

WebService is released directly through the Endpoint.publish method, which is generally used for debugging. After you run the class, the browser accesses http://localhost/HelloService such as

    • Way two:

Published to the Web container. The following example illustrates the release configuration process with Tomcat
Create a new Sun-jaxws.xml file in the Web-inf directory (the XML file name is fixed and cannot be modified arbitrarily), as follows:

<?xml version= "1.0" encoding= "UTF-8"?  <endpoints   xmlns  = "http://java.sun.com/xml/ns/jax-ws/ri/runtime"  version  =;  << Span class= "Hljs-title" >endpoint  name  = " HelloService " implementation  =" Com.gvtv.learn.ws.server.Hello " url-pattern  =  "/helloservice" />  <!-- The url-pattern needs to be consistent--> with the url-pattern of the corresponding servlet in Web.  </ endpoints ;   

then modify Web. XML to add Listener and Servlets as follows:

<! DOCTYPE Web-app Public "-//sun Microsystems, INC.//DTD Web Application 2.3//en" "Http://java.sun.com/dtd/web-app_2_3. DTD "><web-app>    <display-name>Archetype Created Web Application</display-name>    <context-param>        <param-name>Contextconfiglocation</param-name>        <param-value>Classpath*:conf/spring-*.xml</param-value>    </context-param>    <listener>        <listener-class>Com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>    </listener>    <servlet>        <servlet-name>Sayhiservice</servlet-name>        <servlet-class>Com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>    </servlet>    <servlet-mapping>        <servlet-name>Sayhiservice</servlet-name>        <url-pattern>/helloservice</url-pattern>    </servlet-mapping>    <listener>        <listener-class>Org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <servlet>        <servlet-name>Springmvc</servlet-name>        <servlet-class>Org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>Contextconfiglocation</param-name>            <param-value>Classpath*:conf/springmvc-servlet.xml</param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>Springmvc</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping></Web-app>

The Url-pattern in the annotation sun-jaxws.xml needs to be consistent with the url-pattern of the servlet in Web. XML, as in the previous example/helloservice (which can be arbitrarily evaluated).
Launch Tomcat after entering the web address in the browser: http://localhost/jquery_ui/HelloService?wsdl (jquery_ui is my project name), as seen in the WSDL file indicates that the service was published successfully

4.java Client Call

Base code can be generated using the Wsimport tool that comes with the JDK

First, the CMD interface to switch to the project's Src\main\java directory under the second, the execution of commands
xxx\src\main\java>wsimport -encoding utf-8com.gvtv.learn.ws.client http://localhost/jquery_ui/HelloService?wsdl

Wsimport usage is as follows:

用法: wsimport [options] <WSDL_URI>其中 [options] 包括:  -d <directory>            指定放置生成的输出文件的位置  -encoding <encoding>      指定源文件所使用的字符编码  -help                     显示帮助  -keep                     保留生成的文件(指保留java源文件)  -p <pkg>                  指定目标程序包名

At this point, the generated source files can be seen under package com.gvtv.learn.ws.client as follows

Third, use the Java source file generated above to invoke the Web service. Examples are as follows:
publicclass Main {    publicstaticvoidmain(String[] args) {        new HelloService().getHelloPort();        String result = service.sayHello("屌丝!");        System.out.println(result);    }}

Can see print out information

Hello, 屌丝!

At this point, the Java version of the WebService server and client is complete, the process is very simple. Next time, add another point of knowledge

MVN building JAX-ws projects with server and client code implementations

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.