在Eclipse中运行Java REST API [英] Running a Java REST API in Eclipse

查看:229
本文介绍了在Eclipse中运行Java REST API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试遵循本指南

我已经下载了gs-rest-service项目并将其导入我的工作区。

I have downloaded the gs-rest-service project and imported it into my workspace.

现在我只想运行项目 - 无论我尝试似乎都失败了。我是否认为我需要将这个项目部署到服务器上,以便我可以在后台永久运行它,并让它回复我的REST调用?我如何做到这一点?

Now I would simply like to run the project - whatever I try seems to fail though. Am I right in thinking I would need to deploy this project to a server so I could run it permanently in the background and have it reply to my REST calls? How can I do this?

我已经尝试以几乎所有方式运行该项目,作为Java应用程序/ Java Applet / Maven构建。所有似乎都失败了。

I have tried running the project in nearly all ways, as a Java Application/Java Applet/Maven Build. All seem to fail.

我是Spring和MVN的新手,所以我意识到我无疑在做某件事情真的很愚蠢 - 如果有人能告诉我这是什么将是最感激的。

I am new to Spring and MVN so I realise I am undoubtedly doing something really, really stupid here - if anyone could tell me what that is I would be most appreciative.

谢谢。

推荐答案

实际上需要一个Spring应用来运行您的Web服务。您可以尝试以下步骤

You do not actually need a Spring application to run your web service. You can try the below procedure

1)创建并运行休息服务

1) Create and Run a Rest service

创建动态Web项目。在您选择的应用程序服务器中托管它。
按照以下链接:
使用Rest的第一个应用

Create a dynamic web project. host it in your application server of your choice. follow the link below: first application using Rest

2)创建一个休息客户端

2) Create a Rest Client

创建一个新的Java项目 - >创建一个新的Java类来休息调用

Create a new java project -> Create a new Java class to make the rest call

import java.util.List;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.MediaType;

public class WebServiceTester  {

   private Client client;
   private String REST_SERVICE_URL = "http://localhost:8080/UserManagement/rest/UserService/users";
   private static final String PASS = "pass";
   private static final String FAIL = "fail";

   private void init(){
      this.client = ClientBuilder.newClient();
   }

   public static void main(String[] args){
      WebServiceTester tester = new WebServiceTester();
      //initialize the tester
      tester.init();
      //test get all users Web Service Method
      tester.testGetUsers();

   }
   //Test: Get list of all users
   //Test: Check if list is not empty
   private void testGetUsers(){
      GenericType<List<User>> list = new GenericType<List<User>>() {};
      List<User> users = client
         .target(REST_SERVICE_URL)
         .request(MediaType.APPLICATION_XML)
         .get(list);
      String result = PASS;
      if(users.isEmpty()){
         result = FAIL;
      }
      else{
          for(User each: users){
          System.out.println("user id :"+each.getId());
          System.out.println("user name :"+each.getName());
          System.out.println("user id :"+each.getProfession());
          }

      }
      System.out.println("Test case name: testGetUsers, Result: " + result );


   }

}

运行上述客户端程序所需的库

libraries required to run the above client program

<classpathentry kind="lib" path="lib/javax.ws.rs-api-2.0.1.jar"/>
<classpathentry kind="lib" path="lib/jersey-client.jar"/>
<classpathentry kind="lib" path="lib/jersey-common.jar"/>
<classpathentry kind="lib" path="lib/jersey-media-jaxb.jar"/>
<classpathentry kind="lib" path="lib/hk2-api-2.4.0-b34.jar"/>
<classpathentry kind="lib" path="lib/hk2-locator-2.4.0-b34.jar"/>
<classpathentry kind="lib" path="lib/hk2-utils-2.4.0-b34.jar"/>
<classpathentry kind="lib" path="lib/jersey-guava-2.22.2.jar"/>
<classpathentry kind="lib" path="lib/javax.inject-2.4.0-b34.jar"/>
<classpathentry kind="lib" path="lib/javax.annotation-api-1.2.jar"/>

这篇关于在Eclipse中运行Java REST API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆