cxf的soap风格+spirng4+maven 服务端

简介

SOAP

比较复杂,基于XML,有对应规范;REST利用HTTP请请求方式GET,POST,PUT,delete约定具体操作。简单的说,SOAP通过传输XML,XML定义了请求和响应的具体数据,要进行的操作等等.

REST

另一种约定,比如请求/user/100这个RUL,GET方式返回id为100的user信息,put方式则是更新id为1001的user信息,DELETE删除等。

接下来记录搭建cxf服务端

1、pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.jacky</groupId>
    <artifactId>cxf_demo</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>cxf_demo Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>2.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-common</artifactId>
            <version>2.5.4</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-core</artifactId>
            <version>2.6.1</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>2.6.1</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
        </plugins>
        <finalName>cxf_demo</finalName>
    </build>
</project>

2、applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://cxf.apache.org/jaxws
    http://cxf.apache.org/schemas/jaxws.xsd">
    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-xml.xml"/>
    <bean id="getInfoServiceImpl" class="com.jacky.impl.GetInfoServiceImpl"></bean>
    <!-- wsdl地址为 http://www.localhost:8080/cxf_demo/cxf/getInfoService?wsdl -->
    <jaxws:endpoint id="getInfoService" implementor="#getInfoServiceImpl" address="/getInfoService"></jaxws:endpoint>
</beans> 

3、Info.java

package com.jacky.entity;

public class Info {
    private String name;
    private int age;
    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public int getAge()
    {
        return age;
    }
    public void setAge(int age)
    {
        this.age = age;
    }
}

4、GetInfoService.java

package com.jacky;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;

import com.jacky.entity.Info;

@WebService()
public interface GetInfoService {
    @WebMethod(operationName = "add")
    @WebResult(name = "result")
    public int add(@WebParam(name = "number1") int number1, @WebParam(name = "number2") int number2); 

    @WebMethod(operationName = "getInfo")
    @WebResult(name = "result")
    public Info getInfo(@WebParam(name = "name") String name, @WebParam(name = "age") int age);
}

5、GetInfoServiceImpl.java

package com.jacky.impl;

import javax.jws.WebService;

import com.jacky.GetInfoService;
import com.jacky.entity.Info;
/*
 * targetNamespace 的值是服务实现类的接口包的倒序,例如我的接口全路径为com.jacky.GetInfoService,则
 * 名称空间为http://jacky.com/,否则,客户端动态调用会找不到方法
 * */
@WebService(endpointInterface = "com.jacky.GetInfoService",targetNamespace="http://jacky.com/")
public class GetInfoServiceImpl implements GetInfoService {

    @Override
    public int add(int number1, int number2) {
        return number1+number2;
    }

    @Override
    public Info getInfo(String name, int age) {
        Info info = new Info();
        info.setName(name);
        info.setAge(age);
        return info;
    }

}

6.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name> maven project</display-name>
   <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <servlet>
        <servlet-name>CXFService</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
   </servlet>
   <servlet-mapping>
        <servlet-name>CXFService</servlet-name>
        <url-pattern>/cxf/*</url-pattern>
   </servlet-mapping>
</web-app>

到了这里就soap服务端就搭建好了,可以测试了

地址:http://www.localhost:8080/cxf_demo/cxf/getInfoService?wsdl

时间: 2024-08-01 10:43:00

cxf的soap风格+spirng4+maven 服务端的相关文章

cxf的soap风格+spirng4+maven 客户端

上篇博客介绍了,cxf的soap风格的服务端,现在我们写客户端来调用 1.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org

Maven服务端部署

Maven服务端部署 更改/bin/jsw/conf/wrapper.conf,设置wrapper.java.commond=D:\Program Files\Java\jdk1.8.0_40\bin\java.exe 否则,服务安装后无法启动 ? 安装 在CMD下找到nexus.bat,执行nexus.bat start即可. Http://localhost:8081/nexus,若能打开,说明服务安装成功 如果要更改端口号,可以conf/nexus.properties中更改 ? 默认有两

Spring+SpringMVC+MyBatis+Maven 服务端XML配置

项目目录结构 spring-mybatis.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.spr

CXF+Spring+Hibernate实现RESTful webservice服务端实例

1.RESTful API接口定义 /* * Copyright 2016-2017 WitPool.org All Rights Reserved. * * You may not use this file except in compliance with the License. * A copy of the License is located at * http://www.witpool.org/licenses * * or in the "license" file

使用Apache CXF开发WebServices服务端、客户端

在前一篇的博客中,我使用Xfire1.x来开发了WebServies的服务端. 但是如果你访问Apache的官网,可以看到xfire已经被合并了. 最新的框架叫做CXF. Apache CXF = Celtix + XFire. CXF 继承了 Celtix 和 XFire 两大开源项目的精华, 提供了对 JAX-WS 全面的支持,并且提供了多种 Binding .DataBinding.Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code Fi

【JMeter4.0学习(三)】之SoapUI创建WebService接口模拟服务端以及JMeter测试SOAP协议性能测试脚本开发

目录: 创建WebService接口模拟服务端 下载SoapUI 新建MathUtil.wsdl文件 创建一个SOAP项目 接口模拟服务端配置以及启动 [阐述]:首先应该遇到了一个共同的问题,JMeter3.2之后就没有WebService(SOAP) Request,后来经过查询网上资料得知其实可以用HTTP请求来操作,结果是一样的. 具体资料大家可以参照原文<Jmeter测试SOAP协议(Jmeter 3.3)>感谢作者:stone9159 [步骤]: 一.创建WebService接口模拟

PHP写webservice服务端

1) WebService技术介绍 WebService是一种跨编程语言和跨操作系统平台的远程调用技术.仅仅有通过Web Service,client和server才可以自由的用HTTP进行通信.不论两个程序的平台和变成语言是什么. XML.SOAP和WSDL是Web Service平台的三大技术: WebService採用HTTP协议数据传输.採用XML格式封装数据,即XML中说明调用远程服务对象的哪个方法.传递的參数是什么.以及服务对象的返回结果是什么. XML是WebService平台中表

webservice -- cxf客户端调用axis2服务端

背景: 有个项目, 需要由第三方提供用户信息, 实现用户同步操作, 对方给提供webservice接口(axis2实现)并也使用axis2作主客户端调用我方提供的webservice接口 起初, 由于项目使用了spring, 且spring可与cxf较好的集成, 所以也就选用了cxf, 可问题随之出现, 接口可以调用到, 接口的具体方法也可以调用到, 但是, 1. cxf作为客户端, 获取服务端返回值时均为null. 2. cxf作为服务端, 获取axis2客户端传来的参数时, 也均为null.

服务端REST与SOAP的探讨

REST简介 在开始我们的正式讨论之前,让我们简单看一下REST的定义. REST(Representational State Transfer)是Roy Fielding提出的一个描述互联系统架构风格的名词.为什么称为REST?Web本质上由各种各样的资源组成,资源由URI唯一标识.浏览器(或者任何其它类似于浏览器的应用程序)将展示出该资源的一种表现方式,或者一种表现状态.如果用户在该页面中定向到指向其它资源的链接,则将访问该资源,并表现出它的状态.这意味着客户端应用程序随着每个资源表现状态