Webservice入门案例

1.新建项目
2.服务端编写
①接口interface

import javax.jws.WebService;
import javax.jws.WebMethod;
@WebService
public interface WeatherInterface {
    
    @WebMethod
    public String queryWeather(String cityName);
    
}

②实现类

import javax.jws.WebService;
@WebService(targetNamespace="WeatherInterface") //接口所在地址
public class WeatherInterfaceImpl implements
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个使用Spring Boot搭建WebService的简单入门案例: 1.创建Spring Boot项目,在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> ``` 2.创建一个WebService类,在其中定义需要暴露的方法: ```java @WebService public interface HelloWorldService { @WebMethod String sayHello(String name); } ``` ```java @WebService(endpointInterface = "com.example.demo.webservice.HelloWorldService") public class HelloWorldServiceImpl implements HelloWorldService { @Override public String sayHello(String name) { return "Hello " + name + "!"; } } ``` 3.配置WebService的端点和实现类: ```java @Configuration public class WebServiceConfig { @Bean public ServletRegistrationBean<MessageDispatcherServlet> messageDispatcherServlet() { MessageDispatcherServlet servlet = new MessageDispatcherServlet(); servlet.setApplicationContext(new AnnotationConfigApplicationContext(WebServiceConfig.class)); servlet.setTransformWsdlLocations(true); return new ServletRegistrationBean<>(servlet, "/ws/*"); } @Bean(name = "helloWorld") public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema xsdSchema) { DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition(); wsdl11Definition.setPortTypeName("HelloWorldPort"); wsdl11Definition.setLocationUri("/ws"); wsdl11Definition.setTargetNamespace("http://www.example.com/demo/webservice"); wsdl11Definition.setSchema(xsdSchema); return wsdl11Definition; } @Bean public XsdSchema xsdSchema() { return new SimpleXsdSchema(new ClassPathResource("hello.xsd")); } } ``` 4.创建XSD文件定义WebService的参数和返回值: ```xml <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.com/demo/webservice" xmlns:tns="http://www.example.com/demo/webservice" elementFormDefault="qualified"> <xs:element name="sayHelloRequest"> <xs:complexType> <xs:sequence> <xs:element name="name" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="sayHelloResponse"> <xs:complexType> <xs:sequence> <xs:element name="result" type="xs:string"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema> ``` 5.启动应用程序,访问http://localhost:8080/ws/helloWorld.wsdl,将会看到生成的WSDL文件。 6.使用SOAPUI等工具测试webservice。 以上就是一个简单的Spring Boot Webservice入门案例

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值