springBoot 发布 webService

一、服务端编码

1.1 添加 maven 依赖

   <!-- cxf实现webservice相关依赖-->
   <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-frontend-jaxws</artifactId>
        <version>3.2.4</version>
    </dependency> 
    <dependency>
       <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http</artifactId>
       <version>3.2.4</version>
   </dependency>
   <!--alibaba fastjson-->
   <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.4</version>
   </dependency>

1.2 编写服务类

        编写 model 类,如下

public class Teacher {
	private String name;
	private Integer age;
	private String sex;
	private Date birthday;

	// getter、setter

	public Teacher(String name, Integer age, String sex, Date birthday) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.birthday = birthday;
	}
}

        编写需要发布的接口和实现类,如下:

// @WebService:主要用于对接口、类进行注解,表示要发布的Web服务。
@WebService
public interface TeacherService {

    // @WebMethod:表示要发布的方法
	@WebMethod
	String getTeacherName(@WebParam(name="teacherId") String teacherId);
	
    // @WebParam:表示要发布方法的参数,默认的参数名称是args0、args1等,如果有这个注解,teacherId替代args0。
	@WebMethod
	String getUser(String teacherId);
}
// targetNamespace 为接口包所在的路径
// endpointInterface 为需要发布的接口路径
@WebService(targetNamespace="http://service.webService.com/",endpointInterface="com.webService.service.TeacherService")
public class TeacherServiceImpl implements TeacherService{
	@Override
	public String getTeacherName(String teacherId) {
		
		return "我拿到了请求的teacherId"+teacherId;
	}
	@Override
	public String getUser(String teacherId) {
		System.out.println("此次请求的teacherId为"+teacherId);
		Teacher t = new Teacher("王老师",40,"男",new Date());
		return JSON.toJSONString(t);
	}
}

        编写发布接口的代码如下:

@Configuration
public class PublishWebService {

	// 如果这个地方的名字是 dispatcherServlet 则其他的请求也会被 webservice 拦截,访问不了
	@Bean
	public ServletRegistrationBean disServlet() {
		return new ServletRegistrationBean(new CXFServlet(), "/test/*");
	}
	
	@Bean(name = Bus.DEFAULT_BUS_ID)
	public SpringBus springBus() {
		return new SpringBus();
	}
	
	@Bean
	public TeacherService teacherService() {
		return new TeacherServiceImpl();
	}
	@Bean
	public Endpoint endpoint() {
		EndpointImpl endpoint = new EndpointImpl(springBus(), teacherService());
		endpoint.publish("/user");
		return endpoint;
	}
}

       项目文件结构如下:

1.3 启动测试

        启动项目,在浏览器输入: http://localhost:8080/test/user?wsdl 返回结果如下,证明发布成功了。

二、客户端测试 

2.1 客户端编码

       测试服务的 client 代码如下:

public class WebServiceClient {

	public static void main(String[] args) throws Exception {
		
		JaxWsDynamicClientFactory dcf =JaxWsDynamicClientFactory.newInstance();
		org.apache.cxf.endpoint.Client client =dcf.createClient("http://localhost:8080/test/user?wsdl");
		
		// getUser 为接口中定义的方法名称  张三为传递的参数   返回一个 Object 数组
		Object[] objects=client.invoke("getTeacherName","411001");
		
		// 输出调用结果
		System.out.println("*****"+objects[0].toString());
	}
}

       返回结果如下:

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
### 回答1: Spring Boot可以轻松地发布Web服务,只需要遵循以下步骤: 1. 创建一个Spring Boot项目,并添加Web服务的依赖。 2. 创建一个WebService类,并使用@WebService注解标注该类。 3. 在WebService类中定义WebService的方法,并使用@WebMethod注解标注该方法。 4. 在Spring Boot的配置文件中添加WebService的配置信息。 5. 启动Spring Boot应用程序,并访问WebService的URL地址,即可调用WebService的方法。 以上就是使用Spring Boot发布WebService的基本步骤。 ### 回答2: Spring Boot 是一款非常流行的开源 Java 框架,它的特点是快速构建配置后端应用程序。它提供了很多功能,其中一个重要的特点是支持 WebService。下面我将介绍 Spring Boot 如何发布 WebService。 1. 添加依赖 Spring Boot 通过添加spring-boot-starter-web-services依赖来支持 WebService。可以在pom.xml文件中添加以下代码: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> ``` 2. 创建 Endpoint Spring Boot 需要通过 Endpoint 来发布 WebService。创建 Endpoint 的方式有两种: (1)使用 @Endpoint 注解标记的类。这种方式适用于需要手动管理 Endpoint 的情况。 (2)使用 @WebService 注解标记的类。这种方式适用于 Endpoint 的创建和管理都由 Spring 进行管理的情况。 例如,我们创建一个 HelloWorld 的 Endpoint,代码如下: ```java @Endpoint public class HelloWorldEndpoint { @PayloadRoot(namespace = "http://example.com", localPart = "HelloRequest") @ResponsePayload public HelloResponse sayHello(@RequestPayload HelloRequest request) { HelloResponse response = new HelloResponse(); response.setMessage("Hello " + request.getName() + "!"); return response; } } ``` 3. 配置 Endpoint 要发布一个 Endpoint,需要将其向 JAX-WS 发布,可以在配置类中添加以下代码: ```java @Configuration @EnableWs public class WebServiceConfig extends WsConfigurerAdapter { @Bean public ServletRegistrationBean<MessageDispatcherServlet> messageDispatcherServlet(ApplicationContext context) { MessageDispatcherServlet servlet = new MessageDispatcherServlet(); servlet.setApplicationContext(context); servlet.setTransformWsdlLocations(true); return new ServletRegistrationBean<>(servlet, "/ws/*"); } @Bean(name = "hello") public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema helloSchema) { DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition(); wsdl11Definition.setPortTypeName("HelloPort"); wsdl11Definition.setLocationUri("/ws"); wsdl11Definition.setTargetNamespace("http://example.com"); wsdl11Definition.setSchema(helloSchema); return wsdl11Definition; } @Bean public XsdSchema helloSchema() { return new SimpleXsdSchema(new ClassPathResource("xsd/hello.xsd")); } } ``` 4. 创建 WSDL 创建一个包含 Endpoint 信息的 WSDL 文件,例如 hello.xsd,代码如下: ```xml <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://example.com" targetNamespace="http://example.com"> <xs:element name="HelloRequest" type="tns:helloRequest"/> <xs:element name="HelloResponse" type="tns:helloResponse"/> <xs:complexType name="helloRequest"> <xs:sequence> <xs:element name="name" type="xs:string"/> </xs:sequence> </xs:complexType> <xs:complexType name="helloResponse"> <xs:sequence> <xs:element name="message" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:schema> ``` 5. 启动应用 将所有代码打包成 WAR 包,并部署到 Tomcat 等 Web 服务器上。然后就可以通过 http://localhost:8080/ws/hello.wsdl 访问 WSDL 文件,例如: ```xml <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://example.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://example.com"> <wsdl:types> <xsd:schema targetNamespace="http://example.com"> <xsd:import namespace="http://example.com" schemaLocation="hello.xsd"/> </xsd:schema> </wsdl:types> <wsdl:message name="HelloRequest"> <wsdl:part name="parameters" element="tns:HelloRequest"/> </wsdl:message> <wsdl:message name="HelloResponse"> <wsdl:part name="parameters" element="tns:HelloResponse"/> </wsdl:message> <wsdl:portType name="HelloPort"> <wsdl:operation name="sayHello"> <wsdl:input message="tns:HelloRequest"/> <wsdl:output message="tns:HelloResponse"/> </wsdl:operation> </wsdl:portType> <wsdl:binding name="HelloPortSoap11" type="tns:HelloPort"> <soap11:binding xmlns:soap11="http://schemas.xmlsoap.org/wsdl/soap/" style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="sayHello"> <soap11:operation soapAction=""/> <wsdl:input> <soap11:body use="literal"/> </wsdl:input> <wsdl:output> <soap11:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="HelloService"> <wsdl:port name="HelloPort" binding="tns:HelloPortSoap11"> <soap11:address location="http://localhost:8080/ws"/> </wsdl:port> </wsdl:service> </wsdl:definitions> ``` 6. 测试 WebService 现在我们可以使用任何支持 WebService 的客户端来测试我们在 Spring Boot 中发布WebService。打开 SoapUI 客户端,输入 WSDL 地址,点击导入即可自动创建服务代码,然后就可以测试我们的 WebService 服务了。 综上所述,借助 Spring Boot,我们可以构建 WebService 并快速发布它们。 ### 回答3: Spring Boot 是一种快速构建 Spring 应用程序的框架,可以帮助程序员更快地构建 web 服务。Spring Boot 同时支持 REST 和 SOAP 两种形式的 web 服务。本文将讨论如何使用 Spring Boot 发布 web 服务,特别是 SOAP 服务。 在 Spring Boot 中发布 SOAP 服务需要使用 Spring Web Services 模块。该模块提供了一个许多美元的框架去构建和开发SOAP Web服务。在创建 SOAP Web 服务的过程中,你将要使用以下几个 Spring 模块:Spring Boot starter web、Spring Boot starter test 和 Spring Boot starter web services。 以下是创建 SOAP 服务步骤: 1. 创建一个 Spring Boot 项目,添加 Web Services 的依赖,如下所示: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> 2. 创建一个 WSDL 文件。WSDL 文件描述了 SOAP 服务。你可以使用 WSDL 编辑器(例如 Eclipse)来创建该文件。 3. 创建一个 SOAP 服务端点类。 使用 @Endpoint 注释来声明该类是组件,它将处理所有的 SOAP 请求。 使用 @PayloadRoot 注释来绑定特定的承载体到一些处理方法。 3. 实现 SOAP 服务。 在步骤 3 创建的 SOAP 服务端点类中,定义一个方法用于接收请求并返回响应。 将方法的名称与操作名称相同。 将方法的返回类型与 output 元素对应的类型相同。 4. 创建 Web 服务的配置类。 启用 Spring-web 服务对 SOAP web 服务的支持。 使用 @EnableWs 注释开启 Spring Web Servicess 的功能。 通过 bean() 方法将在步骤 3 中创建的 SOAP 服务端点类添加到 Spring 上下文中。 5. 测试 SOAP 服务 在得到一个运行的 Web 服务之后,我们需要测试 SOAP 服务是否运行正常,我们可以使用 SoapUI 软件来测试。 总结: Spring官方自带的 Spring Web Services 模块是一个支持 SOAP 服务的框架,在使用 Spring Boot 构建项目的时候,尤其是构建其它微服务架构的项目,是一个非常好的选择。在日常构建项目中,对于服务接口尽可能的使用REST,对于某些非常适合使用SOAP时,我们完全可以使用这个框架。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

快乐的小三菊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值