[英]Creating HTTPS web service in Java
我在java中创建了一个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服务。
任何帮助同样的赞赏。
服务器/ servlet容器管理要响应的协议。 无论您何时发布它,Web服务都将在某个Web服务器中运行。 在你的情况下,它将是一个servlet容器(可能是Tomcat),因为你正在使用servlet。
你需要做一些(相对)小的配置,如果你在Tomcat 7上,你可以在这里找到它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.