Setting up Jax-rs 2.2 with Jetty - java

I'm trying to set up some REST services on Jetty using Jersey JAXRS. I can't get json data through to my REST service class though. My ajax requests keep getting the "Unsupported Media Type" error and status. I get this regardless of what #Produces and #Consumes annotations I add to my methods though they should both be MediaType.APPLICATION_JSON.
I can't find decent documentation on Jersey and the loads of questions, blogs, and other resources all seem to be out of date. Looks like Jersey has undergone a lot of changes recently and I'm at a loss as to where I should be looking. I set up the following based on the jersey webapp archetype:
web.xml:
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.my.package.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
Code snippet:
#Path("/users")
public class UserService {
// Plain text works!
#GET
#Consumes(MediaType.TEXT_PLAIN)
public String list(){
return "Got it!";
}
// JSON doesn't work! >:(
#POST
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public User create(User user) {
Mocks.USERS.add(user);
return user;
}
My parent pom manages these dependencies ahd the second of these two is a dependency in my jax-rs project pom.
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>2.2</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.2</version>
</dependency>
Do I need something to add support for JSON?

Have you read a chapter dedicated to JSON in the Users Guide? The easiest way would be adding a dependency on MOXy and JSON support would work out-of-the-box (you don't need to explicitly register features the modules provides to make it work as opposed to other JSON modules in Jersey):
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.2</version>
</dependency>
Anyways Jersey provides more modules that would help you with handling JSON media type:
MOXy (examples: json-moxy, bean-validation-webapp)
Jackson (example: json-jackson)
Java API for JSON Processing (JSON-P) (example: json-processing-webapp)
Jettison (example: json-jettison)

Seems Drew was on the right track in his comment. But the answer (for Jersey 2.2 + Jackson at least) was a more up-to-date provider
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.2.3</version>
</dependency>
Using this required no configuration. Use this with the two dependencies in the original questions and you're in business.

JSON start working for me just with 2 dependencies:
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.2.3</version>
</dependency>

Related

HTTP method GET is not supported by this URL Rest-full WS on WL 12.2.1

I have developed a Rest-full WS which runs successfully on tomcat.
The application deploys succesfully in Weblogic 12.2.1 without any exceptions. However, on hitting the URI, I face "HTTP method GET is not supported by this URL Rest-full WS" in response.
This happens for POST methods as well and while generating wadl also.
Below is the Rest WS implemation class
#POST
#Path("/activateService")
#Consumes({MediaType.APPLICATION_XML})
#Produces({MediaType.APPLICATION_XML})
public Response crunchifyREST**(JsonObject model**, #Context
HttpServletRequest request) {
}
#GET
#Path("/verify")
#Produces(MediaType.TEXT_PLAIN)
public Response verifyRESTService(InputStream incomingData) {
String result = "GMPPMediatorTIMService Successfully started..";
// return HTTP response 200 in case of success
return Response.status(200).entity(result).build();
}
web.xml :-
<servlet>
<servlet-name>FacadeHandsetTimpay</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.trivnet.mediator.tim.services</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>FacadeHandsetTimpay</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
I am using URI as :- http://localhost:8070/FacadeHandsetTimpay/api/getLastTimPayTransactions
I checked below links , however I could find any help
HTTP method GET is not supported by this URL. (Java rest api with jersey)
I got the solution . 12.2.1, WebLogic Server Jersey 1.x server-side APIs are no longer supported. You should use the corresponding standard JAX-RS 2.0 or Jersey 2.x APIs instead. The Jersey 1.x client API is deprecated. It is recommended that you update your RESTful client applications to use the JAX-RS 2.0 client APIs at your earliest convenience.
So , remove these dependencies:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0-m12</version>
And use these dependencies:
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.17</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.17</version>
</dependency>
And also change web.xml as follows:-
<servlet>
<servlet-name>FacadeHandsetTimpay</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.trivnet.mediator.tim.services</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>FacadeHandsetTimpay</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
Source:-
https://docs.oracle.com/middleware/12212/wls/RESTF/intro-restful-service.htm#RESTF109

mixing spring and jersy?

I have a web application that works great
and I would like to integrate it with another web application that is rest interface in JSON implemented using Jersey
the spring controllers are using RequestMapping like:
#Controller
public class AdminPrinterController {
#RequestMapping(value = "/contact/view.action")
public #ResponseBody
Map<String, ? extends Object> view() throws Exception {...}
while the Jersey controllers look like?:
#Path("/printerList")
public class PrinterListApi{
#Path("/internalPrinterList/{locationId}")
I integrated the code, but its obviously not working... probably because spring is intercepting the Jersey URL
this is my spring filter mapping:
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Can Jersey live together with spring?
Or should i switch to Spring rest implementation
Thank you
It's possible. You'll have to include the spring-jersey dependency in your project. Then you should be able to use the standard spring servlet mapping. I pulled this out of a pretty old project so you might want to check for updated versions. Mixing jersey and spring is a little messy though. I think the more modern way is to follow a spring boot rest tutorial.
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<!-- if your container implements Servlet API older than 3.0, use "jersey-container-servlet-core" -->
<artifactId>jersey-container-servlet</artifactId>
<version>2.23</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>2.23</version>
<exclusions>
<exclusion>
<artifactId>jersey-bean-validation</artifactId>
<groupId>org.glassfish.jersey.ext</groupId>
</exclusion>
<exclusion>
<artifactId>bean-validator</artifactId>
<groupId>org.glassfish.hk2.external</groupId>
</exclusion>
</exclusions>
</dependency>
Sorry for the edit. I forgot in your web.xml you'll have to point to the jersey servlet container.
<servlet>
<servlet-name>SpringApplication</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.application.MainApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringApplication</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

Jersey Servlet running on Tomcat?

Currently, it seems like my jax-rs services is using with Jersey ServletContainer but running on Tomcat. This baffled me because according to my understanding, Jersey is a server that have more functionality than Tomcat, and don't contain Tomcat, but now, my project is using Jersey's library but running on Tomcat, how could this happen?
Below is my web.xml
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- Jersey Servlet -->
<servlet>
<servlet-name>jersey-servlet</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>main.java</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-servlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
Below is my pom.xml(I used Maven):
<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warSourceDirectory>web</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20140107</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<!-- servlet dependencies -->
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.19</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.19</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.19</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.19</version>
</dependency>
</dependencies>
Tomcat is a Java web server, which implements several, but not all, Java EE specifications. Another Java web server would be Jetty. They differ from full application servers like Glassfish or JBoss / WildFly in the number of Java EE specifications they implement. The rather minimal Tomcat implements JavaServer Pages and Java Servlets, which is enough for a lot of applications.
Jersey is a Java library for both serving and calling REST (or mainly HTTP, since not everything is REST) APIs. It's build on top of Java EE specifications, so it can be used on any server that implements these specifications, e.g. Tomcat.
In your web.xml file you can define multiple servlets. What the servlet does is defined by the <servlet-class> element. You could pass your own implementation on top of the HttpServlet. In your case, you are using the Jersey servlet, which then manages all requests to the URLs it is mapped to (<servlet-mapping>). You can now learn to work with Jersey, implemented your desired API behaviour and build a web archive (.war). This web archive can then be deployed to any web server, that implements the required specifications, e.g. Tomcat. If you start using other Java EE technologies like Enterprise JavaBeans, you need to check which server implementation implements this technology. You could use Glassfish, there would be no difference for Jersey.
EDIT: I forgot to say that Jersey is one possible (the reference) implementation for the JAX-RS specification, like Tomcat is one possible Java Servlet (and others) implementation. Nevertheless, one is a web server and the other is a web service library, so its not possible to compare them or say that one has "more functionality than" the other.
Tomcat is a Servlet container. If Jersey has a Servlet, Jersey can run in Tomcat.

make wadl file resteasy without maven

i am trying to make Wadl file for rest services in my application , i am using resteasy , all tutorials and examples is to make it with maven & jersy
i dont use maven i use eclipse Wildfly and Resteasy , is there is any explanation how to make this .
thank you.
As of Resteasy 3.0.14-Final this should be possible:
https://issues.jboss.org/browse/RESTEASY-166
https://docs.jboss.org/resteasy/docs/3.1.0.Final/userguide/html/WADL.html
Add this to your web.xml:
<servlet>
<servlet-name>RESTEasy WADL</servlet-name>
<servlet-class>org.jboss.resteasy.wadl.ResteasyWadlServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RESTEasy WADL</servlet-name>
<url-pattern>/application.xml</url-pattern>
</servlet-mapping>
I tried this on WildFly 10.1.0 and I had to include an extra library:
Maven artifact: https://javalibs.com/artifact/org.jboss.resteasy/resteasy-wadl
Add the following to your pom.xml:
<!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-wadl -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-wadl</artifactId>
<version>3.0.14.Final</version>
<exclusions>
<exclusion>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
</exclusion>
</exclusions>
</dependency>
Next re-publish and access the WADL at [context-root]/application.xml

Jersey web services

I am trying to set up a simple Java web application using jersey for web services. However I have de following problem.
The tomcat server can’t find the resource http://localhost:8081/OnlineShop/rest/books/list but it can find my simple servlet http://localhost:8081/OnlineShop/index
I have the following web.xml
In the other hand I noticed that com.sun.jersey.spi.container.servlet.ServletContainer is present in my project because I added the dependency using maven however jersey.config.server.provider.packages is not present. Maybe that is the problem but I don’t know the exact dependency which I have to add.
My BookRest.java has the following code and is on the com.shop.rest package.
Finally my pom.xml has the following dependencies.
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib-ext-spring</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.18</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.19</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>2.17</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.17</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
Please, please get rid of this whole project. You're obviously a beginner and seem to be just putting random configurations and dependencies together, maybe from different tutorials. Your dependencies are incompatible and your web.xml configuration is wrong. Like i said, scrap the whole project and start from scratch. If you are just beginning, you should use one of the startup apps.
You're in Netbeans, so just do the following
File → New Project
Maven → Project from Archetype
Search jersey-quickstart-webapp
Select the one with the Group ID org.glassfish.jersey.archetypes
The latest version should be displayed.
Should be self explanitory from there
You will that the only dependency added is
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<!-- use the following artifactId if you don't need servlet 2.x compatibility -->
<!-- artifactId>jersey-container-servlet</artifactId -->
</dependency>
And the web.xml will look something like
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.stackoverflow.jersey</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
This will get you a simple app up and running. You will see a dependency that you need to un-comment for JSON support. Un-comment it. Or better yet, un-comment it, then change jersey-media-moxy to jersey-media-json-jackson. Jackson is IMO a better JSON library.
Also keep the Jersey Documentation handy for some good reading and reference material for working with Jersey

Categories