问题
I created a basic REST service that works when it is invoked over a http connection. But when I try to add SSL on top I have some problems.
This is the code for the server creation:
private HttpContext getHttpContext() {
HttpsServer server = null;
try {
server = HttpsServer.create(new InetSocketAddress("localhost", 443), 5);
server.setHttpsConfigurator(new HttpsConfigurator(SecureChatSslContextFactory.getServerContext()) {
public void configure(HttpsParameters params) {
SSLContext context = getSSLContext();
// get the default parameters
SSLParameters sslparams = context.getDefaultSSLParameters();
params.setSSLParameters(sslparams);
}
});
server.start();
return server.createContext("/customerservice/customer");
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
Endpoint e = Endpoint.create(HTTPBinding.HTTP_BINDING, new RestSourcePayloadProvider());
e.publish(getHttpContext());
The class RestSourcePayloadProvide
r has the public method invoke, but it never gets called. I suppose that the reason for this behaviour is that the binding for the endpoint is HTTPBinding.HTTP_BINDING
, and not HTTPS. But I didn't manage to find a way to bind to https.
If I run the test client or the browser on link text I get the same answer:
500 Internal Server Error No handler for context
回答1:
I found the answer to my problem. I was not creating a handler for the incoming requests. I was supposed to use
return server.createContext("/customerservice/customer", new HttpHandler(...));
instead of
return server.createContext("/customerservice/customer");
It looks like the interface Endpoint
is implemented by the EndpointImpl
class in Apache's CFX. When calling publish(address)
, the method called in EndpointImpl
creates a ServerImpl
that will be the service server. If I call publish(server context)
, nothing happens because EndpointImpl
doesn't override that method also.
回答2:
For those who want to use the CFX's EndpointImpl and SSL try this: link text
It uses the Jetty Server package to create the working server and publish it to the endpoint.
来源:https://stackoverflow.com/questions/4735116/bind-j2se-endpoint-to-https