In my Java app, I'm getting the following error for all Rest service calls like:
https://example.com/secselfservice/rest/ping
which returns: HTTP Status 500 - Authenticator.invoke() failed
Log of the error:
javax.servlet.ServletException: Authenticator.invoke() failed at
com.sap.core.jpaas.security.auth.service.lib.AbstractAuthenticator.invoke(AbstractAuthenticator.java:174)
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)
at
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)
at
com.sap.core.tenant.valve.TenantValidationValve.invokeNextValve(TenantValidationValve.java:168)
at
com.sap.core.tenant.valve.TenantValidationValve.invoke(TenantValidationValve.java:94)
at
com.sap.js.statistics.tomcat.valve.RequestTracingValve.invoke(RequestTracingValve.java:38)
at
com.sap.core.js.monitoring.tomcat.valve.RequestTracingValve.invoke(RequestTracingValve.java:27)
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:442)
at
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1083)
at
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:640)
at
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:807) Caused by:
java.lang.NullPointerException: while trying to invoke the method
java.lang.String.length() of a null object loaded from local variable
'name' at
org.eclipse.osgi.internal.loader.BundleLoader.loadClass(BundleLoader.java:338)
at
org.eclipse.osgi.framework.internal.core.BundleHost.loadClass(BundleHost.java:229)
at
org.eclipse.osgi.framework.internal.core.AbstractBundle.loadClass(AbstractBundle.java:1212)
at
org.eclipse.gemini.web.tomcat.internal.loading.BundleDelegatingClassLoader.findClass(BundleDelegatingClassLoader.java:91)
at
org.eclipse.gemini.web.tomcat.internal.loading.BundleDelegatingClassLoader.loadClass(BundleDelegatingClassLoader.java:139)
at java.lang.ClassLoader.loadClass(ClassLoader.java:427) at
org.eclipse.gemini.web.tomcat.internal.loading.ChainedClassLoader.doLoadClass(ChainedClassLoader.java:174)
at
org.eclipse.gemini.web.tomcat.internal.loading.ChainedClassLoader.loadClass(ChainedClassLoader.java:164)
at
org.eclipse.gemini.web.tomcat.internal.loading.BundleWebappClassLoader.loadClass(BundleWebappClassLoader.java:298)
at java.lang.ClassLoader.loadClass(ClassLoader.java:427) at
org.apache.catalina.core.StandardWrapper.servletSecurityAnnotationScan(StandardWrapper.java:1211)
at
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:494)
at
com.sap.core.jpaas.security.auth.service.lib.AbstractAuthenticator.invoke(AbstractAuthenticator.java:170)
... 16 common frames omitted
Here is the code of my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>secselfservice</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>com.sap.coe.securityselfservice.rest.SecSelfServApplication</servlet-name>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>com.sap.coe.securityselfservice.rest.SecSelfServApplication</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<resource-ref>
<res-ref-name>user/Provider</res-ref-name>
<res-type>com.sap.security.um.user.UserProvider</res-type>
</resource-ref>
<resource-ref>
<res-ref-name>mail/SAPInternalNWCloudSession</res-ref-name>
<res-type>javax.mail.Session</res-type>
</resource-ref>
<login-config>
<auth-method>FORM</auth-method>
</login-config>
<security-role>
<description>All SAP HANA Cloud Platform users</description>
<role-name>EVERYONE</role-name>
</security-role>
<security-role>
<description>Members of security team</description>
<role-name>Approver</role-name>
</security-role>
<security-role>
<description>Read only access to admin view</description>
<role-name>Viewer</role-name>
</security-role>
<security-role>
<description>Superadmin</description>
<role-name>Tester</role-name>
</security-role>
<security-constraint>
<web-resource-collection>
<web-resource-name>all</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>Approver</role-name>
<role-name>Viewer</role-name>
<role-name>Tester</role-name>
<role-name>EVERYONE</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<ejb-local-ref>
<ejb-ref-name>ejb/CustomDataEJB</ejb-ref-name>
<local>com.sap.coe.securityselfservice.ejb.Customization</local>
</ejb-local-ref>
<ejb-local-ref>
<ejb-ref-name>ejb/MailTemplateEJB</ejb-ref-name>
<local>com.sap.coe.securityselfservice.ejb.MailTemplates</local>
</ejb-local-ref>
<ejb-local-ref>
<ejb-ref-name>ejb/QuestionEJB</ejb-ref-name>
<local>com.sap.coe.securityselfservice.ejb.Questions</local>
</ejb-local-ref>
<ejb-local-ref>
<ejb-ref-name>ejb/UsrVariantEJB</ejb-ref-name>
<local>com.sap.coe.securityselfservice.ejb.UsrVariants</local>
</ejb-local-ref>
<ejb-local-ref>
<ejb-ref-name>ejb/ProjectEJB</ejb-ref-name>
<local>com.sap.coe.securityselfservice.ejb.Projects</local>
</ejb-local-ref>
<ejb-local-ref>
<ejb-ref-name>ejb/ProjectRequestEJB</ejb-ref-name>
<local>com.sap.coe.securityselfservice.ejb.ProjectRequests</local>
</ejb-local-ref>
<ejb-local-ref>
<ejb-ref-name>ejb/UserEJB</ejb-ref-name>
<local>com.sap.coe.securityselfservice.ejb.Users</local>
</ejb-local-ref>
<ejb-local-ref>
<ejb-ref-name>ejb/ResourcesEJB</ejb-ref-name>
<local>com.sap.coe.securityselfservice.ejb.Resources</local>
</ejb-local-ref>
<ejb-local-ref>
<ejb-ref-name>ejb/ProjectAnswerObserverEJB</ejb-ref-name>
<local>com.sap.coe.securityselfservice.ejb.observer.ProjectAnswerObserver</local>
</ejb-local-ref>
<ejb-local-ref>
<ejb-ref-name>ejb/ProjectDataObserverEJB</ejb-ref-name>
<local>com.sap.coe.securityselfservice.ejb.observer.ProjectDataObserver</local>
</ejb-local-ref>
<context-param>
<param-name>resteasy.providers</param-name>
<param-value>com.sap.coe.securityselfservice.rest.exception.WebServiceExceptionHandler</param-value>
</context-param>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
The code of the servlet SecSelfServApplication:
/**
* Application
*/
package com.sap.coe.securityselfservice.rest;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;
/**
* JAX-RS Application
*/
public class SecSelfServApplication extends Application {
#Override
public Set<Class<?>> getClasses() {
Set<Class<?>> s = new HashSet<Class<?>>();
s.add(InitService.class);
s.add(MailTemplateService.class);
s.add(PingService.class);
s.add(ProjectRequestService.class);
s.add(ProjectService.class);
s.add(QuestionService.class);
s.add(ResourceService.class);
s.add(UserService.class);
s.add(UsrVariantService.class);
return s;
}
}
And for PingService.java:
package com.sap.coe.securityselfservice.rest;
import javax.servlet.ServletException;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
//import com.qmino.miredot.annotations.ReturnType;
import com.sap.coe.securityselfservice.rest.bean.WebServiceException;
import com.wordnik.swagger.annotations.ApiOperation;
/**
* Rest Service - Ping
*
* #author C5178621
* #servicetag Ping
*/
#Path("/ping")
public class PingService {
/**
* Ping the rest service application and check if it is alive
*
* #summary Test Application Alive
* #return "pong" if application is up and running
* #throws WebServiceException
*/
#GET
#Path("/")
#Produces(MediaType.TEXT_PLAIN)
#ApiOperation(value = "ping", notes = "ping", response = String.class)
public Response ping() throws ServletException {
return Response.ok().entity("pong").build();
}
}
Also the pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--<?xml version="1.0" encoding="UTF-8"?>-->
<parent>
<groupId>com.sap.it.mobile</groupId>
<artifactId>hcp-parent-pom</artifactId>
<version>2.3.3</version>
</parent>
<groupId>com.sap.coe</groupId>
<artifactId>SecSelfService</artifactId>
<version>1.1.14-SNAPSHOT</version>
<packaging>war</packaging>
<licenses>
<license>
<name>SAP DEVELOPER LICENSE AGREEMENT</name>
<url>https://tools.hana.ondemand.com/developer-license-3.1.txt</url>
</license>
</licenses>
<scm>
<url>git#github.example.corp:SRCOffice/SelfService_Backend.git</url>
<connection>scm:git:git#github.example.corp:SRCOffice/SelfService_Backend.git</connection>
<developerConnection>scm:git:git#github.example.corp:SRCOffice/SelfService_Backend.git</developerConnection>
<tag>HEAD</tag>
</scm>
<properties>
<java-version>1.7</java-version>
<war.name>secselfservice</war.name>
<sap.cloud.application>secselfservice</sap.cloud.application>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<!-- Project plugin versions -->
<version.eclipse-plugin>2.6</version.eclipse-plugin>
<version.shade-plugin>2.2</version.shade-plugin>
</properties>
<!--Use Java EE6-->
<profiles>
<profile>
<id>neo-javaee6-wp</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>com.wordnik</groupId>
<artifactId>swagger-jaxrs_2.10</artifactId>
<version>1.3.13</version>
<exclusions>
<exclusion>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
<dependency>
<groupId>com.sap.it.mobile</groupId>
<artifactId>hcp-java-util</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>${java-version}</source>
<target>${java-version}</target>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.2</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<warName>secselfservice</warName>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>${version.eclipse-plugin}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${version.shade-plugin}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
</plugins>
</pluginManagement>
</build>
</project>
Anyone has an idea how to solve this?
Thank you
Hmm, obviously classloading problems with no very useful hint from the eclipse (osgi) framework...
I've seen from time to time failing apps when they bring their own versions of libraries which collide with some libs that are already part of the provided platform.
Consider this HCP maven blog: there they reference one artifact and most imporantly they use <scope>provided</scope> to tell maven to NOT include the libs into your app's war file (as some jars are already provided by the platform).
My best bet is that at least org.eclipse.persistence is already provided by the platform (and maybe other libs you want as well).
I faced the same error (my Q&A you can find here) and found a solution in this SAP discussion.
For me the error did no longer occur as soon as I added jersey manually and removed the <servlet-class> tag from the web.xml In the end the configuration within the web.xml wasn't necessary at all for my simple rest service (see this answer).
So It seems that this is a Hana cloud platform specific problem.
Related
I'm learning Java web technologies and I'm trying to deploy a trivial REST service using Jersey, when I try to access the resource I get greeted with the following error:
SEVERE: Servlet.service() for servlet [Jersey REST Service] in context with path [/mkstick] threw exception [org.glassfish.jersey.server.ContainerException: java.lang.NoSuchMethodError: 'void jakarta.servlet.http.HttpServletResponse.setStatus(int, java.lang.String)'] with root cause
java.lang.NoSuchMethodError: 'void jakarta.servlet.http.HttpServletResponse.setStatus(int, java.lang.String)'
My java class resource:
package com.dbmw.mkstick;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
#Path("/hello")
public class Resource {
#GET
#Produces(MediaType.TEXT_HTML)
public String getText() {
return "<html><head/><body>Hello world!</body></html>" ;
}
}
My web xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>Jersey REST Service</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.dbmw.mkstick</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dbmw</groupId>
<artifactId>mkstick</artifactId>
<version>0.1</version>
<name>mkstick</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
</properties>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.bundles</groupId>
<artifactId>jaxrs-ri</artifactId>
<version>3.0.4</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
<finalName>mkstick</finalName>
</build>
<packaging>war</packaging>
</project>
From what I can grasp it seems to be an issue with versions and I've tried to get every dependency up to date but it has not helped at all.
As comment said, if you are deploying on Tomcat 10.1.x or greater you need to upgrade your jersey version to 3.1+ (it is out now).
To get a starting template use the maven build archetype for Jersey 3.1.1 as seen here.
This is a confusing error too because the updates from Tomcat 10.0.x to Tomcat 10.1.x entail an entire version jump in Servlet spec (version 5 to 6) see link. Usually there arent such big version changes within the same major version ie 10.x
I am new to swagger and jersey.
My api operations are working fine. I followed the steps in the link: https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5 to add swagger into my api. When I give my try to access the swagge.json via the url:http://localhost:8080/messenger/webapi/swagger.json I get 404 -Not found. Here are my api codes. Can you please help me out with this.
pom.xml file
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.vishwas</groupId>
<artifactId>messenger</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>messenger</name>
<build>
<finalName>messenger</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<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>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-jersey2-jaxrs</artifactId>
<version>1.5.0</version>
</dependency>
</dependencies>
<properties>
<jersey.version>2.16</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" 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_2_5.xsd">
<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>
io.swagger.jaxrs.listing,
com.vishwas.messenger
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>Swagger Configuration</servlet-name>
<servlet-class>com.vishwas.messenger.sevlet.SwaggerConfigurationServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Jersey2Config</servlet-name>
<servlet-class>io.swagger.jersey.config.JerseyJaxrsConfig</servlet-class>
<init-param>
<param-name>api.version</param-name>
<param-value>1.0</param-value>
</init-param>
<init-param>
<param-name>swagger.api.basepath</param-name>
<param-value>http://localhost:8080/messenger/webapi</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
</web-app>
3.Beans config class for swagger
package com.vishwas.messenger.sevlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import io.swagger.jaxrs.config.BeanConfig;
public class SwaggerConfigurationServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
public void init(ServletConfig config) throws ServletException {
super.init(config);
BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("1.0");
beanConfig.setSchemes(new String[]{"http"});
beanConfig.setHost("localhost:8080");
beanConfig.setTitle("Messenger Api documentation");
beanConfig.setBasePath("/messenger/webapi");
beanConfig.setResourcePackage("com.vishwas.messenger.resources");
beanConfig.setPrettyPrint(true);
beanConfig.setScan(true);
}
}
[output screenshot][1]
[1]: https://i.stack.imgur.com/fLx83.png
I found the solution to the problem, I changed the swagger-jersey2-jaxrs artifact version to the latest one and also installed the compile dependencies along with it which was provided in the maven repository website.
Please check the version properly!!
I am sending a GET request to my StoryBoardResource, class responsible for generating JSON format data, using POSTMAN app. The associated method consumes nothing but produces Application/JSON data: #Produces(MediaType.APPLICATION_JSON).
I am recieving 500 internal server error on POSTMAN though there is nothing showing up on my IDE's console as am logging some statements too for debugging purpose.
If there had been some jar file issue it must have thrown an error for no MesageBodyWriter found ...right? I have updated pom.xml to include JSON dependency and the jar is also there.
Here is my StoryBoardResource class to which the request is delegated:
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import org.harsh.webapp.storyBoard.model.StoryBoardModel;
import org.harsh.webapp.storyBoard.service.StoryBoardService;
#Path("/authenticateUser")
public class StoryBoardResource {
private StoryBoardService sts = new StoryBoardService();
#GET
#Produces(MediaType.APPLICATION_JSON)
public StoryBoardModel authUser(#QueryParam("username") String username, #QueryParam("password") String password){
return sts.authUser(username, password);
}
}
which calls authUser method on my service class StoryBoardService :
public StoryBoardModel authUser(String username, String password){
System.out.println("In here");
if(username.equals(map.get(username).getUsername()) && password.equals(map.get(username).getPassword())){
System.out.println("true");
return map.get(username);
}
return map.get(username);
}
Could this be bcoz of some jar or Jersey version conflict?
Here is my pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.harsh.webapp</groupId>
<artifactId>storyBoard</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>storyBoard</name>
<build>
<finalName>storyBoard</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<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>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
</dependencies>
<properties>
<jersey.version>2.16</jersey.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
And here is a snapshot of Maven Dependencies
Sorry for such a long post but I thought I should explain things clearly.
Please help on finding what could I be doing wrong here?
And web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" 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_2_5.xsd">
<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>org.harsh.webapp.storyBoard</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/webapi/*</url-pattern>
</servlet-mapping>
</web-app>
I worked it out and it appeared so that I missed adding a NO-Argumentconstructor in a model class.
public someClass(){};
Always remember to add a No-Arg constructor whenever you create a model class for your Maven-Jersey application.
And it worked!
However, I am not clear on why would a no-arg constructor make the application run or fail!!
If any body know the answer to this. Please share your knowledge!!
I always end up with a 404 response.
Resource not found. Testing with tomcat
v8.0. Below are the
details.
URL : http://localhost:8080/ESTServer/rest/message/hello
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ESTServer</groupId>
<artifactId>ESTServer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>JBoss repository</id>
<url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.13.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-servlet-initializer</artifactId>
<version>3.0.13.Final</version>
</dependency>
</dependencies>
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" version="3.1"> <display-name>ESTServer</display-name>
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/rest</param-value>
</context-param>
<servlet>
<servlet-name>resteasy-servlet</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.mota.rest.CaDistributionApplication</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
Service code :
package com.mota.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
#Path("/message")
public class CaDistributionService {
#GET
#Path("/{param}")
public Response printMessage(#PathParam("param") String msg) {
String result = "Restful example : " + msg;
return Response.status(200).entity(result).build();
}
}
project structure
CaDistributionApplication.java
package com.mota.rest;
import javax.ws.rs.core.Application;
import java.util.HashSet;
import java.util.Set;
import com.mota.rest.CaDistributionService;;
public class CaDistributionApplication extends Application{
private Set<Object> singletons = new HashSet<Object>();
public CaDistributionApplication() {
singletons.add(new CaDistributionService());
}
#Override
public Set<Object> getSingletons() {
return singletons;
}
}
What am I missing here ?
Please help !!!!
I just tried restarting the eclipse.
Multiple clean/build/refresh/install.
Checking the Target folder for generated
war and classes. Launching tomcat in debug mode.
But basically didn't change anything in the code.
And yes it works finally.
I'm trying to make a webservice and pack this into a .war file.
Everything worked fine, untill i made some edits to the pom. After a few CTRL+Z's i can't get it back the way is was..
My current (not working pom) gives the following error:
Failed to collect dependencies at com.sun.jersey:jersey-client:jar:1.8
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gis</groupId>
<artifactId>com.gis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src.com.gis</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.8</version>
</dependency>
</dependencies>
My web.xml:
<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>Restful Web Application</display-name>
<servlet>
<servlet-name>jersey-servlet</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.gis</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
I have added the following jars to my Build Path:
java-json.jar
javax.ws.rs-api-2.0.1.jar
jersey-bundle-1.8.jar
jersey-server.jar
Can anyone give me a hint to make this work properly again?
EDIT: Removing .jars and closing proxy did the trick.
Unfortunatly, the next error is coming in:
SEVERE: Servlet [jersey-servlet] in web application [/com.gis] threw load() exception java.lang.ClassNotFoundException: com.sun.jersey.spi.container.servlet.ServletContainer
Any hints anyone?
Updated:Add jersey-server dependency
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
</dependency>
Try: Rightclick on project > Build with dependencies