Why is jax-rs still scanning all my packages even I declared certain package? - jax-rs

<web-app>
<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>de.mycompany.dept.restapi</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/api/v2/*</url-pattern>
</servlet-mapping>
</web-app>
Even though I declare my url to be /api/v2/* it is still possible to connect to all endpoints without this prefix . This is really annoying. So for example endpoint /employee can be connected
as I wrote in web.xml /api/v2/employee
/employee
I do not want second option. How can I prevent it. Thanks

Maybe Different approach will work. Go to Servlet class and add "import javax.servlet.annotation.WebServlet;". Enter your url path using "#WebServlet" annotation on the class. I leave an example below.
package net.usermanagement.web;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;// Look here
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#WebServlet("foo/baz") // Look here
public class UserServlet extends HttpServlet {
.
.
.

Related

Getting 404 error on JAX-RS web service/ Maven/Eclipse/Tomcat 9

I am trying to show the Alien Object in XML format with AlienResouces as Resource.However, it is giving me 404 error during runtime on the browser and the debugging stmnt GeAlien called is not showing on Tomcat console
Web Service
package com.dip.testproj;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement
public class Alien {
private String name;
private int points;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPoints() {
return points;
}
public void setPoints(int points) {
this.points = points;
}
}
The following is the Resource for the Alien object. The object Alien is getting the resource Name and Number and trying to display it on the browser during runtime.
We Service Resources
package com.dip.testproj
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
#Path("aliens")
public class AlienResources {
#GET
#Produces(MediaType.APPLICATION_XML)
public Alien getAlien(){
System.out.println("getAlien Called");
Alien a1 = new Alien();
a1.setName("Navin");
a1.setPoints(60);
return a1;
}
}
There is no compilation error or any error on the console for Tomcat. server. However, it is giving "404" or "Resource could not be found" while trying to run the THIS on the FF browser
The URL Pattern on WEB.xml looks like this.
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>com.dip.testproj</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>

Wrong target URL

I've configured my project to load at this URL:
http://localhost:7101/foo/rs/user/home
However, when I deploy the application through JDeveloper I can see this in the Debugging: IntegratedWebLogicServer tab within the IDE:
Target Application WADL -- http://localhost:7101/foo/application.wadl
Target URL -- http://localhost:7101/foo/user/home
Note the /rs part is missing. It's mostly cosmetic (the site loads correctly in the browser when you use the correct URL) but, apart from confusing, it might be a symptom of some configuration issue. Where does JDeveloper get this URL from?
These are some of the hopefully relevant settings:
\ViewController\ViewController.jpr
<hash n="oracle.jdeveloper.model.J2eeSettings">
<value n="j2eeWebAppName" v="Foo-ViewController-webapp"/>
<value n="j2eeWebContextRoot" v="foo"/>
</hash>
ViewController\public_html\WEB-INF\web.xml
<?xml version = '1.0' encoding = 'windows-1252'?>
<web-app 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"
version="2.5" xmlns="http://java.sun.com/xml/ns/javaee">
<filter>
<filter-name>JpsFilter</filter-name>
<filter-class>oracle.security.jps.ee.http.JpsFilter</filter-class>
<init-param>
<param-name>enable.anonymous</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>JpsFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<servlet>
<servlet-name>jersey</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.example.foo.view.rest</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.mvc.templateBasePath.jsp</param-name>
<param-value>/WEB-INF/jsp</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.media.multipart.MultiPartFeature, org.glassfish.jersey.server.mvc.jsp.JspMvcFeature</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey</servlet-name>
<url-pattern>/rs/*</url-pattern>
</servlet-mapping>
<mime-mapping>
<extension>js</extension>
<mime-type>application/javascript</mime-type>
</mime-mapping>
<security-constraint>
<web-resource-collection>
<web-resource-name>Jersey resources</web-resource-name>
<url-pattern>/rs/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>valid-users</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
<security-role>
<role-name>valid-users</role-name>
</security-role>
<ejb-local-ref>
<ejb-ref-name>ejb/FooEJB</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local>com.example.foo.model.FooEJB</local>
<ejb-link>FooEJB</ejb-link>
</ejb-local-ref>
<listener>
<listener-class>com.example.foo.view.listeners.SessionListener</listener-class>
</listener>
</web-app>
com.example.foo.view.rest.UserService
package com.example.foo.view.rest;
import org.glassfish.jersey.server.mvc.Viewable;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.security.RolesAllowed;
import javax.naming.NamingException;
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;
#Path("user")
#RolesAllowed("valid-users")
public class UserService extends BaseService {
public UserService() throws NamingException {
super();
}
#GET
#Path("/home")
#Produces(MediaType.TEXT_HTML)
public Response showHome() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", "Home");
return Response.ok(new Viewable("/user/home", map)).build();
}
}
com.example.foo.view.rest.BaseService
package com.example.foo.view.rest;
import com.example.foo.commons.Constants;
import com.example.foo.model.FooEJB;
import javax.annotation.Resource;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.SecurityContext;
public class BaseService {
#Resource
protected FooEJB fooEJB;
#Context
protected HttpServletRequest request;
#Context
protected HttpServletResponse response;
#Context
protected SecurityContext securityContext;
public BaseService() throws NamingException {
InitialContext context = new InitialContext();
fooEJB = (FooEJB)context.lookup(Constants.EJB_JDNI_NAME);
}
}

Jersey 1 ContainerResponseFilter

I have a Jersey 1 ContainerResponseFilter in a shared library, which I register in Jersey 1 web services by specifying the package this filter exists in inside the web.xml eg.
<servlet>
<servlet-name>ServletAdaptor</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.xxx.utils.jersey
</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
<param-value>com.xxx.utils.jersey.CrossDomainFilter</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.xxx.utils.jersey</param-value>
</init-param>
...
</servlet>
I am now trying to use this shared library in a Jersey 2 web service which instead of using the web.xml to configure Jersey uses a class annotated with #ApplicationPath and extending the Jersey ResourceConfig. Inside this class I am not specifying the package of this Jersey 1 filter anywhere.
The problem I am getting is because this Jersey 2 web service doesn't include any Jersey 1 dependencies in the war I am getting a ClassNotFoundException during deployment of the webservice for com.sun.jersey.spi.container.ContainerResponseFilter
Here is an example of how I am creating my config
#ApplicationPath("/")
public class AppConfig extends ResourceConfig
{
private static final String RESOURCE_PACKAGE = "com.xxx.services.rest";
public AppConfig()
{
packages(RESOURCE_PACKAGE);
register(ApiListingResource.class);
register(SwaggerSerializers.class);
BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("1.0.0");
beanConfig.setResourcePackage(RESOURCE_PACKAGE);
beanConfig.setSchemes(new String[]{"http"});
beanConfig.setHost("localhost:7001");
beanConfig.setBasePath("/usr/");
beanConfig.setScan(true);
}
}
Is there a way I can make sure this Jersey 1 class is not loaded from the shared library, can I explicitly exclude the package?
EDIT:
A bit more investigation seems to imply it's something to do with CDI, the ContainerResponseFilter is annotated as a #Producer. If I remove the annotation from the class then I no longer receive the exception.
The container I am using is weblogic 12.2.1 which I believe uses Weld as its CDI provider. So I created a beans.xml with the following as indicated by this bit of Weld documentation
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:weld="http://jboss.org/schema/weld/beans">
<weld:scan>
<weld:exclude name="com.xxx.utils.jersey.**"/>
</weld:scan>
</beans>
But it doesn't seem to solve the issue. Any ideas?

How to write JAX-RS resource for consuming uploaded file

This is my class:
package pkg.rest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataParam;
#Path("/file")
public class UploadFileService {
#POST
#Path("/upload")
#Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
#FormDataParam("file") InputStream uploadedInputStream,
#FormDataParam("file") FormDataContentDisposition fileDetail) {
String uploadedFileLocation = "d://uploaded/"
+ fileDetail.getFileName();
// save it
writeToFile(uploadedInputStream, uploadedFileLocation);
String output = "File uploaded to : " + uploadedFileLocation;
return Response.status(200).entity(output).build();
}
// save uploaded file to new location
private void writeToFile(InputStream uploadedInputStream,
String uploadedFileLocation) {
try {
OutputStream out = new FileOutputStream(new File(
uploadedFileLocation));
int read = 0;
byte[] bytes = new byte[1024];
out = new FileOutputStream(new File(uploadedFileLocation));
while ((read = uploadedInputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
this is my Html File for uploading :
<html>
<body>
<h1>File Upload with Jersey</h1>
<form action="rest/file/upload" method="post" enctype="multipart/form-data">
<p>
Select a file : <input type="file" name="file" size="45" />
</p>
<input type="submit" value="Upload It" />
</form>
</body>
</html>
Web.xml file:
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>REST_WS</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>
</web-app>
when i try to upload file to server then no action perform i dint create client direct run it html file in tomcat server and try to access it please help me what we need to do change this i am writing first time so please help .
You didn't put enough stuff in web.xml, so the container does not know how to route request to your Jersey servlet.
Add the following to your web.xml:
<servlet>
<servlet-name>UploadServlet</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>pkg.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>UploadServlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
You need to have the pattern /rest/* in all the url's which you want to be handled as a service.

PrimeFaces 4.0 FileUpload works with Mojarra 2.2 but not MyFaces 2.2

I am having an interesting problem with the PrimeFaces 4.0 final FileUpload element.
I am trying to run:
PrimeFaces 4.0 final
Apache MyFaces 2.2.0-beta
Tomcat 7.0.27
I have a very simple setup right now,
XHTML page:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
<h:form>
<p:fileUpload
fileUploadListener="#{fileUploadController.handleFileUpload}"
mode="advanced" update="messages" sizeLimit="100000"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />
<p:growl id="messages" showDetail="true" />
</h:form>
</h:body>
</html>
With this backing bean:
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.FileUploadEvent;
#ManagedBean
#RequestScoped
public class FileUploadController
{
public void handleFileUpload(FileUploadEvent event)
{
FacesMessage msg = new FacesMessage("Succesful", event.getFile()
.getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
When selecting a file and uploading it, nothing happens.
The upload submit succeeds with the following response:
<?xml version="1.0" encoding="UTF-8"?><partial-response><changes><update id="j_id__v_0:javax.faces.ViewState:1"><![CDATA[2C7ZmtwSmrlbgI/wJLI2CLBaMOQP9R/pYkIXpHlXkhSKIhtfFM0sx0HmL8o9MQY2MdHXg4t1vUjJbUYkAdFBmOQUaFy7hFhPr34Za4hOuLW4CPNx]]></update></changes></partial-response>
but no message is displayed, and if I set a breakpoint, it does not get hit.
If, however, I pull out MyFaces 2.2.0-beta and put in Mojarra 2.2.0, everything works as expected.
I would prefer to continue to use MyFaces as it is what I've used in the past, so if anyone has any ideas as to a patch to get this to work, it would be much appreciated.
Thank you
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>UploadTest</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>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>*.xhtml</url-pattern>
</servlet-mapping>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<description>
This parameter tells MyFaces if javascript code should be allowed in
the rendered HTML output.
If javascript is allowed, command_link anchors will have javascript code
that submits the corresponding form.
If javascript is not allowed, the state saving info and nested parameters
will be added as url parameters.
Default is 'true'</description>
<param-name>org.apache.myfaces.ALLOW_JAVASCRIPT</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<description>
If true, rendered HTML code will be formatted, so that it is 'human-readable'
i.e. additional line separators and whitespace will be written, that do not
influence the HTML code.
Default is 'true'</description>
<param-name>org.apache.myfaces.PRETTY_HTML</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>org.apache.myfaces.DETECT_JAVASCRIPT</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<description>
If true, a javascript function will be rendered that is able to restore the
former vertical scroll on every request. Convenient feature if you have pages
with long lists and you do not want the browser page to always jump to the top
if you trigger a link or button action that stays on the same page.
Default is 'false'
</description>
<param-name>org.apache.myfaces.AUTO_SCROLL</param-name>
<param-value>true</param-value>
</context-param>
<listener>
<listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
<!-- <listener-class>com.sun.faces.config.ConfigureListener</listener-class> -->
</listener>
Update
It seems that Myfaces 2.2.0-beta has problems using the Part API present in servlet 3.x.
udaykiran pulipati has part of a solution with using web the web.xml filters that PrimeFaces 3.x required and the commons file upload & commons io jars, however, we also need to add the following context-param to the web.xml or the filters get ignored :
<context-param>
<param-name>primefaces.UPLOADER</param-name>
<param-value>commons</param-value>
</context-param>
This will force PrimeFaces to use the commons library which fixes the problem
That being said, I would still like to know why MyFaces can't seem to use the servlet Part API if anyone has any ideas. I suspect it may have to do with my Tomcat version as I am only on 7.0.27, but I doubt that.
Mention below filters in web.xml file for uploading a file using PrimeFaces
<!-- PrimeFaces FileUpload Filter -->
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
and add jars to lib folder. PrimeFaces needs below jars for fileuploading.
commons-fileupload-1.3.jar,
commons-io-2.4.jar
Recently it was found a similar issue with a better description in MYFACES-3835. It was a problem related to webkit browsers that only appears when the ajax response is large enough. It has been already fixed.
udaykiran pulipati's answer motivated me to replace commons-fileupload-1.2.2.jar with commons-fileupload-1.3.jar in my project, but that didn't solve the issue for me, as I'm using MyFaces 2.2, PrimeFaces Elite 4.0.8, and TomEE 1.6.1-snapshot.
Also, per udaykiran pulipati's answer, I already added PrimeFaces FileUpload filter config to my web.xml, many months ago.
So, I looked at PrimeFaces 4.0 user guide, and recognized something 'new' that could be specified in web.xml. So, I added the following to my web.xml,
<context-param>
<param-name>primefaces.UPLOADER</param-name>
<param-value>commons</param-value>
</context-param>
and finally, PrimeFaces (Elite) 4.0.x FileUpload works with MyFaces 2.2.