首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Java创建HTTPS web服务

问用Java创建HTTPS web服务
EN

Stack Overflow用户
提问于 2013-06-26 14:08:32
回答 1查看 5.4K关注 0票数 1

我在java中创建了一个web服务,如下所示:

代码语言:javascript
复制
@Path("/rest")
public class GetStatus {
    @GET
    @Produces("application/xml")
    @Path("/getMethod")
    public String getDetails() {
                  return "Hello World !!!";
    }

相应的web.xml如下:

代码语言:javascript
复制
<?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>

我把它消费如下:

代码语言:javascript
复制
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 web服务,但我想首先创建一个HTTPS服务。

任何帮助都会受到同样的赞赏。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-26 14:40:01

服务器/servlet容器管理要响应的协议。无论您在哪里发布它,web服务都将在某个web服务器中运行。在您的示例中,它将是一个servlet容器(可能是Tomcat),因为您使用的是servlet。

您需要做一些(相对的)小配置,如果您在Tomcat 7上,您可以找到它这里。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17322443

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档