1

I am trying to follow this guide.

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

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?

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

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.

Thanks.

1
  • Have you tried using the Spring Tool Suite plugin for Eclipse? Commented Aug 2, 2016 at 15:06

1 Answer 1

2

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

1) Create and Run a Rest service

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

2) Create a Rest Client

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"/>
Sign up to request clarification or add additional context in comments.

1 Comment

The guide you cite is for building a spring app in the form of an executable jar. After you build and run this jar, the greeting service will be available from your browser. What errors do you get ? There is so many fails possible Some checks: do you have maven installed correctly, accessible from shell, with environment variables set ? do you have enough disk space for all dependencies to be downloaded ? does your eclipse project have the maven facet/aspect ? did you try mvn spring-boot:run ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.