This is my first time dealing with web-services. Simply, I need to send a post request from jersey web service client (inside a webpage implemented in javascript) to a jersey service which is in one of my maven modules.
As I said I've created jersey-server within one of my maven modules and I would like to run it somehow (I do not know how to run a web service program.) before starting client side of my implementation. Through searching on the web, I saw lots of examples but all of them was using tomcat. So my first question is that do I need to use tomcat (or something like this ) in order to run a web service ? Secondly, below I shared my jersey-server module. How could I start to run it ?
package com.exampleProject.rest;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;
@Path("/test")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class SiderRecommender {
@POST
@Path("/functiontest")
public List<Recommendation> sampleFunction() {
// return something here. I removed it for simplicity.
}
}
Since your using Maven, hence you need to configure the properties to run on the Tomcat.
Right click on the Project -> Run as-> Run Configuration Select the Maven Tomcat-> change the goals to 'tomcat:run'
I can't comment the first answer. So I'm adding a comment here. If you choose to generate the project with the archetype, the command line should be
And not the command line in the link. This will work with the pom provided by peeskillet
You don't have to run a Jersey app in an installed web server. You can run it in an embedded server, meaning a server that runs in standalone mode with a normal
main
method.If you are using Maven, and you are familiar with creating Maven archetypes, you can use the
jersey-quickstart-grizzly2
archetypejersey-quickstart-grizzly2
)jersey-quickstart-grizzly2
).This is everything you get for free with the archetype project.
Main.java
MyResource.java
MyResourceTest.java
pom.xml
- I added thejersey-media-json-jackson
and themaven-assembly-plugin
myself, so that you can create a single runnable jar file.With all the above, you can
cd
to the project from the command line and domvn clean package
java -jar target/jersey-grizzly-jar-with-dependencies.jar
and the application will start.
You can access it from
http://localhost:8080/myapp/myresource
That's it. Note that the above is a normal jar project. So if you can't follow how to create the archetype, you can pretty much copy everything above into a jar project.
See Also: