jax-rs jersey servlet not found - java

I'm trying to get this tutorial to work
https://avaldes.com/file-upload-example-using-restful-web-service-with-jax-rs-and-jersey/comment-page-1/#comment-319
This looks like an eclipse project. I downloaded it and imported it into eclipse. How to run it? There was no "run on server" or "run as web page". The only options available was "run as application" and "run as applet", neither of which this is. My project tree looks just like theirs except for JRE System Library, Pivotal tc Server, and Web App Libraries. I gave up.
Next I went to NetBeans and imported the eclipse project. Clicked green Run button and up popped the web page, running on tomcat. But the url showed http://localhost:8084/, not localhost:8084/RestfulFileUploadExample.
I clicked the Browse... button and grabbed a file. Then clicked Upload File button. It put the following url into the browser: http://localhost:8084/RestfulFileUploadExample/rest/files/upload but got "The requested resource is not available. Now what?

there are issue in web.xml file check this file again it should be on WEB-INF folder and then check mapping of class file is proper or not. you can use this bellow code :-
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:Beans.xml</param-value>
</context-param>
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.demo.services</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

Related

spring framework nesting java.io.FileNotFoundException:

I want to run my first web application and I am getting this error
I have searched the web but I cant figure out what the problem is.
I have trying googling what nesting is but I could not understand what it is talking about
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/todo-servlet.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/todo-servlet.xml]
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:342)
web.xml
The path to the todo-servlet is configured here.
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/todo-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/spring-mvc/*</url-pattern>
</servlet-mapping>
I have created todo-servlet.xml in src/main/webapp/WEB-INF/todo-servlet.xml
but I am still getting file not found exception
Refer below link and follow step by step check configuration in xml as well as in class and file path locations.
http://websystique.com/springmvc/spring-4-mvc-helloworld-tutorial-full-example/
This usually happens in a Spring Web application, when a DispatcherServlet is set up in the web.xml for Spring MVC. By default, Spring will look for a file called exactly springMvcServlet-servlet.xml in the /WEB-INF directory of the web application.mvc-servlet.xml file that indeed exists under /WEB-INF.
or it may happen because when something in the application points to an XML resource that doesn't exist, or is not placed where it should be.
Create the file and to place it under the /src/main/resources directory of the project – this way, the file will exist on the classpath.

Run only HTML or JSP page in spring mvc project without the controller

tried adding servlet mapping in web.xml as shown below but unable to load the page.
Here is the directory structure for webapp|->html|->hello.jsp or hello.html (tried with both jsp and html)
Click here to see directory structure
web.xml file
<servlet>
<servlet-name>hello</servlet-name>
<jsp-file>/html/hello.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
tried with multiple path in jsp-file tag for example /webapp/html/hello.jsp but end up with ERROR: javax.servlet.ServletException: missing jspFile:
This error at least tells servlet dispatcher is able to get the request /hello . but further its failing to load JSP or HTML page.

Deploying a webapp under ROOT - index.html not displaying

I am using tomcat7 and have a small Java application that needs to be deployed under ROOT. The reason for this has to do with the client's inability to specify the webapp's proper context path at this time.
I have an index.html in this webapp and my issue is that when I deploy the application under the webapp name - ${catalina_home}/mywebapp - the index.html renders without a problem when I navigate to http://localhost:8080/mywebapp.
However, when I deploy it under ROOT - ${catalina_home}/ROOT - the index.html inside does not render when I navigate to http://localhost:8080/. The error is 404 not found. Does this have anything to do with overriding tomcat's default page?
My web.xml:
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/</param-value>
</context-param>
...
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
...
Can someone explain why this is so?
Thanks in advance.
UPDATE: I see in this stackoverflow Display html page in tomcat from maven RESTEasy webapp that the issue may be that my filter says anything under /* should go to the servlet. I tried to set up a default servlet for .html pages, but that does not see to help yet...
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
How do you deploy the app as ROOT? If you package as war file, to deploy as ROOT, change the war file to ROOT.war and have the index.html under the ROOT folder. Also check the web.xml under WEB-INF to have the welcom page set to index.html as below:
<welcome-file-list>
<welcome-file>
index.html
</welcome-file>
</welcome-file-list>
I found the answer here - http://docs.jboss.org/resteasy/docs/3.0.2.Final/userguide/html_single/:
"The downside of running Resteasy as a Servlet is that you cannot have static resources like .html and .jpeg files in the same path as your JAX-RS services. Resteasy allows you to run as a Filter instead. If a JAX-RS resource is not found under the URL requested, Resteasy will delegate back to the base servlet container to resolve URLs."
I created a filter instead and I was able to serve both the static page and my regular REST resources.

Deploy Jersey web services in referenced .jar

I've created a .jar with some Jersey web services in it, and I want to import that .jar in other projects. Those web services work like a handler/proxy for local code to link with a remote service provider.
So, I import the .jar document in a project and configure web.xml like this:
<servlet>
<servlet-name>Services</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.package.for.services.in.current.project, com.package.for.services.in.jar</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Services</servlet-name>
<url-pattern>/s/*</url-pattern>
</servlet-mapping>
But when I run the project, the web services specified in com.package.for.services.in.jar aren't found.
The .jar document is in build path. What am I doing wrong? Isn't possible to run web services from an external library?
Thanks in advance.
I found the answer from this post: Jersey REST The ResourceConfig instance does not contain any root resource classes
The PackagesResourceConfig class depends on having a directory with the name of the package in order to find the classes within it. I went back to Eclipse, and found an option at the bottom of the "Export ... > Jar" dialog for "Add directory entries." I turned that on, exported the jar again, and then the scanning found my resource classes. You'll need to find some comparable option in your build environment.

Remote application debug in eclipse

I m trying to debug some jsp pages in eclipse. These pages depends on some other projects, that are located in workspace. The physical files are located in webapps directory. I'm using tomcat7 as server. I have started Tomcat with standard configuration(debug at port 8000) and left also port 8000 in remote debug configuration. In my workspace there is a ic-config with WEB-INF and web.xml. It maps com.quartal.irtoolbox.ic.ControllerServlet to Controller like the following:
<servlet>
<servlet-name>Controller</servlet-name>
<servlet-class>com.quartal.irtoolbox.ic.ControllerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern>/Controller</url-pattern>
</servlet-mapping>
But when i try to access Controller , it is not accessible at all.I'm getting the error: "The requested resource (/Controller) is not available". What should i change and how should i call the servlet to be able to get and debug it?
Thanks
Magda

Categories