Frontend

Frontend

CXF provides the concept of frontend modeling, which lets you create web services 

using different frontend APIs. The APIs let you create a web service using simple 

factory beans and JAX-WS implementation. It also lets you create dynamic web 

service clients. The primary frontend supported by CXF is JAX-WS. We will look  

at how to use the Frontend programming model in the next chapter.

 

JAX-WS

JAX-WS is a specification that establishes the semantics to develop, publish, and 

consume web services. JAX-WS simplifies web service development. It defines 

Java-based APIs that ease the development and deployment of web services. 

The specification supports WS-Basic Profile 1.1 that addresses web service 

interoperability. It effectively means a web service can be invoked or consumed by 

a client written in any language. JAX-WS also defines standards such as JAXB and 

SAAJ. CXF provides support for complete JAX-WS stack.

 

JAXB provides data binding capabilities by providing a convenient way to map XML 

schema to a representation in Java code. The JAXB shields the conversion of XML 

schema messages in SOAP messages to Java code without the developers seeing 

XML and SOAP parsing. JAXB specification defines the binding between Java and 

XML Schema. SAAJ provides a standard way of dealing with XML attachments 

contained in a SOAP message.

 

JAX-WS also speeds up web service development by providing a library of 

annotations to turn Plain Old Java classes into web services and specifies a detailed 

mapping from a service defined in WSDL to the Java classes that will implement that 

service. Any complex types defined in WSDL are mapped into Java classes following 

 the mapping defined by the JAXB specification.

 

As discussed earlier, two approaches for web service development exist: Code-First 

and Contract-First. With JAX-WS, you can perform web service development using 

 one of the said approaches, depending on the nature of the application.

 

With the Code-first approach, you start by developing a Java class and interface and 

annotating the same as a web service. The approach is particularly useful where  

Java implementations are already available and you need to expose implementations 

as services.

 

You typically create a Service Endpoint Interface (SEI) that defines the service 

methods and the implementation class that implements the SEI methods. The 

consumer of a web service uses SEI to invoke the service functions. The SEI directly 

corresponds to a wsdl:portType element. The methods defined by SEI correspond 

to the wsdl:operation element. 

@WebService

public interface OrderProcess {

    String processOrder(Order order);

}

 

JAX-WS makes use of annotations to convert an SEI or a Java class to a web  

service. In the above example, the @WebService annotation defined above the 

interface declaration signifies an interface as a web service interface or Service 

Endpoint Interface.

 

In the Contract-first approach, you start with the existing WSDL contract, and generate 

Java class to implement the service. The advantage is that you are sure about what to 

expose as a service since you define the appropriate WSDL Contract-first. Again the 

contract definitions can be made consistent with respect to data types so that it can be 

easily converted in Java objects without any portability issue. In Chapter 3 we will look 

at how to develop web services using both these approaches.

 

 WSDL contains different elements that can be directly mapped to a Java class 

that implements the service. For example, the wsdl:portType element is directly 

mapped to SEI, type elements are mapped to Java class types through the use of Java 

Architecture of XML Binding (JAXB), and the wsdl:service element is mapped to 

a Java class that is used by a consumer to access the web service.

 

The WSDL2Java tool can be used to generate a web service from WSDL. It has various 

options to generate SEI and the implementation web service class. As a developer, 

you need to provide the method implementation for the generated class. If the WSDL 

includes custom XML Schema types, then the same is converted into its equivalent 

Java class.

 

Simple frontend

Apart from JAX-WS frontend, CXF also supports what is known as 'simple frontend'. 

The simple frontend provides simple components or Java classes that use reflection 

to build and publish web services. It is simple because we do not use any annotation 

to create web services. In JAX-WS, we have to annotate a Java class to denote it 

as a web service and use tools to convert between a Java object and WSDL. The 

simple frontend uses factory components to create a service and the client. It does 

so by using Java reflection API. In Chapter 3 we will look at how to develop simple 

frontend web services

The following code shows a web service created using simple frontend:

// Build and publish the service

OrderProcessImpl orderProcessImpl = new OrderProcessImpl();

ServerFactoryBean svrFactory = new ServerFactoryBean();

svrFactory.setServiceClass(OrderProcess.class);

svrFactory.setAddress("http://localhost:8080/OrderProcess");

svrFactory.setServiceBean(orderProcessImpl);

svrFactory.create();

你可能感兴趣的:(api,frontend)