在Java中创建HTTPS web服务
我用java创建了一个web服务,如下所示:在Java中创建HTTPS web服务,java,web-services,https,Java,Web Services,Https,我用java创建了一个web服务,如下所示: @Path("/rest") public class GetStatus { @GET @Produces("application/xml") @Path("/getMethod") public String getDetails() { return "Hello World !!!"; } <?xml version="1.0" encoding="UTF-
@Path("/rest")
public class GetStatus {
@GET
@Produces("application/xml")
@Path("/getMethod")
public String getDetails() {
return "Hello World !!!";
}
<?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.xml如下:
@Path("/rest")
public class GetStatus {
@GET
@Produces("application/xml")
@Path("/getMethod")
public String getDetails() {
return "Hello World !!!";
}
<?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 web服务,但我想先创建一个HTTPS服务
感谢您的帮助。服务器/servlet容器管理响应哪个协议。无论您在哪里发布它,web服务都将在某个web服务器中运行。在您的例子中,它将是一个servlet容器(可能是Tomcat),因为您使用的是servlet
您需要进行一些(相对)较小的配置,如果您在Tomcat 7上,您可以找到它。服务器/servlet容器管理响应哪个协议。谷歌搜索Tomcat https获取更多信息。您需要做一些(相对)小的配置。Tomcat是如何出现的?如果我想在线发布它呢?无论您在哪里发布它,web服务都将在某个web服务器中运行。在您的例子中,它将是一个servlet容器(可能是Tomcat),因为您使用的是servlet。好啊知道了。您可以合并以上两个注释作为答案。