学习随笔-Java WebService

webService 可以将应用程序转换成网络应用程序。是简单的可共同操作的消息收发框架。

基本的webService平台是 XML 和 HTTP。

      HTTP 是最常用的互联网协议;

      XML 是 webService 的基础,因为可以用于不同平台和编程语言之间。

webService平台的元素:

       SOAP(简易对象访问协议);

       UDDI(通用描述、发现、整合);

       WSDL(web service 描述语言);

webService 有两种类型的应用

      1)可重复使用的应用程序组件:有些功能是不同应用程序常常用到的,比如:天气预报。webService可以把应用程序组件当做服务来提供;

      2)连接现有软件:通过为不同程序提供一种链接其数据的途径,解决协同办公的问题。

简单程序:

创建一个service 的 web service project工程 :

package com.service;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService   //将类标注为webService
public class Test {
    
    @WebMethod  //标注客户端调用的方法
    public String getString (String name){
        return name.toUpperCase();
    }
    public static void main(String[] args) {

   //发布到服务端 参数1:网络协议+ip+端口号(不能被占用)+上下文根+调用方法  参数2:new一个本类的实例
        Endpoint.publish("http://localhost:8085/Test", new Test());

   //标志着编译成功结束 可不写
        System.out.println("OK");
    }
}

//配置文件我现在还没有搞明白  如果不配配置测试地址就无响应

配置一个xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 注册组件扫描器 -->
    <context:component-scan base-package="com.service" />
</beans>

测试地址:http://localhost:8085/Test?wsdl

利用 webService soapUI 4.5.2软件 链接http://localhost:8085/service/Test 自动生成应用服务

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <ser:getString>
         <!--Optional:-->
         <arg0>yyyy</arg0>  //<arg0>?</arg0> 原始显示是? 为需求参数
      </ser:getString>
   </soapenv:Body>
</soapenv:Envelope>

 

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:getStringResponse xmlns:ns2="http://service.com/">
         <return>YYYY</return>  //<return>?</return> 原始显示是? 为处理结果
      </ns2:getStringResponse>
   </S:Body>
</S:Envelope>


 

2、命令提示窗口执行生成命令。

先创建一个client 的web service project 工程

打开 命令窗口 输入

格式:wsimport -s "src目录" -p “生成类所在包名” -keep “wsdl发布地址”

    1)"src目录"地址不可含空格

    2)“wsdl发布地址”不要漏了“?wsdl”

效果:

  wsimport -s E:\WorkSpace\maven\client\src  -p com.client -keep http://localhost:8085/Test?wsdl
  正在解析 WSDL...
  正在生成代码...
  正在编译代码...

刷新项目就可以看到生成的client代码

但是client 客户端代码会有乱码问题目前还没有解决

转载于:https://www.cnblogs.com/myblog-2016/p/6055430.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值