Use CXF to do Webservice simple example

Apache CXF is a The open source framework provides a reliable infrastructure for easily building and developing Web services. It allows the creation of high-performance and scalable services that you can deploy in Tomcat and Spring-based lightweight containers, as well as on more advanced servers such as Jboss, IBM® WebSphere® or BEA WebLogic.

The framework provides the following functions:

  • Web service standard support: CXF supports the following Web service standards:
    • Java API for XML Web Services (JAX-WS)
    • SOAP
    • Web Services Description Language (WSDL)
    • Message Transmission Optimization Mec hanism, MTOM)
    • WS-Basic Profile
    • WS-Addressing
    • WS-Policy
    • WS-ReliableMessaging
    • WS-Security

  • Front-end modeling: CXF provides front-end modeling The concept allows you to use different front-end APIs to create web services. The API allows you to use simple factory beans and implement them through JAX-WAS to create Web services. It also allows you to create dynamic web service clients.
  • Tool support: CXF provides Different tools for converting between Java Beans, Web services, and WSDL. It provides support for Maven and Ant integration, and seamlessly supports Spring integration.
  • RESTful service support:CXF support representative State transfer (Representational State Transfer, RESTful) service concept, and supports JAX-RS implementation of the Java platform. (Part 2 of this series will provide more information about RESTful services.)
  • Support for different transfers and bindings: CXF supports different kinds of transfers, from XML to Comma Separated Value (CSV). In addition to supporting SOAP and HTTP protocol binding, it also supports Java Architecture for XML Binding (JAXB) and AEGIS data binding.
  • Support for non-XML binding: CXF supports non-XML bindings such as JavaScript Object Notation (JSON) and Common Object Request Broker Architecture (CORBA). It also supports Java Business Integration (JBI) architecture and Service Component Architecture (SCA).
  • code first or xml first: support Use code first or xml first to create web services.

Create an independent web service with the help of annotation.

Preparation: Import the required jar packages for the new project:

Dependent packages:

Commons-logging-1.1.jar
ger_spec-on-activation_1.1 1.0-M1.jar (or Sun’s Activation jar)
geronimo-annotation_1.0_spec-1.1.jar ( JSR 250)
Geronimo-javamail_1.4_spec-1.0-M1.jar (or Sun’s JavaMail jar)
onimo servlet_2.5_spec-1.1-M1.jar (or Sun’s Servlet jar)
Geronimo-ws-metadata_2.0_spec-1.1.1.jar (JSR 181)
jaxb-api-2.1.jar
jaxb-api-2.1.jar
jaxb-api-2.1.jar
Jetty- 0px- 0px- util-style padding: 0px- util-style padding-6.1.5.jar
Neethi-2.0.jar
jar-api- 1.3. margin:0px; padding:0px”> saaj-impl-1.3.jar
stax-api-1.0.1.jar
j -1 6.1 dl ws jar
Jar style=”margin:0px; padding:0px”> jargin=”margin-1.2. Xml Schema: 1.2. 0px; padding:0px”> xml-resolver-1.2.jar xml-resolver-1.2.jar

spring jar package, used to support xml configuration:

aopalliance-1.0.jar
2.0. 4.jar
Jar
spring-beans-2.0.4.jar
-context-2. 0.4.-context br style=”margin:0px; padding:0px”> spring-web-2.0.4.jar

CXF jar package:

cxf-2.1.jar

The above jar package can be downloaded from the official apache website apache-cxf-2.1.2.zip, and then obtained from the apache-cxf-2.1.2/lib directory

1 Service point interface first.

package com.demo;

import java.util.List;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface HelloWorld {
String sayHi(@WebParam(name=”text”)String text);
String HiToUser(User user);< br style="margin:0px; padding:0px"> String[] SayHiToUserList(List userList);
}

2 Write service implementation

p ackage com.demo;

import java.util.LinkedHashMap;
import java.util. List;
import java.util.Map;

import javax.jws.WebService;< /span>

@WebService(endpointInterface=”com.demo.HelloWorld”, serviceName=”Hello World”)
public class HelloWorldImpl implements HelloWorld {

Map users = new LinkedHashMap();


Public String sayHi(String text) {
Return “Hello” + text;

public String sayHiToUser(User user) {
users.put(users.size()+1, user) style “margin:0px; padding:0px”> Return “Hello “+ user.getName();
Margin=” }

Public String[] SayHiToUserList(List userList) {
(String[] result = new String[userList.size] =”margin:0px; padding:0px”> Int i=0; int i=0;
Int i=0; padding:0px”> Margin:0px; padding:0px”> Margin:0px; padding:0px”> Margin:0px; padding:0px”> Margin:0px; padding:0px; style:0margin:0px;userList;userList style:0 “> Result[i] = “Hello “+ u.getName();
i++;
padding:0px”> padding:0px} return result; padding:0px br style=”margin:0px; padding:0px”> }

3 Write the webServiceApp.java class to expose web services.

package com.demo;

import javax.xml.ws.Endpoint;

public class webServiceApp {
Public static void main(String[] args) {
System.out.println(“web style service start”): 0px ; p adding:0px”> HelloWorldImpl implementor= new HelloWorldImpl();
“Margin:0px; padding:0px”> “margin:0px; padding:0px”> “margin:0px; padding:0px”> “margin:0px; padding:0px”>
String address=” ://localhost:8080/helloWorld“;
Endpoint.publish(address, implementor) ;
System.out.println(“web service started”);
Margin style=”margin: 0px; padding:0px”> }

4 Run webServiceApp.java class to start the service. Visit http://localhost:8080/helloWorld?wsdl to see if it is displayed

wsdl.

5 Write client access services.

package com.demo;

import java.util.ArrayList;
import java.util.List;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.context.support.ClassPathXmlApplicationContext;

< p style="mar gin:10px auto; padding-top:0px; padding-bottom:0px; color:rgb(73,73,73); font-size:14px; line-height:22.4px"> public class HelloWorldClient {
public static void main(String[] args) {
Bean Proxy {
svr = new JaxWsProxyFactoryBean();
svr.setServiceClass(HelloWorld.class);
svr.setServiceClass(HelloWorld.class);
/span>http://localhost:8080/helloWorld“);
HelloWorld hw = (HelloWorld) svr.create();
() User= new User= new style margin:0px; padding:0px”> user.setName(“Tony”);
user.setDescription(“test”);
System.out.println(hw.sayHiToUser(user));

}

6 test: run webServiceApp.java class to start the service, and then run HelloWorldClient.java to access the service.

two integrated into spring.

< /span>

1 in web.xml Join:




index.jsp
span>



contextConfigLocation
WEB-INF/classes/applicationContext.xml




org.springframework.web.context.Context. Listener


CXFServlet
CXFServlet

org.apache.cxf.transport.servlet.CXFServlet
style “margin:0px; padding:0px” > 1


CXF Servlet
url-pattern>/webservice/*


web-app>

2 Add in applicationContext.xml:


http ://www.springframework.org/schema/beans

xmlns:xsi=”
http://www.w3.org/2001/XMLSchema-instance
xmlns:jaxws=”
http://cxf.apache.org/jaxws
xsi:schemaLocation=”
http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws< span style="margin:0px; padding:0px">http://cxf.apache.org/schemas/jaxws.xsd “>






hello hello style=”World” padding:0px”> padding:0px”> hello padding:0px> hello padding:0px> padding:0px” padding:0px” padding:0px; padding:0px” >
factory-bean=” clientFactory” factory-method=”create”/>



value=”margin:0px; padding:0px”>http://localhost:8080/s/ webservice/helloWorld”/>

Note: Here you need to add xmlns:jaxws=”http://cxf.apache.org/jaxwsand

              http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd

 

  3  修改客户端。

      package com.demo;

     import java.util.ArrayList;
     import java.util.List;

     import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
     import org.springframework.context.support.ClassPathXmlApplicationContext;

 

     public class HelloWorldClient {
              public static void main(String[] args) {
                        HelloWorld client = (HelloWorld)context.getBean(“client”);
                        User user1 = new User();
                        user1.setName(“Tony”);
                        user1.setDescription(“test”);
                        User user2 = new User();
                        user2.setName(“freeman”);
                        user2.setDescription(“test”);
                        List userList= new ArrayList();
                        userList.add(user1);
                        userList.add(user2);
                        String[] res = client.SayHiToUserList(userList);
                        System.out.println(res[0]);
                        System.out.println(res[1]);          

              }
     }

 

  4  发布工程 启动web服务器(我用 tomcat 6 ).

 

  5 访问 http://localhost:8080/s/webservice/helloWorld?wsdl  查看是否显示 wsdl 。

 

  6  run run HelloWorldClient.java 来访问服务。



原文:http://www.cnblogs.com/frankliiu-java/articles/1641949.html

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3520 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.