2

我知道如何使用 CXF 创建一个基于 REST 的 Web 服务,并将数据自动反序列化为对象。但是如何使用 CXF 创建基于 SOAP 的 Web 服务?我需要将 SOAP 调用数据类似地反序列化为 java 对象。

提前致谢

4

2 回答 2

2

我找到了解决方案,这就是它的进展方式......

POM.xml

<dependency>
  <groupId>org.apache.cxf</groupId>
  <artifactId>cxf-rt-frontend-jaxws</artifactId>
  <version>${cxf.version}</version>
  <exclusions>
        <exclusion>
              <groupId>wsdl4j</groupId>
              <artifactId>wsdl4j</artifactId>
        </exclusion>
        <exclusion>
              <groupId>org.eclipse.jetty</groupId>
              <artifactId>jetty-server</artifactId>
        </exclusion>
        <exclusion>
              <groupId>org.apache.geronimo.specs</groupId>
              <artifactId>geronimo-servlet_2.5_spec</artifactId>
        </exclusion>
  </exclusions>
</dependency>

<dependency>
  <artifactId>cxf-rt-transports-http</artifactId>
  <groupId>org.apache.cxf</groupId>
  <version>${cxf.version}</version>
</dependency>

豆类.xml

<!-- xmlns:jaxws="http://cxf.apache.org/jaxws" http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd -->

<jaxws:endpoint address="/mysws"
    implementor="com.company.project.MySoapService"
    id="mySoapService" />

爪哇

@WebService
public interface IMySoapService {
      MySoapResponse someOperation(MySoapRequest requestObj);
}

@WebService(endpointInterface = "com.company.project.IMySoapService")
public class MySoapService implements IMySoapService {
      public MySoapResponse someOperation(MySoapRequest requestObj) {
        MySoapResponse myResponse = null;

        try {
            myResponse = new MySoapResponse("12345", "6789");
        } catch (Exception ex) {
            myResponse = new MySoapResponse(false, 234, "", "");
            myResponse.setErrorMessage(ex.getMessage());
        }   

        return myResponse;
    }
}

希望对有需要的人有所帮助!

于 2012-05-28T07:18:33.137 回答
-1

添加依赖

<dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</artifactId>
        <version>${cxf.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http</artifactId>
        <version>${cxf.version}</version>
    </dependency>


    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.1</version>
    </dependency>

添加 web.xml

<web-app>
<display-name>CXF</display-name>


<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:ApplicationContext-cxf.xml</param-value>
</context-param>

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

<servlet>
    <servlet-name>CXFServlet</servlet-name>
    <display-name>CXF Servlet</display-name>
    <servlet-class>
        org.apache.cxf.transport.servlet.CXFServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>CXFServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

添加应用程序上下文

<?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:jaxws="http://cxf.apache.org/jaxws" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:cxf="http://cxf.apache.org/core" xmlns:task="http://www.springframework.org/schema/task"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
     http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd">



    <import resource="classpath:META-INF/cxf/cxf.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-extension-xml.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />

        <task:annotation-driven />
        <context:component-scan base-package="com.smoothexample">
            <context:include-filter type="annotation"
                expression="org.springframework.stereotype.Service" />
            <context:include-filter type="annotation"
                expression="org.springframework.stereotype.Component" />
            <context:include-filter type="annotation"
                expression="org.springframework.stereotype.Repository" />
        </context:component-scan>
        <context:annotation-config />
        <context:component-scan base-package="com.smoothexample" />
        <bean       class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" /> 
    <bean id="addressSoapService" class="com.smoothexample.cxf.soap.ws.impl.AddressSoapServiceImpl" />
        <jaxws:endpoint id="addressEndpointId"
        implementorClass="com.smoothexample.cxf.soap.ws.impl.AddressSoapServiceImpl"
            implementor="#addressSoapService" address="/addressSoapService">

        </jaxws:endpoint>
    </beans>

网络服务

package com.smoothexample.cxf.soap.ws;
import javax.jws.WebService;
import com.smoothexample.dto.Address;
    @WebService(endpointInterface = " com.smoothexample.cxf.soap.ws.AddressSoapService", 
        serviceName = "addressSoapService")
    public interface AddressSoapService {
        public Address getAddress()
                 throws Exception;

    }

执行

 package com.smoothexample.cxf.soap.ws.impl;

import com.smoothexample.cxf.soap.ws.AddressSoapService;
import com.smoothexample.dto.Address;

public class AddressSoapServiceImpl implements AddressSoapService {

    public Address getAddress() {

        return createAddress();
    }

private Address createAddress() {
    Address address = new Address();
    address.setStreetAddress("4800 abc Rd");
    address.setCity("abcd");
    address.setState("NJ");
    address.setCountry("US");
    address.setZip("10001");
    address.setAddressOptional("addressOptional");

    return address;
}

}

请在http://www.smoothexample.com/webservices/apache_cxf_soap_web_services.html找到更多详细信息

于 2016-10-26T22:54:02.520 回答