Java6 开发的Web Service, 发布https的wsdl地址(一)

        目前有许多开发web service的方法,其中发布https通道wsdl的也有许多。但是对于小的应用程序来说,不需要采用应用服务器来进行发布和运行,只需要一个小小的jar包就可以满足需求了,这种方式更加地便捷。

要求: 安装jdk1.6.0_26及以上。(本人只在jdk1.6.0_26和jdk1.6.0_29上测试通过,其他版本没有测试过,所以,26以下的版本也有可能可以测试通过的,这个数字不完全精确。)

 

下面通过一个简单的例子来进行展示:

 

新建一个Java Project(HelloWorld)。

 

由于SSL需要密钥文件,所以要先生成一个.keystore文件。

生成方法:(1)在cmd中输入命令:keytool -genkey -alias tomcat-server -keyalg RSA -keypass changeit -storepass changeit -validity 3600    

                    (2)按照提示一次输入相应的内容,最后一步输入“y”即可。

                    (3)在运行命令的目录下就生成了.keystore文件。密码为:changeit。修改文件名,例如,hello.keystore,否则程序不识别。

将hello.keystore文件拷贝到刚刚建好的Java Project根目录下。

                 

在刚刚建好的Project 下面 新建一个class文件(HelloWorld.java)。

HelloWorld.java的代码如下:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;

import javax.jws.WebService;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.xml.ws.Endpoint;
import com.sun.net.httpserver.HttpContext;
import com.sun.net.httpserver.HttpsConfigurator;
import com.sun.net.httpserver.HttpsServer;

@WebService(targetNamespace="http://hello/")
public class HelloWorld {
 
 public String sayHi(){
  return "Hello World!";
 }
 
 public static void main(String[] args) throws CertificateException, FileNotFoundException, IOException, UnrecoverableKeyException {
  try {
   SSLContext ssl = SSLContext.getInstance("TLS");
   KeyManagerFactory keyFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());

   KeyStore store = KeyStore.getInstance("JKS");
   String keystoreFile="hello.keystore";
   String keyPass="changeit";
   store.load(new FileInputStream(keystoreFile),keyPass.toCharArray());
   
   keyFactory.init(store, keyPass.toCharArray());
   TrustManagerFactory trustFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
   trustFactory.init(store);
   KeyManager[] keyManagers=new KeyManager[1];
   keyManagers=keyFactory.getKeyManagers();
   TrustManager[] trustManagers=trustFactory.getTrustManagers();
   ssl.init(keyManagers, trustManagers, new SecureRandom());
   HttpsConfigurator configurator = new HttpsConfigurator(ssl);
   HttpsServer httpsServer = HttpsServer.create(new InetSocketAddress("localhost", 8440),8440);
   httpsServer.setHttpsConfigurator(configurator); 
   HttpContext httpContext = httpsServer.createContext("/Hello"); 
   httpsServer.start(); 
    
   Endpoint endpoint = Endpoint.create(new HelloWorld());
   endpoint.publish(httpContext);  
   
  } catch (NoSuchAlgorithmException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  } catch (KeyStoreException e) {
  // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (KeyManagementException e) {
  // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
 }

}

万事具备,只欠东风。现在只需要运行HelloWorld.java就ok了。

单击HelloWorld.java右键,点击Run as--> Java Application,在console里面就可以看到如下提示:

2011-11-2 10:47:12 com.sun.xml.internal.ws.model.RuntimeModeler getRequestWrapperClass
信息: Dynamically creating request wrapper Class jaxws.SayHi
2011-11-2 10:47:13 com.sun.xml.internal.ws.model.RuntimeModeler getResponseWrapperClass
信息: Dynamically creating response wrapper bean Class jaxws.SayHiResponse

 

说明一切进展顺利。

此时,只需要在IE浏览器里输入wsdl地址:https://localhost:8440/Hello?wsdl 。浏览器会报一个“此网站的证书有问题”的错误提示信息,Don't worry,勇敢地点击“继续浏览此网站(不推荐)”,wsdl就显示出来了。

 

wsdl内容如下:

 
<?xml version="1.0" encoding="UTF-8"?>
<!--

 Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. 

  -->
<!--

 Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. 

  -->
-<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://hello/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://hello/" name="HelloWorldService">
-<types>
-<xsd:schema>
  <xsd:import namespace="http://hello/" schemaLocation="https://localhost:8440/Hello?xsd=1" />
  </xsd:schema>
  </types>
-<message name="sayHi">
  <part name="parameters" element="tns:sayHi" />
  </message>
-<message name="sayHiResponse">
  <part name="parameters" element="tns:sayHiResponse" />
  </message>
-<portType name="HelloWorld">
-<operation name="sayHi">
  <input message="tns:sayHi" />
  <output message="tns:sayHiResponse" />
  </operation>
  </portType>
-<binding name="HelloWorldPortBinding" type="tns:HelloWorld">
  <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
-<operation name="sayHi">
  <soap:operation soapAction="" />
-<input>
  <soap:body use="literal" />
  </input>
-<output>
  <soap:body use="literal" />
  </output>
  </operation>
  </binding>
-<service name="HelloWorldService">
-<port name="HelloWorldPort" binding="tns:HelloWorldPortBinding">
  <soap:address location="https://localhost:8440/Hello" />
  </port>
  </service>
  </definitions>


 

 

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值