GAMA
GAMA

Reputation: 5996

Creating HTTPS web service in Java

I have created a web service in java as follows:

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

Corresponding web.xml is as follows:

<?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>

And I'm consuming it as follows:

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();
        }

But currently web service is HTTP. How do I make it HTTPS? What are requirements to do so?

I know how to access HTTPS web service from client but I want to create a HTTPS service first.

Any help appreciated on the same.

Upvotes: 1

Views: 5541

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280171

The server/servlet container manages which protocol to respond to. Wherever you're publishing it, the web service will be running inside some web server. In your case, it'll be a servlet container (possibly Tomcat) since you're using servlets.

There's some (relatively) small configuration you need to do and you can find it here if you're on Tomcat 7.

Upvotes: 3

Related Questions