JERSEY and JAX-RS - java

I am trying to deploy a simplest REST service using jersey, and JAX-RS, but I am getting this error,
HTTP ERROR: 404
NOT_FOUND
RequestURI=/hosting/demo/example
Powered by Jetty://
Where I think I have done everything right, below is the code I am using for it.
POM.XML (only pasting the part related to jersey)
<dependency>
<!-- the implementation of JAX-RS -->
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.0.1</version>
<scope>compile</scope>
</dependency>
WEB.XML
<servlet>
<servlet-name>jersey</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>mop.core.service.restservices</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>jersey</servlet-name>
<url-pattern>/demo</url-pattern>
</servlet-mapping>
My Class having #GET
package mop.core.service.restservices;
import javax.swing.JOptionPane;
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
#Path("/example")
public class PricePointRestService {
#GET
#Produces("text/plain")
public String getPricePoint(){
JOptionPane.showMessageDialog(null, "GET CALLED");
return "hello";
}
#POST
#Produces("application/xml")
#Consumes({"application/x-www-form-urlencoded", "multipart/form-data"})
public String doPost(#FormParam("xml") String xml) {
return "<xml></xml>";
}
}
The url I hit is: http://localhost/hosting/demo/example

Change your url-pattern in your web.xml to:
<url-pattern>/demo/*</url-pattern>

Are you sure the "hosting" part of the url is correct? What is the web app deployed under? Can you hit any pages in the web app?
Try adding a jsp to the web-app and see if you can hit that.

The problem i figured out was compatibility issue of ASM 2.2.3 and Jersey which requires ASM 3.1.
Hibernate depends upon ASm 2.2.3.
This bug is reported at http://opensource.atlassian.com/projects/hibernate/browse/EJB-358?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
I am trying to find out the work arounds for this, but those doesnt seem to work out, as cglib 2.2 and cglib-nodep also doesnt help.
javassist cannot be used through XML config file of hibernate, it requires hibernate.properties file, where the project i am working on used hibernate.cfg.xml.
Any Solution ?

Agree with Damo on:
<url-pattern>/demo/*</url-pattern>
Also add:
<load-on-startup>1</load-on-startup>
before:
</servlet>
I assume you're using Maven. Hibernate 3.2.4 runs fine on ASM 3.1 so you can ignore that dependency. This entry will work with Jersey.
<dependency><!-- hibernate 3.2.4.ga -->
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.2.6.ga</version>
<exclusions>
<exclusion>
<groupId>asm</groupId>
<artifactId>asm</artifactId>
</exclusion>
<exclusion>
<groupId>asm</groupId>
<artifactId>asm-attrs</artifactId>
</exclusion>
<exclusion>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
</exclusion>
</exclusions>
</dependency>

Related

Jersey 2.x JSP pages redirection

I have this problem. I'm trying to use Jersey 2.x in order to make REST api, and I would like to use jsp pages for my template (everything without spring or springboot, I can't include them into my project). I have a maven project with this structure:
enter image description here
the pom.xml has the following dependencies:
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.27</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-mvc-jsp</artifactId>
<version>2.27</version>
</dependency>
</dependencies>
and the web.xml is:
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>testjsp</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>it.testjsp.config.JerseyConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>testjsp</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
When I run my app with tomcat, the following url
http://localhost/testjsp
redirects correctly the index.hml.
I have configured the JerseyConfig class in the following way:
#ApplicationPath("/api")
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
packages("it.testjsp.endpoints");
register(JspMvcFeature.class);
property("jersey.config.server.mvc.templateBasePath", "/WEB-INF/jsp");
}
}
In order to map all the end points exposed into that package and to use jsp pages into WEB-INF/JSP.
I have two end points:
#Path("/test")
#Produces(MediaType.APPLICATION_JSON)
public class TestEndPoint {
#GET
public Map<String, Object> testApi() {
System.out.println("test jersey 2.27");
Map<String, Object> result = new HashMap<String, Object>();
result.put("result", "test jersey 2.27");
return result;
}
}
This is inside TestEndPoint class and the application responds as I expect (http://localhost/testjsp/api/test returns the json).
The PagesEndPoint is:
#Path("/pages")
public class PagesEndPoint {
#GET
#Path("{pageName}")
public Viewable getPage(#PathParam("pageName") String pageName) {
System.out.println("Try " + pageName + ".html");
return new Viewable("/" + pageName + ".html");
}
}
But when I run the app with tomcat, I have always a 404. Is possible to use jsp (or other html pages)? What I did wrong? Thanks for the help

Adding Multipart feature into Jersey Web Service in Tomcat

I am trying to implement a Jax-RS(Jersey) which has a /upload end-point where user can upload images using Multipart. Project already has other parts implemented and working correctly, the problem occurs when I try to add Multipart feature.
I am using IntelliJ IDEA and Tomcat 8.5.xx to deploy my app.
My dependencies in pom.xml as follows;
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.25.1</version> <!-- 2.10.1 -->
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.25.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.25.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.42</version>
</dependency>
</dependencies>
And here is my web.xml part;
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value> org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
</init-param>
When I launch the WAR in Tomcat, it shows Internal Server Error 500 and in logs;
java.lang.IllegalStateException: The resource configuration is not modifiable in this context.
org.glassfish.jersey.server.ResourceConfig$ImmutableState.register(ResourceConfig.java:274)
org.glassfish.jersey.server.ResourceConfig$ImmutableState.register(ResourceConfig.java:221)
org.glassfish.jersey.server.ResourceConfig.register(ResourceConfig.java:453)
org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:387)
org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:177)
org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:369)
javax.servlet.GenericServlet.init(GenericServlet.java:158)
I don't know what is the reason for the problem. From the name of the exception, I guess it is related with the MultipartFeature that is added by the web.xml. Yet, as I am not experienced enough and my searchs on google not resulted good, I need assistance from more experienced person.
Well, such error generally come when there are more than one mapping to jersey resource method for that particular URL.
You mentioned in the question that you are adding path for upload web service /upload . So, possible reason is this path already exist for one of your API developed before. So, check and update this path accordingly.
There are other possible reasons also for this error which you can find here and here.

Spring MVC : URL Pattern

here is my web.xml
<servlet>
<servlet-name>Learn</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Learn</servlet-name>
<url-pattern>/learn/*</url-pattern>
</servlet-mapping>
if i change this code
<url-pattern>/learn/*</url-pattern>
to
<url-pattern>/learn/abc/</url-pattern>
i can hit my controller code which is given as
#Controller
#RequestMapping(value = "/learn")
public class ControllerClass
{
#RequestMapping(value = "/", method = RequestMethod.GET)
public String callRequest(ModelMap model)
{
return "index";
}
#RequestMapping(value = "/abc/", method = RequestMethod.GET)
public String personController(ModelMap model)
{
return "welcome";
}
}
but i also want to hit the first method or i will add more method, which i can not achieve by
/learn/abc/
in url mapping.
so please help me out with this
pom.xml
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- spring-context which provides core functionality -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<!-- The spring-aop module provides an AOP Alliance-compliant aspect-oriented
programming implementation allowing you to define -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<!-- The spring-webmvc module (also known as the Web-Servlet module) contains
Spring’s model-view-controller (MVC) and REST Web Services implementation
for web applications -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<!-- The spring-web module provides basic web-oriented integration features
such as multipart file upload functionality and the initialization of the
IoC container using Servlet listeners and a web-oriented application context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
</dependencies>
please let me know if i missed any dependency
With this url-pattern <url-pattern>/learn/*</url-pattern> configuration, to hit your method, the url path will be: /learn/learn
The url-pattern atribute on the web.xml works like a basepath for spring's servlet. So a good option is to change your url-pattern to /* , like this:
<url-pattern>/*</url-pattern>
Or if you want some basepath, change the url pattern to something like this:
/basepath/*
And to hit your method, you need to use the path url: /basepath/learn

Creating my own REST API throws servlet execution exception

I have been working on my own REST API built with jersey and java. Today it stopped working though after a friend who also works on it pushed his changes. he didn't change anything on the dependency side, but he did add an controller that our main API class creates. Now whenever I try to reach a resource the tomcat server throws an error:
exception
javax.servlet.ServletException: Servlet execution threw an exception
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.AbstractMethodError: javax.ws.rs.core.UriBuilder.uri(Ljava/lang/String;)Ljavax/ws/rs/core/UriBuilder;
javax.ws.rs.core.UriBuilder.fromUri(UriBuilder.java:119)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:669)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.ja
We belive it started after he added jsoup dependency.
EDIT:
I edited my dependencies and web.xml, and now I only get 404 not found.
This is my dependencies form my pom.xml
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.23.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.23.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.3.8</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.twitter4j</groupId>
<artifactId>twitter4j-core</artifactId>
<version>[4.0,)</version>
</dependency>
<dependency>
<groupId>org.facebook4j</groupId>
<artifactId>facebook4j-core</artifactId>
<version>[2.4,)</version>
</dependency>
</dependencies>
This is my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>api</display-name>
<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>
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
Edit
If I try to reach: http://localhost:8080/api-mashup-api/api/v1/foobar
#Path("/v1")
public class API {
private Controller controller;
public API(){
controller = new Controller();
}
/**
* Prints to the screen if we are in /v1/foobar
*
* #return
*/
#GET
#Path("/foobar")
#Produces(MediaType.TEXT_HTML)
public String print2() {
return "Please specify what resource you need.";
}
I just get 404.
Probably your issue caused by wrong jersey-container-servlet which may cause the wrong uribuilder to be pickedup
https://jersey.java.net/documentation/latest/modules-and-dependencies.html#server-jdk
jersey-container-servlet => Jersey core Servlet 3.x implementation
jersey-container-servlet-core => Jersey core Servlet 2.x implementation
Change:
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.23.2</version>
<scope>provided</scope>
</dependency>
to
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.23.2</version>
<scope>provided</scope>
</dependency>
Also refer https://jersey.java.net/documentation/latest/modules-and-dependencies.html#dependencies to find out which dependencies you need to provide for Glassfish and other Servlet based (non jax-rs integrated) containers
1) You have an AbstractMethodError exception which is thrown when an application tries to call an abstract method.
uri is an abstract method in UriBuilder, so you need an implementation of this.
You should use JAX-RS 2.0 with Jersey 2.* that implements JAX-RS 2.0 and contains an implementation to uri method.
2) By looking at your stack trace it clearly states that you are using both Jersey versions 1 & 2 and that's not possible, So the classloader is picking up the wrong URIBuilder class.
Stack Trace:
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:669)
The Jersey dependencies in group com.sun.jersey are all Jersey version 1. Jersey version 2 uses the group org.glassfish.jersey.

Restfull Webservice Maven Project netbeans

I'm looking at every way to try to make a request type to get to my web service created by restfull Webservice into a project with Tomcat , Maven and some servlets , but nothing I do not start by mistake does not find the resource . What am I doing wrong? I may not have configured the web.xml ? how can I do?
I put the file pom.xml below and
<dependencies>
<dependency>
<groupId>org.glassfish.metro</groupId>
<artifactId>webservices-rt</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.8.3</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20150729</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.5.4</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901-1.jdbc4</version>
</dependency>
</dependencies>
This is code of my web service:
#Path("ws")
public class WsUserJson {
#Context
private UriInfo context;
/**
* Creates a new instance of WsUserJson
*/
public WsUserJson() {
}
/**
* Retrieves representation of an instance of com.lillonet.testmavenservletws.WsUserJson
* #return an instance of java.lang.String
*/
#GET
#Produces("application/json")
public String getJson(#QueryParam("name") String nome) {
//TODO return proper representation object
return "{"+nome+"}";
}
/**
* PUT method for updating or creating an instance of WsUserJson
* #param content representation for the resource
* #return an HTTP response with content of the updated or created resource.
*/
#PUT
#Consumes("application/json")
public void putJson(String content) {
}
}
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
That is only a jar with a bunch of interfaces. There is no implementation. That dependency should only be used when you plan on deploying to a an EE compliant Server, like Glassfish or Wildfly. Tomcat is not an EE compliant server. It is only a Servlet container. Therefore any features you use from that javaee-web-api, you need to also include an implementation.
So for now, just get rid of it so you don't use ant classes for which there is no implementation. Then you need to decide on a JAX-RS implementation to use. For now I'll just say to use Jersey. So just add this dependency
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.22.1</version>
</dependency>
Then you need to configure the application in the web.xml. You can see here for more options. You basically want something like
<web-app>
<servlet>
<servlet-name>MyApplication</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>org.foo.myresources</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>MyApplication</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
The param-value in the init-param is the package you want Jersey to scan to pick up and register all your classes annotated with #Path and #Provider. The scan is recursive, so you can list the root-most package in your project if you have your resource scattered in different packages.
From here it should work. Then for JSON/POJO support, you can just add the following dependency
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.22.1</version>
</dependency>
No extra configuration is needed for that.

Categories

Resources