如何在没有像tomcat / Jboss这样的容器的情况下在核心java中创建Restful Web服务

时间:2016-01-27 06:08:46

标签: java web-services rest tomcat jersey

我有核心java项目( swing模块),但最近要求在没有任何容器的核心java上部署一个restful web服务。

那么是否可以在没有任何容器的情况下部署restful Web服务?

我搜索了很多网站,我的代码如下:

public class JerseyEmbeddedHTTPServerCrunchify {

public static void main(String[] args) throws IOException {
    System.out
            .println("Starting Crunchify's Embedded Jersey HTTPServer...\n");
    HttpServer crunchifyHTTPServer = createHttpServer();
    crunchifyHTTPServer.start();
    System.out.println(String.format(
            "\nJersey Application Server started with WADL available at "
                    + "%sapplication.wadl\n", getCrunchifyURI()));
    System.out
            .println("Started Crunchify's Embedded Jersey HTTPServer Successfully !!!");
}

private static HttpServer createHttpServer() throws IOException {
    // ResourceConfig crunchifyResourceConfig = new
    // PackagesResourceConfig("com.crunchify.tutorial");
    ResourceConfig crunchifyResourceConfig = new ResourceConfig();
    // This tutorial required and then enable below line:
    // http://crunfy.me/1DZIui5
    // crunchifyResourceConfig.getContainerResponseFilters().add(CrunchifyCORSFilter.class);
    // return HttpServerFactory.create(getCrunchifyURI(),
    // crunchifyResourceConfig);
    System.out.println("URI : " + getCrunchifyURI());
    return JdkHttpServerFactory.createHttpServer(getCrunchifyURI(),
            crunchifyResourceConfig);
}

private static URI getCrunchifyURI() {
    // return UriBuilder.fromUri("http://" + crunchifyGetHostName() +
    // "/").port(18085).build();
    return UriBuilder.fromUri("http://" + "localhost" + "/").port(18085)
            .build();
}

private static String crunchifyGetHostName() {
    String hostName = "localhost";
    try {
        hostName = InetAddress.getLocalHost().getCanonicalHostName();
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
    return hostName;
}    

Maven依赖:

<dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-jdk-http</artifactId>
        <version>2.4.1</version>
    </dependency>

在上面的代码中我得到了以下异常:

Exception in thread "main" java.lang.IllegalStateException: server in wrong state
at sun.net.httpserver.ServerImpl.start(ServerImpl.java:139)
at sun.net.httpserver.HttpServerImpl.start(HttpServerImpl.java:58)
at org.glassfish.jersey.jdkhttp.JdkHttpServerFactory$1.start(JdkHttpServerFactory.java:143)

1 个答案:

答案 0 :(得分:0)

您可以尝试

@RestController
public class WebServiceController 
{
    @RequestMapping(value="/getresultbyNumber/{number1}/{number2}", method={RequestMethod.POST,RequestMethod.GET})
    public @ResponseBody Object getData(@PathVariable String number1, @PathVariable String number2 , HttpServletRequest request)
    {
        String URL="http://192.168.4.218:8081/DemoSpringMVC/getResultbyNumberdata/"+ number1 +"/"+number2;
        HttpClient client= HttpClientBuilder.create().build();

    HttpGet httpRequest=new HttpGet(URL);
    String res =null;

    try {
        HttpResponse response=client.execute(httpRequest);
        res = new BasicResponseHandler().handleResponse(response);
        System.out.println("result===>"+res);
    } catch (Exception e) {
        e.printStackTrace();

    }
    return res ;
}
}