webservice使用笔记(生成、调用等)


首先webservice是有服务端和调用端的。
我们可以搭建服务端,也可以只使用调用端。这样分开来比较好理解。

webservice服务端的搭建

springboot集成webservice

这块放到最后,因为一般都是对接别人的webservice,自己不太爱用这个。

添加依赖:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <version>1.5.2.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web-services</artifactId>
  <version>1.5.2.RELEASE</version>
</dependency>
<dependency>
  <groupId>wsdl4j</groupId>
  <artifactId>wsdl4j</artifactId>
  <version>1.6.3</version>
</dependency>

pom.xml如下:

  <dependencies>
    <!-- spring boot的包 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <version>1.5.2.RELEASE</version>
    </dependency>

    <!--spring web Service的包-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web-services</artifactId>
      <version>1.5.2.RELEASE</version>
    </dependency>

    <!--spring web service wsdl包-->
    <dependency>
      <groupId>wsdl4j</groupId>
      <artifactId>wsdl4j</artifactId>
      <version>1.6.3</version>
    </dependency>

  </dependencies>
</project>

resources下创建countries.xsd文件,内容如下:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.yourcompany.com/webservice"
           targetNamespace="http://www.yourcompany.com/webservice" elementFormDefault="qualified">

    <xs:element name="getCountryRequest">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name" type="xs:string"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="getCountryResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="country" type="tns:country"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:complexType name="country">
        <xs:sequence>
            <xs:element name="name" type="xs:string"/>
            <xs:element name="population" type="xs:int"/>
            <xs:element name="capital" type="xs:string"/>
            <xs:element name="currency" type="tns:currency"/>
        </xs:sequence>
    </xs:complexType>

    <xs:simpleType name="currency">
        <xs:restriction base="xs:string">
            <xs:enumeration value="GBP"/>
            <xs:enumeration value="EUR"/>
            <xs:enumeration value="PLN"/>
        </xs:restriction>
    </xs:simpleType>
</xs:schema>

根据xsd生成java代码:
idea中—tools—jaxb— generate xml schema from java using jaxb

configuration代码:

@EnableWs
@Configuration
public class WebServiceConfig extends WsConfigurerAdapter {
    @Bean
    public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(applicationContext);
        servlet.setTransformWsdlLocations(true);
        return new ServletRegistrationBean(servlet, "/ws/*");
    }

    @Bean(name = "countries")
    public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema countriesSchema) {
        DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
        wsdl11Definition.setPortTypeName("CountriesPort");
        wsdl11Definition.setLocationUri("/ws");
        wsdl11Definition.setTargetNamespace("http://www.yourcompany.com/webservice");
        wsdl11Definition.setSchema(countriesSchema);
        return wsdl11Definition;
    }

    @Bean
    public XsdSchema countriesSchema() {
        return new SimpleXsdSchema(new ClassPathResource("countries.xsd"));
    }
}

CountryEndpoint代码:

@Endpoint
public class CountryEndpoint {
    private static final String NAMESPACE_URI = "http://www.yourcompany.com/webservice";
    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "getCountryRequest")
    @ResponsePayload
    public GetCountryResponse getCountry(@RequestPayload GetCountryRequest request) {
        GetCountryResponse response = new GetCountryResponse();

        Country country = new Country();
        country.setName("china");
        country.setCapital("beijing");
        country.setPopulation(160000000);
        country.setCurrency(Currency.GBP);

        return response;
    }
}

启动后,输入url :

http://localhost:8080/ws/countries.wsdl

如图即为成功:在这里插入图片描述

调用方的搭建

一般是调用端才需要代码生成。

代码生成

生成方式有几种。

soapui生成

略,写在soapui的笔记里面了。

doc命令行生成

wsimport 命令一般都自带。

生成的代码结构

ObjectFactoryjava
package-info.java
QueryWeather.java
QueryWeatherResponse.java
WeatherServicelmpl.java
WeatherServicelmplService.java

调用端发送请求例子

自动生产会有一系列类,一般来说service类就是我们需要用到的发送类。
代码:

//设置账号密码
Authenticator.setDefault(new java.net.Authenticator(){
    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(username,password.toCharArray());
    }
});
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
URL url = new URL(soapUrl);
SOSJS2UserService sosjs2UserService = new SOSJS2UserService(url);
SOSJS2User wsi = sosjs2UserService.getHTTPPort();
DTJS2UserREQ params = new DTJS2UserREQ();
params.setMESSAGE(message);

log.info("user查询方法_rest请求webservice开始,请求报文:{}", JSON.toJSONString(params));
DTJS2UserRES soapResult = wsi.sosJS2User(params);

log.info("user查询方法_rest请求webservice完成,返回报文:{}", JSON.toJSONString(soapResult));

wsdl样例(常用的地址)

以下地址仅作为wsdl的展示例子,不确定可以生成成功。

IP地址来源搜索 WEB 服务(是目前最完整的IP地址数据)
Endpoint :http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx
Disco         :http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?disco
WSDL       :http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl

随机英文、数字和中文简体字 WEB 服务
Endpoint :http://www.webxml.com.cn/WebServices/RandomFontsWebService.asmx
Disco        :http://www.webxml.com.cn/WebServices/RandomFontsWebService.asmx?disco
WSDL       :http://www.webxml.com.cn/WebServices/RandomFontsWebService.asmx?wsdl

中国邮政编码 <-> 地址信息双向查询/搜索 WEB 服务
Endpoint :http://www.webxml.com.cn/WebServices/ChinaZipSearchWebService.asmx
Disco        :http://www.webxml.com.cn/WebServices/ChinaZipSearchWebService.asmx?disco
WSDL      :http://www.webxml.com.cn/WebServices/ChinaZipSearchWebService.asmx?wsdl

webservice服务代码的git地址

https://github.com/1054294965/git-longhu-webservice.git

restful和webservice的区别

****restfulwebservice
量级restful是一种轻量级架构webservic是重量级架构,面向的是soap协议所以可以跨公司使用。
跨语言、跨公司、跨平台。。。webservice是跨语言、跨公司、跨平台的,扯这么远说白了就是樽还xml及xsd协议,只要双方都遵循这个协议,不论什么语言、什么公司、什么平台都可以。
安全性也可以实现安全性自带安全属性,包块wsdl的访问,及调用都是自带密码机制的,可以暴露给想要暴露的客户。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值