Jersey 2 servlet 404 issue - java

I have read all topics related to my issue but solution still not found.
The problem is that when i use this dependency in pom.xml
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>1.19.1</version>
</dependency>
and this serlvet mapping in web.xml
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
everything works correctly.
But when i change dependency to:
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.22.2</version>
</dependency>
and servlet mapping to:
<servlet>
<servlet-name>jerseyServlet</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.myApp</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jerseyServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
then Tomcat give me 404 error.
The main cause for using second config entry is that i need async support, and as i understand this is possible only in Jersey 2.x
Can anyone help me to solve this issue?

You need the Jersey servlet dependency
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.22.2</version>
</dependency>
Also for async support, you should also add <async-supported>true</async-supported> to your Jersey servlet declaration

Related

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.

java.lang.NoClassDefFoundError: javax/ws/rs/core/Configuration

I am implementing rest easy web service and i am using jboss 4.0 but i am getting following exception...
java.lang.NoClassDefFoundError: javax/ws/rs/core/Configuration
my web.xml is
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>RestfulWebService</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!-- <servlet>
<servlet-name>HelloServlet</servlet-name>
<load-on-startup>1</load-on-startup>
</servlet> -->
<!-- <servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>javax.ws.rs.core.Application</servlet-class>
</servlet> -->
<listener>
<listener-class>
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
</listener-class>
</listener>
<!-- <context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param> -->
<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.websevices.TestService</param-value>
</init-param>
</servlet>
<!-- <servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping> -->
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/rest</param-value>
It's easy to meet such error when migrating/copying dependencies from servlet based project to standalone. For servlet based project one usually needs dependency with scope = 'provided' relying that servlet provides implementation:
Dependency causing error:
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
<scope>provided</scope>
</dependency>
But for standalone projects there is no servlet implementation so you need to turn on copy javax.ws.rs-api to classpath. Usually you need just to remove <scope>provided</scope> line and have straightforward dependency:
Fixed dependency:
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
</dependency>
From the Exception its clear that The class loader is not able to find the class Configuration (from javax/ws/rs/core/Configuration).
Please check classpath, corresponding JAR file and make sure that the class file exist.

"URI is not registered" when use http://xmlns.jcp.org/jsf/html" in XHTML page

pom.xml file
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-impl</artifactId>
<version>2.2.0-m08</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.0-m08</version>
<scope>provided</scope>
</dependency>
xhtml file
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
web.xml file
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
Always "http://xmlns.jcp.org/jsf/html" shown in red colour and it says "URI is not registered".
I need to know, why is it say so and how it fix it. Anyone have any idea?
I am using Intellij IDEA and maven for build project.
I faced same problems when wrote jsf-based application in Intellij IDEA Community Edition. Only ultimate version has jsf support.

Security and Webflow in Spring 3.1 MVC web application configuration example?

I am trying to add webflow and security configuration to an operational Spring 3.1 MVC web application.
I got rid of the application-context.xml file and ContextLoaderListener, and use AnnotationConfigWebApplicationContext with an #EnableWebMvc #Configuration annotated class.
Yet, when I follow the webflow and security documentation, it seems like an application-context.xml file and ContextLoaderListener are necessary. The later seems to create a conflict with AnnotationConfigWebApplicationContext.
Can anyone provide a simple but complete Spring 3.1 MVC Web Application configuration with webflow and security enabled example? I am looking for a web.xml example file, plus any other required files.
If someone has a complete sample application downloadable online, it is even better.
The following works when deploying to Tomcat 7:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
metadata-complete="false">
<!-- Context Params -->
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.mypackage.web.WebConfig</param-value>
</context-param>
<!-- Filters -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Listeners -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Declaring and configuring the default Spring Servlet -->
<servlet>
<servlet-name>springExample</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- Enabling annotation configuration for web app -->
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<!-- Loading order -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springExample</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
WebConfig
#EnableWebMvc
#ImportResource({ "/WEB-INF/spring-security.xml", "/WEB-INF/spring-webflow.xml"})
#Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
// ...
}
Caveat: using #Import(SomeConfig.class) does not work anymore. It causes initialization issues. The workaround is to move the content of SomeConfig.class to WebConfig.class itself.
It is important to add proper maven dependencies, otherwise, Tomcat results in 404 Resource not found error messages:
pom.xml
<properties>
...
<spring.framework.version>3.1.2.RELEASE</spring.framework.version>
<spring.security.version>3.1.1.RELEASE</spring.security.version>
<spring.webflow.version>2.3.1.RELEASE</spring.webflow.version>
</properties>
<dependencies>
...
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.framework.version}</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.1_3</version>
</dependency>
<dependency>
<groupId>org.springframework.webflow</groupId>
<artifactId>spring-webflow</artifactId>
<version>${spring.webflow.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.webflow</groupId>
<artifactId>spring-binding</artifactId>
<version>${spring.webflow.version}</version>
</dependency>
</dependencies>

Using Tomcat, #WebFilter doesn't work with <filter-mapping> inside web.xml

Here's a working web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<filter>
<filter-name>rememberMeCookieFilter</filter-name>
<filter-class>be.example.fun.jsp.filters.RememberMeCookieFilter</filter-class>
</filter>
<filter>
<filter-name>mustBeSignedInFilter</filter-name>
<filter-class>be.example.fun.jsp.filters.MustBeSignedInFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>rememberMeCookieFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>mustBeSignedInFilter</filter-name>
<url-pattern>/private/*</url-pattern>
</filter-mapping>
</web-app>
When I remove the <filter> elements and use the following annotations instead:
#WebFilter(filterName="rememberMeCookieFilter")
public class RememberMeCookieFilter implements Filter
#WebFilter(filterName="mustBeSignedInFilter")
public class MustBeSignedInFilter implements Filter
Then Tomcat 7.0.14 gives me the following error:
java.lang.IllegalArgumentException: Filter mapping must specify either a <url-pattern> or a <servlet-name>
at org.apache.catalina.core.StandardContext.validateFilterMap(StandardContext.java:2956)
at org.apache.catalina.core.StandardContext.addFilterMap(StandardContext.java:2915)
at org.apache.catalina.deploy.WebXml.configureContext(WebXml.java:1180)
at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1270)
...
I followed the answer of this question, but that doesn't work for me.
Here are the dependencies of my web application:
<dependencies>
<!-- SLF4J (+ LOGBack) for logging -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>1.8.3</version>
</dependency>
<!-- The servlet API that I installed in my local repo -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0</version>
<type>jar</type>
<scope>provided</scope>
<!--optional>false</optional-->
</dependency>
<!-- JUnit for testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
EDIT: I only have the issue when using Tomcat (7.0.14). Glassfish is fine.
It's a bug in Tomcat 7. I reported it as issue 53354.
As it's not possible to specify the invocation order in a #WebFilter, users are forced to explicitly specify <filter-mapping> in web.xml. This works in combination with a #WebFilter(filterName) in Glassfish and JBoss AS as follows:
#WebFilter(filterName="filter1")
public class Filter1 implements Filter {}
#WebFilter(filterName="filter2")
public class Filter2 implements Filter {}
with
<filter-mapping>
<filter-name>filter1</filter-name>
<url-pattern>/url1/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>filter2</filter-name>
<url-pattern>/url2/*</url-pattern>
</filter-mapping>
However it fails in Tomcat 7.0.27 with the following confusing exception (the <url-pattern> is been set)
Caused by: java.lang.IllegalArgumentException: Filter mapping must specify either a <url-pattern> or a <servlet-name>
at org.apache.catalina.core.StandardContext.validateFilterMap(StandardContext.java:3009)
at org.apache.catalina.core.StandardContext.addFilterMap(StandardContext.java:2968)
at org.apache.catalina.deploy.WebXml.configureContext(WebXml.java:1207)
at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1294)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:855)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:345)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5161)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 7 more
In the meanwhile your best bet is to use Glassfish or JBoss AS instead, or to register the filters by <filter> anyway.
You must specify a target for the Servlet Filter. Either provide a value for 'servletNames' or 'urlPatterns'.
http://docs.oracle.com/javaee/6/api/javax/servlet/annotation/WebFilter.html
e.g.
#WebFilter(filterName="mustBeSignedInFilter", urlPatterns={ "/signed/in/path/*" })
public class MustBeSignedInFilter implements Filter

Categories

Resources