Create REST Services using Java in Eclipse

12,053

Rather than copying jars, it would be better to use maven or gradle for package management. A simple pom.xml (maven) with the dependencies can help you abstract determining the compile and runtime dependencies.

Share:
12,053
Deslyxia
Author by

Deslyxia

Updated on June 04, 2022

Comments

  • Deslyxia
    Deslyxia almost 2 years

    I have been looking for a few days at tutorials on this subject but either they aren't exactly what i'm looking for or I cant get them to work. I cant imagine that more people aren't confused on the subject so I will ask here.

    What I would like to create is a REST service in Eclipse that I can run on my web server and "connect to" using ajax from a separate dynamic web project. All i'm looking for here at the moment is a simple hello world example of a service returning ajax working alongside a separate web project that consumes the JSON it returns.

    Im hoping to get a usable user guide (or at least links to one) that will help me out and future people looking for this same thing.

    I have gotten as far as this simple class (i have included Jersey Jars but I dont understand what to do from here):

    public class UserRestService {
    private static final Logger log = Logger.getLogger(UserRestService.class.getName());
    
    private CreateUserService createUser;
    
    @POST
    @Path("/CreateUser/{name}/{age}")
    @Consumes("text/html")
    public User createUser(@PathParam("name") String name, @PathParam("age") Integer age)            {
    
        return createUser.createUser(name, age);    
    }
    }
    

    How do i get this class to be an accessible api service on my tomcat server? How do I setup another web project to consume it (I understand how to make an ajax call this is more a question of how do i setup the projects)? Where do servlets come in ?

    • OldProgrammer
      OldProgrammer over 10 years
    • Deslyxia
      Deslyxia over 10 years
      I see those but what confuses me is that when i download the latest stuff from jersey that it doesnt matchup with what they have in the tutorial or they are doing stuff in eclipse that is on an older version and different now. Basically when i go through many of them i am left wondering if it doesnt work because i royally screwed up or if it needs to be done differently becaust eclipse changed or the jars changed etc...
    • Aaron
      Aaron over 10 years
      I have this problem with lots of frameworks. Sometimes it's worth a little detective work to see what version the tutorials use, and follow suit.
  • Deslyxia
    Deslyxia over 10 years
    so as im going through the tutorial I get confused about which jars to copy into the WEB-INF/lib folder. When i unpack the zip file from jersey.java.net i wind up with 3 folders (api,ext, and lib) what exactly do i copy into my WEB-INF/lib folder?
  • Kevin Bayes
    Kevin Bayes over 10 years
    I would convert this to a maven or gradle project to handle your dependencies for you. There is a parent pom file with all the dependencies on github github.com/jersey/jersey/blob/master/examples/…