Springboot注解开发WebService

本项目使用IDEA开发,Eclipse同理

一、项目初始化

打开IDEA,选择Spring boot生成器,填写项目相关信息

注意:若需要使用jdk8,服务器URL需要更换为start.aliyun.com

本文编辑时,start.spring.io要求jdk最低版本为:17

jdk版本不同,所需依赖版本也有所不同

下一步,勾选Spring Web Service

自jdk1.8之后,javax.* 被调整为:jakarta .*,请参考 cxf 4.0 说明:

Apache CXF -- 4.0 Migration Guide

jdk1.8 pom.xml文件依赖如下:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web-services</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
        <version>3.2.7</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>6.0.18.Final</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

jdk17 pom.xml文件依赖如下:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web-services</artifactId>
    </dependency>

    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-spring-boot-starter-jaxws</artifactId>
        <version>4.0.4</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>8.0.1.Final</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

二、添加接口

一个接口类可以写多个方法

package top.braycep.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public interface EndpointDemo {
    @WebMethod
    String doBiz(String request);
}

三、添加实现

此处REQUEST_RAW_CONTENT来源于拦截器中提取的原始请求报文,并非自带属性

package top.braycep.ws.impl;

import org.springframework.stereotype.Component;
import top.braycep.ws.EndpointDemo;

import javax.annotation.Resource;
import javax.xml.ws.WebServiceContext;

@Component
public class EndpointDemoImpl implements EndpointDemo {
    @Resource
    private WebServiceContext webServiceContext;

    @Override
    public String doBiz(String request) {
        Object rawContent = webServiceContext.getMessageContext().get("REQUEST_RAW_CONTENT");
        if (rawContent instanceof String && !((String) rawContent).isEmpty()) {
            System.out.println(rawContent);
        }

        return "doBiz By EndpointDemoImpl";
    }
}

四、发布服务

多个接口服务类,每个都需要注册

package top.braycep.ws.config;

import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import org.apache.cxf.transport.servlet.CXFServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.braycep.ws.EndpointDemo;

import javax.xml.ws.Endpoint;

@Configuration
public class WSConfig {

    @Autowired
    private EndpointDemo endpointDemo;

    @Autowired
    @Qualifier(Bus.DEFAULT_BUS_ID)
    public Bus bus;

    @Bean
    public ServletRegistrationBean<CXFServlet> servletServletRegistrationBean() {
        return new ServletRegistrationBean<>(new CXFServlet(), "/ws/*");
    }

    // 发布服务
    @Bean
    public Endpoint endpointDemo() {
        EndpointImpl endpoint = new EndpointImpl(bus, endpointDemo);
        endpoint.publish("/endpointDemo");
        return endpoint;
    }
}

另一种发布方式

// 方式一
@Bean
public Endpoint endpointDemo1() {
    EndpointImpl endpoint = new EndpointImpl(bus, endpointDemo);
    // 内部会调用 bean.create();
    endpoint.publish("/endpointDemo");
    return endpoint;
}

// 方式二
@Bean
public JaxWsServerFactoryBean endpointDemo2() {
    JaxWsServerFactoryBean bean = new JaxWsServerFactoryBean();
    bean.setAddress("/endpointDemo");
    bean.setServiceClass(EndpointDemoImpl.class);
    bean.setServiceBean(endpointDemo);
    bean.setBus(bus);
    // 必需
    bean.create();
    return bean;
}

五、测试

打开地址:http://127.0.0.1:8080/ws/

点击右侧WSDL链接,即可进入

使用soapui测试

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是使用Idea写一个Spring Boot项目开发Web Service的Demo: 1. 创建Spring Boot项目 在Idea中创建一个新的Spring Boot项目,选择Web和Spring Web Services依赖项。 2. 创建一个Web Service 在src/main/java目录下创建一个包,命名为com.example.demo.webservice,然后在该包下创建一个类,命名为HelloWebService。 ``` package com.example.demo.webservice; import org.springframework.stereotype.Component; import javax.jws.WebMethod; import javax.jws.WebService; @Component @WebService(serviceName = "HelloWebService") public class HelloWebService { @WebMethod public String sayHello(String name) { return "Hello, " + name + "!"; } } ``` 这里我们使用了注解@WebService来标注这是一个Web Service,并使用@WebMethod标注了其中一个方法。我们还使用了@Component注解来将此类标记为Spring Bean。 3. 配置Web Service 在application.properties文件中添加以下配置: ``` # Web Service spring.webservices.path=/ws ``` 这将Web Service的路径设置为/ws。 4. 发布Web Service 在启动类中添加以下代码: ``` import com.example.demo.webservice.HelloWebService; import org.apache.cxf.Bus; import org.apache.cxf.jaxws.EndpointImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import javax.annotation.PostConstruct; import javax.xml.ws.Endpoint; @SpringBootApplication public class DemoApplication { @Autowired private Bus bus; @Autowired private HelloWebService helloWebService; public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @PostConstruct public void endpoint() { EndpointImpl endpoint = new EndpointImpl(bus, helloWebService); endpoint.publish("/hello"); } } ``` 这里我们使用了CXF框架来发布Web Service。在启动类中,我们注入了一个Bus对象和一个HelloWebService对象,并使用EndpointImpl将Web Service发布到路径/hello。 5. 测试Web Service 现在,我们可以使用任何Web Service客户端来测试我们的Web Service了。在浏览器中访问http://localhost:8080/ws/hello?wsdl,你应该能够看到自动生成的WSDL描述文件。现在,你可以使用SoapUI等工具来测试我们的Web Service。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值