如何开发一个Service

CXF provides you with many options to build services. This guide is meant to give you a quick overview of those options and help you orient yourself quickly with CXF.

Different Types Of Services

CXF support three major types of services:

  • SOAP - this page summarizes the options for creating SOAP services.
  • REST-ful - REST support is described here.
  • CORBA

JAX-WS Annotated Services from Java

The JAX-WS APIs include a set of annotations which allow you to build services using annotated classes. These services are based on a single class which contains a set of operations.

Here's a simple example:

@WebService
public class Hello {
   public String sayHi(String name) {
     return "Hello " + name;
   }
}

JAX-WS includes many more annotations as well such as:

  • @WebMethod - allows you to customize the operation name, exclude the operation from inclusion in the service, etc
  • @WebParam - allows you to customize a parameter's name, namespace, direction (IN or OUT), etc
  • @WebResult - allows you to customize the return value of the web service call

Data is marshalled from XML to Java and vice versa via the JAXB data-binding.

Services are publish via one of two means:

  • The JAX-WS standard Endpoint APIs
  • CXF's XML configuration format - i.e. <jaxws:endpoint ... />

More Information: A simple JAX-WS service, Developing a JAX-WS Service (goes into much more depth), Writing a service with Spring

JAX-WS Annotated Services from WSDL

If you have existing WSDLs for your service or wish to write your WSDL first and then generate classes, CXF has many tools to help you do this.

The WSDL2Java tool will generate a JAX-WS annotated service and server stub from your WSDL. You can run it one of three ways:

Note that CXF generally restricts WSDL support to WSI-BP, not the full WSDL 1.1 specification.

There is also a Simple Frontend that allows you to create services without usage of Java annotations, using XML configuration files instead.

JAX-WS Providers

JAX-WS Providers allow you to create services which work at the message level - as opposed to the operation level as with annotated classes. The have a single operation "invoke" which receives either the message payload (i.e. the SOAP Body) or the whole message itself (i.e. the SOAP Envelope).

Here's a simple example:

@WebServiceProvider
public class HelloProvider {
   public Source invoke(Source request) {
     return ....;
   }
}

Services are publish via one of two means:

  • The JAX-WS standard Endpoint APIs
  • CXF's XML configuration format - i.e. <jaxws:endpoint ... />

More Information: Developing a JAX-WS Service

Javascript

CXF provides a Javascript module which allows you to build services in Javascript with the Java Rhino library. One advantage of this is that you can use E4X to interact more easily with the XML. For more information see the JavaScript page.

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值