`

JAX-WS demo

    博客分类:
  • cxf
 
阅读更多

This example will lead you through creating your first service with doing "code first" development with JAX-WS.

This example corresponds to the java_first_jaxws example in the CXF distribution.

Setting up your build

The use of Apache Maven is recommended for your web service projects, as it will automatically bring in all necessary dependencies for your web service project. See the Maven pom.xml for this sample for the configuration needed. All samples provided by CXF use Apache Maven, except for the antbuild sample which shows how you can build CXF projects with Apache Ant instead.

The mvn dependency:list and mvn dependency:tree commands from the Maven Dependency Plugin will show all dependencies used by your project.

Writing your Service

First we'll write our service interface. It will have one operation called sayHi which says "Hello" to whoever submits their name.

@WebService
public interface HelloWorld {
 
    String sayHi(String text);
 
 
    /* Advanced usecase of passing an Interface in.  JAX-WS/JAXB does not
     * support interfaces directly.  Special XmlAdapter classes need to
     * be written to handle them
     */
    String sayHiToUser(User user);
 
 
    /* Map passing
     * JAXB also does not support Maps.  It handles Lists great, but Maps are
     * not supported directly.  They also require use of a XmlAdapter to map
     * the maps into beans that JAXB can use.
     */
    @XmlJavaTypeAdapter(IntegerUserMapAdapter.class)
    Map<Integer, User> getUsers();
}

To make sure your parameter is named correctly in the xml you should use:

@WebService
public interface HelloWorld {
    String sayHi(@WebParam(name="text") String text);
}

The @WebParam annotation is necessary as java interfaces do not store the Parameter name in the .class file. So if you leave out the annotation your parameter will be named arg0.

Our implementation will then look like this:

package demo.hw.server;
 
import java.util.LinkedHashMap;
import java.util.Map;
 
import javax.jws.WebService;
 
@WebService(endpointInterface = "demo.hw.server.HelloWorld",
            serviceName = "HelloWorld")
public class HelloWorldImpl implements HelloWorld {
    Map<Integer, User> users = new LinkedHashMap<Integer, User>();
 
 
    public String sayHi(String text) {
        System.out.println("sayHi called");
        return "Hello " + text;
    }
 
    public String sayHiToUser(User user) {
        System.out.println("sayHiToUser called");
        users.put(users.size() + 1, user);
        return "Hello "  + user.getName();
    }
 
    public Map<Integer, User> getUsers() {
        System.out.println("getUsers called");
        return users;
    }
 
}

The @WebService annotation on the implementation class lets CXF know which interface we want to create our WSDL with. In this case its simply our HelloWorld interface.

Publishing your service

System.out.println("Starting Server");
HelloWorldImpl implementor = new HelloWorldImpl();
String address = "http://localhost:9000/helloWorld";
Endpoint.publish(address, implementor);

whole code at http://svn.apache.org/repos/asf/cxf/trunk/distribution/src/main/release/samples/java_first_jaxws/src/main/java/demo/hw/server/Server.java

Alternatively you can use the following code. This gives you more control over the behaviour. For example you can add a logging interceptor:

HelloWorldImpl implementor = new HelloWorldImpl();
JaxWsServerFactoryBean svrFactory = new JaxWsServerFactoryBean();
svrFactory.setServiceClass(HelloWorld.class);
svrFactory.setAddress("http://localhost:9000/helloWorld");
svrFactory.setServiceBean(implementor);
svrFactory.getInInterceptors().add(new LoggingInInterceptor());
svrFactory.getOutInterceptors().add(new LoggingOutInterceptor());
svrFactory.create();

You could leave out the ServiceClass. But it is better to use it so the server and the client are created from the same interface. If you instead only use the implementation class subtle problems may occur.

Pointing your browser at http://localhost:9000/helloWorld?wsdl will display the wsdl for this service

Accessing your service

and client code to see it working is at http://svn.apache.org/repos/asf/cxf/trunk/distribution/src/main/release/samples/java_first_jaxws/src/main/java/demo/hw/client/Client.java

For the client there is also the alternative approach that gives you more flexibility. Of course like above the logging interceptors are optional but they help a lot when starting:

JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.getInInterceptors().add(new LoggingInInterceptor());
factory.getOutInterceptors().add(new LoggingOutInterceptor());
factory.setServiceClass(HelloWorld.class);
factory.setAddress("http://localhost:9000/helloWorld");
HelloWorld client = (HelloWorld) factory.create();
 
String reply = client.sayHi("HI");
System.out.println("Server said: " + reply);
System.exit(0);
分享到:
评论

相关推荐

    jax-ws webservice demo

    基于jax-ws 实现的web service client和server端的demo程序。 注:如果使用的是 myeclipse 时 server 部署到tomcat 启动的时候会报错 解决办法:找到myeclipse安装目录下的 plugins 目录里 查找 webservices-rt.jar,...

    jax-ws webservice简单demo

    在"demo-ws"这个压缩包中,可能包含了以下内容: 1. **SEI接口文件**:定义了Web服务提供的操作。 2. **服务实现类文件**:实现了SEI接口,包含具体业务逻辑。 3. **服务部署描述符(wsdd, Web Service Deployment ...

    jax-ws json demo

    jax-ws demo.分享一个从别处得到的helloworld demo

    metro-jax-ws-jaxws221x.zip

    【描述】中提到的“jax-ws开发dome”,这里的“dome”很可能是指“demo”,即示例项目。这表明压缩包内包含了演示如何使用JAX-WS创建、部署和消费Web服务的代码样本。它涵盖了从基本的SOAP(Simple Object Access ...

    webservice Demo注解+jax-ws

    在这个“webservice Demo注解+jax-ws”示例中,我们将深入探讨Web服务的注解使用以及基于Java API for XML Web Services (JAX-WS)的实现。 首先,让我们了解一下JAX-WS。它是Java平台的标准组件,用于创建和处理Web...

    JAX-WS开发的文件生成与部署相关全视频过程

    基于MyEclipse,压缩文件中含源码与视频以及一个说明文档 如果基于一个JAX-WS进行WebService开发,有很多教程,但是具体怎么更自动...相信大家根据视频操作一定能够成功写出一个JAX-WS的DEMO(下载后请先看说明文档)

    JAX_WS的demo

    **JAX-WS详解与应用** Java API for XML Web Services(JAX-WS)是Java平台上用于构建Web服务和客户端的API,它简化了Web服务的开发和使用。本篇文章将深入探讨JAX-WS的基本概念、核心组件以及如何通过一个简单的...

    webservice--javademo

    【标题】"webservice--javademo" 涉及的知识点主要集中在Java Web Services(JAX-WS)上,这是一种用于创建Web服务的技术。在Java领域,Web服务是一种允许不同应用程序之间交换数据的方式,它基于开放标准,如SOAP...

    jaxws-demo:JAX-WS Web 服务演示

    **在"jaxws-demo-master"项目中** 在这个项目中,你将找到一个完整的JAX-WS Web服务示例,包括服务端点接口定义、服务实现、WSDL生成和客户端调用的代码。通过这个项目,你可以学习如何在实际开发中使用JAX-WS创建...

    jaxws-webservice 的Demo

    【JAX-WS Web服务简介】 JAX-WS(Java API for XML Web Services)是Java平台上用于构建Web服务和客户端的API,它替代了早期的...通过对"jaxws-webservice"的Demo学习,你将能够熟练地运用JAX-WS创建和消费Web服务。

    wsdl2java源码-CXFDemo:一个关于CXF实现jax-ws规范的webservice

    一个关于CXF实现jax-ws规范的webservice #CXF框架 Apache CXF=Celtix+Xfire.是一个开源的一个webservice,可以与spring无缝集成。支持soap1.1、1.2、RESTtful或者CORBA。 ##使用CXF实现jax-ws规范的webservice ...

    jaxws的小demo

    在这个"jaxws的小demo"中,我们将深入理解JAX-WS的基本概念、工作原理以及如何在Java环境中使用它来开发Web服务。 1. **JAX-WS概述** JAX-WS是Java EE平台的一部分,主要用于简化Web服务的开发。它提供了从Java...

    jdk5 webservice demo

    这个"JDK5 WebService Demo"项目是一个理想的起点,帮助开发者理解和实践JAX-WS在JDK 5中的使用,掌握Web服务的基本原理和操作流程。通过这个示例,可以学习如何创建、部署和调用Web服务,以及如何进行客户端和...

    springMybatisWebservice

    在这个项目中,"demo"可能是一个简化的应用程序,演示了如何在Spring和MyBatis的基础上创建和使用JAX-WS Web服务。 结合这些信息,这个项目的核心目标是展示如何在MyEclipse中集成Spring、MyBatis和JAX-WS,以及...

    webService DEMO

    2. **JAX-WS**:掌握如何使用JAX-WS的注解(如@WebService、@WebServiceClient等)来创建服务提供者和服务消费者。 3. **REST原则**:理解RESTful架构风格,包括资源、URI、HTTP方法和状态码等基本概念。 4. **JAX...

    springwebservice

    在这个“springmybatis-jax-ws集成demo”中,我们可能会看到以下关键步骤: 1. **Spring与MyBatis集成**:通过Spring的DataSource、SqlSessionFactoryBean和MapperScannerConfigurer配置,我们可以将MyBatis的...

    webservice_server_client.zip

    本教程将深入探讨Java如何调用Web服务,主要分为两种方式:JAX-WS(Java API for XML Web Services)和Apache CXF。 首先,我们来看JAX-WS,它是Java平台标准版(Java SE)和企业版(Java EE)的一部分,为开发和...

    mpaf-spring-boot-demo

    概述 使用 Spring Boot 的 Metafour PAF 服务演示项目。 更改日志 0.0.1-M1 带有两个 RESTfull 服务端点的 Spring Boot: 两者都默认返回 JSON 数据,或者如果 Accept 标头是 ... JAX-WS commons - JAX-WS RI 的 S

    soapui-netbeans-demo

    教你如何使用soapui来测试netbeans下基于jax-ws的webservice

    webService-demo

    总的来说,"webService-demo"是一个学习和实践Web服务开发的好起点,它涵盖了SOAP协议、Java的JAX-WS框架以及基本的服务发布和调用流程。通过深入研究这个项目,开发者可以更好地理解和掌握Web服务的核心概念和技术...

Global site tag (gtag.js) - Google Analytics