当前位置: 代码网 > it编程>编程语言>Java > Spring WebService的两种主流实现方式‌及说明

Spring WebService的两种主流实现方式‌及说明

2026年04月27日 Java 我要评论
spring webservice主流实现方式‌spring-ws(spring web services)‌:采用 ‌contract first(自顶向下)&zwnj

spring webservice主流实现方式‌

spring-ws(spring web services)‌:采用 ‌contract first(自顶向下)‌ 方式,先定义 xsd/wsdl,再生成 java 代码。适用于企业级、高可维护性的 soap 服务。

spring boot + jax-ws(通常用 apache cxf)‌:采用 ‌contract last(自底向上)‌ 方式,通过 @webservice 注解将 java 类暴露为 web service。开发更快速,适合快速原型或内部系统集成。

1、‌spring-ws(contract first)核心步骤‌

添加依赖‌(maven):

    <dependency>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-starter-web-services</artifactid>
    </dependency>
    <dependency>
        <groupid>wsdl4j</groupid>
        <artifactid>wsdl4j</artifactid>
    </dependency>

定义 xsd schema‌(如 src/main/resources/xsd/login.xsd):

<xs:schema targetnamespace="http://example.com/ws/login"
               elementformdefault="qualified"
               xmlns:xs="http://www.w3.org/2001/xmlschema">
        <xs:element name="loginrequest">
            <xs:complextype>
                <xs:sequence>
                    <xs:element name="username" type="xs:string"/>
                    <xs:element name="password" type="xs:string"/>
                </xs:sequence>
            </xs:complextype>
        </xs:element>
        <xs:element name="loginresponse">
            <xs:complextype>
                <xs:sequence>
                    <xs:element name="result" type="xs:string"/>
                </xs:sequence>
            </xs:complextype>
        </xs:element>
    </xs:schema>

自动生成 java 类‌(使用 jaxb2 maven 插件):

<plugin>
        <groupid>org.codehaus.mojo</groupid>
        <artifactid>jaxb2-maven-plugin</artifactid>
        <version>1.6</version>
        <configuration>
            <schemadirectory>${project.basedir}/src/main/resources/xsd/</schemadirectory>
            <outputdirectory>${project.basedir}/src/main/java/</outputdirectory>
        </configuration>
        <executions>
            <execution>
                <goals><goal>xjc</goal></goals>
            </execution>
        </executions>
    </plugin>

创建 endpoint‌:

@endpoint
    public class loginendpoint {
        private static final string namespace_uri = "http://example.com/ws/login";
        @payloadroot(namespace = namespace_uri, localpart = "loginrequest")
        @responsepayload
        public loginresponse login(@requestpayload loginrequest request) {
            // 业务逻辑
            loginresponse response = new loginresponse();
            response.setresult("success");
            return response;
        }
    }

配置 webserviceconfig‌:

    @configuration
    @enablews
    public class webserviceconfig extends wsconfigureradapter {
        @bean
        public servletregistrationbean messagedispatcherservlet(applicationcontext context) {
            messagedispatcherservlet servlet = new messagedispatcherservlet();
            servlet.setapplicationcontext(context);
            servlet.settransformwsdllocations(true);
            return new servletregistrationbean(servlet, "/ws/*");
        }
        @bean(name = "login")
        public defaultwsdl11definition defaultwsdl11definition(xsdschema schema) {
            defaultwsdl11definition wsdl = new defaultwsdl11definition();
            wsdl.setporttypename("loginport");
            wsdl.setlocationuri("/ws");
            wsdl.setschema(schema);
            return wsdl;
        }
        @bean
        public xsdschema schema() {
            return new simplexsdschema(new classpathresource("xsd/login.xsd"));
        }
    }

访问 wsdl‌:

启动应用后,访问 http://localhost:8080/ws/login.wsdl 查看服务描述。

2、‌spring boot + cxf(contract last)

添加依赖‌:

    <dependency>
        <groupid>org.apache.cxf</groupid>
        <artifactid>cxf-spring-boot-starter-jaxws</artifactid>
        <version>3.3.1</version>
    </dependency>

定义接口与实现类‌:

    @webservice
    public interface orderws {
        @webmethod
        order getorderbyid(int id);
    }
    @webservice(endpointinterface = "com.example.orderws")
    public class orderwsimpl implements orderws {
        @override
        public order getorderbyid(int id) {
            return new order(id, "product", 999.99);
        }
    }

配置发布端点‌:

    @configuration
    public class webserviceconfig {
        @autowired
        private orderwsimpl orderwsimpl;
        @bean
        public endpoint endpoint() {
            endpointimpl endpoint = new endpointimpl(new springbus(), orderwsimpl);
            endpoint.publish("/orderws");
            return endpoint;
        }
    }

访问 wsdl‌:

http://localhost:8080/orderws?wsdl

3、‌注意事项‌

  • spring-ws‌ 更适合需要严格契约控制、长期维护的系统。
  • cxf + jax-ws‌ 开发更快,但耦合度较高,适合内部服务或快速迭代场景。
  • 避免使用过时的 axis1/axis2,除非维护遗留系统 ‌。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com