---
title: "使用jdk6发布调用web service"
author: "thoughtfly"
date: "2012-06-26T14:22:43Z"
tags: []
categories: ["编程语言"]
slug: "jdk6-web-service"
draft: false
lastmod: "2012-06-26T14:22:43Z"
id: 1568080
url: "https://thoughtfly.iteye.com/blog/1568080"
---

### 发布web service

如果使用的jdk是1.6 update 17以上的

则直接使用　@WebService　标签和　Endpoint.publish静态方法即可

```
package test;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;

import t.TestService;

@WebService
public class test
{
    
    public String hello(){
        return "hello";
    }
    public static void main( String[] args )
    {
       Endpoint.publish( "http://localhost:8080/test/ts", new test() );
     }
}
```

 直接运行main方法就发布成功成功了

如果使用的jdk1.6是在update 17以下，则要对要发布的类执行apt操作

```
apt -d . test.java
```

 apt和javac的使用方式差不多。运行成功后会在指定路径下生成一个jaxws文件夹，并在里面生成相应的java类文件。将文件放置于项目路径下，再运行test的main方法即可成功

可以使用http://localhost:8080/test/ts?wsdl进行测试

 

### 调用service

运行service并测试发布成功

使用jdk直带的wsimport工具自动生成调用类

```
wsimport -keep -p test http://localhost:8080/test/ts?wsdl
```

 执行后在相应的包里生成几个类文件，对于我的示例来说TestService.java就是调用的客户端代码

```
TestService service = new TestService();
System.out.println(service.getTestPort().hello());
```

 即可成功调用webService