2013-06-26 163 views
0

我已經用Java創建Web服務如下:創建HTTPS Web服務

@Path("/rest") 
public class GetStatus { 
    @GET 
    @Produces("application/xml") 
    @Path("/getMethod") 
    public String getDetails() { 
        return "Hello World !!!"; 
    } 

通訊web.xml中如下:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    id="WebApp_ID" version="2.5"> 
    <display-name>MyWebService</display-name> 
    <servlet> 
     <servlet-name>ServletAdaptor</servlet-name> 
     <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> 
     <init-param> 
      <param-name>com.sun.jersey.config.property.packages</param-name> 
      <param-value>com.xmlws</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>ServletAdaptor</servlet-name> 
     <url-pattern>/check/*</url-pattern> 
    </servlet-mapping> 
</web-app> 

而且我消費它如下:

String wsdl = "http://localhost:8080/MyWebService/check/rest/getMethod/"; 
HttpClient httpclient = new DefaultHttpClient(); 
HttpResponse response; 
HttpGet getMethod = new HttpGet(wsdl); 
String responseString = null; 
try { 
      response = httpclient.execute(getMethod); 

      StatusLine statusLine = response.getStatusLine(); 
      System.out.println("getStatusCode = " + statusLine.getStatusCode()); 
      if (statusLine.getStatusCode() == HttpStatus.SC_OK) { 
       ByteArrayOutputStream out = new ByteArrayOutputStream(); 
       response.getEntity().writeTo(out); 
       out.close(); 
       responseString = out.toString(); 
      } else { 
       response.getEntity().getContent().close(); 
       throw new IOException(statusLine.getReasonPhrase()); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (Exception e) { 
      e..printStackTrace(); 
     } 

但是目前的web服務是HTTP。我如何製作HTTPS?有什麼要求這樣做?

我知道如何從客戶端訪問HTTPS Web服務,但我想先創建一個HTTPS服務。

任何幫助讚賞相同。

+0

服務器/ Servlet容器管理的協議,以應對。 Google for Tomcat https獲取更多信息。有一些(相對)小的配置你需要做。 –

+0

Tomcat如何進入圖片?如果我想在線發佈,該怎麼辦? – GAMA

+0

無論你在哪裏發佈它,Web服務都將在某個Web服務器中運行。在你的情況下,因爲你使用的是servlet,所以它將成爲一個servlet容器(可能是Tomcat)。 [檢查它](http://tomcat.apache.org/tomcat-7.0-doc/ssl-howto.html) –

回答

2

服務器/ servlet容器管理要響應的協議。無論你在哪裏發佈它,Web服務都將在某個Web服務器中運行。在你的情況下,因爲你使用的是servlet,所以它將成爲一個servlet容器(可能是Tomcat)。

有一些(相對)小的配置,你需要做的,你可以找到它here如果你在Tomcat 7