java - 如何在没有任何容器(如 tomcat/Jboss)的情况下在核心 java 中创建 Restful Web 服务

标签 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)

最佳答案

你可以试试这个

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

关于java - 如何在没有任何容器(如 tomcat/Jboss)的情况下在核心 java 中创建 Restful Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35029985/

相关文章:

java - 验证不可变值对象的良好做法

java - 菜单系统可以工作但无法关闭

java - 如何根据来电者过滤service now事件记录?

python - 除运行 py 文件外,无法在 python-eve 中包含模型

node.js - Paypal :使用 REST API/Nodejs SDK 的交易历史

java - 如何对 vector 的 vector 进行排序?

java - Jcreator运行文件时出错

java - 无法使用 jdk8 和 netbeans 8 打开 Web 服务测试器页面

java - 在 Web 服务/客户端之间传递 JAXB 对象

api - paypal rest api 测试信用卡号码