如何在核心java中创建Restful web服务,而不使用像tomcat/Jboss这样的容器

如何在核心java中创建Restful web服务,而不使用像tomcat/Jboss这样的容器,java,web-services,rest,tomcat,jersey,Java,Web Services,Rest,Tomcat,Jersey,我有一个核心java项目(swing module),但最近要求在核心java上部署一个restful web服务,而不使用任何容器 那么,在没有任何容器的情况下部署restful web服务是可能的吗 我已经搜索了很多网站,我得到了如下代码: public class JerseyEmbeddedHTTPServerCrunchify { public static void main(String[] args) throws IOException { System.out

我有一个核心java项目(swing module),但最近要求在核心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>
你可以试试这个

@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 ;
}
}

Jetty也是一个web站点container@JunedAhsan是否可以在核心java中部署restful web服务?如果您自己实现HTTP协议,则可以,但请不要这样做,并使用可用的web容器。如果您不想重新实现连接处理,则可能需要使用某些web服务器。看一看,这是一个非常小且易于创建web服务的库,使用jetty但完全透明。看起来像一个。。。
@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 ;
}
}