How can I access a JAX-RS resource? - jax-rs

I've created a simple JAX-RS resource class for my Java EE based project. I am trying to get a response back from the server but when I access my url I get an error back. This is the class that I wrote:
#Path("/rest")
public class RestResource {
#GET
#Produces(MediaType.APPLICATION_JSON)
public String create(String name)
{
return "print my string";
}
}
I cannot figure out what I'm doing wrong.
Do I have to define a new servlet in my web.xml? Is there something wrong with my current one?
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
metadata-complete="false">
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<display-name>crud-app</display-name>
<!-- Activate the JSF 2.0 servlet -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Tell the context which URLs to send through JSF -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<!-- This is an optional parameter, but it makes troubleshooting errors
much easier -->
<!-- You are advised to remove this context parameter before a production
deployment -->
<context-param>
<param-name>facelets.DEVELOPMENT</param-name>
<param-value>true</param-value>
</context-param>
<!-- Welcome page -->
<welcome-file-list>
<welcome-file>person.jsf</welcome-file>
</welcome-file-list>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/ui/error/error.jsf</location>
</error-page>
<error-page>
<exception-type>javax.faces.application.ViewExpiredException</exception-type>
<location>/ui/error/viewExpired.jsf</location>
</error-page>
</web-app>

I managed to fix my problem by using RESTEasy. Here is my updated web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
metadata-complete="false">
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<display-name>crud-app</display-name>
<!-- Activate the JSF 2.0 servlet -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Tell the context which URLs to send through JSF -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<!-- Auto scan REST service -->
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
<!-- this need same with resteasy servlet url-pattern -->
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/rest</param-value>
</context-param>
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<!-- This is an optional parameter, but it makes troubleshooting errors
much easier -->
<!-- You are advised to remove this context parameter before a production
deployment -->
<context-param>
<param-name>facelets.DEVELOPMENT</param-name>
<param-value>true</param-value>
</context-param>
<!-- Welcome page -->
<welcome-file-list>
<welcome-file>person.jsf</welcome-file>
</welcome-file-list>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/ui/error/error.jsf</location>
</error-page>
<error-page>
<exception-type>javax.faces.application.ViewExpiredException</exception-type>
<location>/ui/error/viewExpired.jsf</location>
</error-page>
</web-app>

Related

jsf web.xml to java config

I wonder what is the equivalent of the following code ( web.xml) in Java.
.........................................................................
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<!-- Welcome page -->
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
<!-- JSF mapping -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map these files with JSF -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
And xml(extensible markup language) file is just a way to store and transport data. There is no equivalent java class nor instance.
To learn more about how this data is used check: java servlets

Primefaces fileupload uploads only files < 30kb

I'm having problems with my chat project, which i'm building for learning purposes. I'm having a weird little problem with PrimeFaces fileUpload component. It is working fine for a file not larger than 30kb (for larger file it throws HTTP error 400).I'm using PrimeFaces 5.3, Apache commons-io 2.3, Commons-fileupload 1.3. Because this is my learning project, i have uploaded it on public git repository, which is, for better understanding of the problem, accessible here: https://bitbucket.org/salkovich123/testfileupload.git
The fileUpload component is located in file register.xhtml.
I am also sharing my web.xml file here:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>primefaces.UPLOADER</param-name>
<param-value>auto</param-value>
</context-param>
<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>
<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>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>faces/index.xhtml</welcome-file>
</welcome-file-list>
This are the header details, which throws HTTP error 400 (bad request)
UPDATE #1
This is most definetly some kind of jQuery problem, because error is thrown in browser console, and no other server side methods are called after thrown error

i try to write login page in Osgi-karaf with ldap but i got this error "Can't connect to the LDAP server: [LDAP: error code 49 - Invalid Credentials]"

i want to write login page in Osgi-karaf.
My login.xhtml is ;
<?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:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>JSF 2.0 Hello World_denememememe</title>
</h:head>
<h:body>
<h3>JSF 2.0 Hello World Example - login.xhtml</h3>
<h:form id="formLogin" prependId="false"
onsubmit="document.getElementById('formLogin').action = 'j_security_check';">
<h:panelGrid columns="2">
<f:facet name="header">
Login
</f:facet>
<h:outputLabel for="j_username" value="Username: "/>
<h:inputText id="j_username" required="true" value="#{helloBean.name}" />
<h:outputLabel for="j_password" value="Password: "/>
<h:inputSecret id="j_password" required="true" value="#{helloBean.pass}" />
<f:facet name="footer">
<h:commandButton value="Login" action="#{helloBean.birlestir}"/>
</f:facet>
</h:panelGrid>
</h:form>
</h:body>
</html>
and my web.xml is ;
<?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_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>xhtmlDeneme</display-name>
<!-- Change to "Production" when you are ready to deploy -->
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<!-- Welcome page -->
<welcome-file-list>
<welcome-file>faces/login.xhtml</welcome-file>
</welcome-file-list>
<!-- JSF mapping -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map these files with JSF -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<login-config>
<auth-method>FORM</auth-method>
<realm-name>karaf</realm-name>
<form-login-config>
<form-login-page>/faces/login.xhtml</form-login-page>
<form-error-page>/faces/login.xhtml</form-error-page>
</form-login-config>
</login-config>
I'm using karaf. I know when karaf is seen "j_security_check", it look "ldap-module.xml". And here is my ldap-module.xml ;
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:jaas="http://karaf.apache.org/xmlns/jaas/v1.0.0"
xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0">
<jaas:config name="karaf" rank="1">
<jaas:module className="org.apache.karaf.jaas.modules.ldap.LDAPLoginModule"
flags="required">
initialContextFactory=com.sun.jndi.ldap.LdapCtxFactory
connection.username=--valid username--
connection.password=--valid password--
connection.protocol=
connection.url=--valid url--
user.base.dn=cn=Users,dc=vmldapdevelop,dc=com
user.filter=(cn=%u)
user.search.subtree=true
role.base.dn=cn=Groups,dc=vmldapdevelop,dc=com
role.name.attribute=cn
role.filter=(uniquemember=%nsdn)
role.search.subtree=true
authentication=simple
</jaas:module>
</jaas:config>
</blueprint>
I try everything almost, but i don't change this error! When i delete from login.xhtml "j_security_check" part, its working.
Anybody help me?

How can i remove uploaded image to replace with another image in primefaces <p:fileUpload>?

I am just a beginner in primefaces and I just want to know how I can remove the uploaded file. Another one is that within <p:fileUpload> tag, can I show the uploaded file for update or remove? Is this supported by <p:fileUpload>? My uploaded file is not yet on the disk or in the database but still is an object. My code is as below. Please help me or give me some idea.
This is fileupload.xhtml
<h:outputLabel value="Choose Image:" />
<p:fileUpload fileUploadListener="#{ManageProductCode.handleFileUpload}" mode= "advanced"
sizeLimit="3000000" allowTypes="/(\.|\/)(gif|jpe?g|png)$/" id="MyAttachment" fileLimit="1"
required="true" requiredMessage="Image Is Require."/>
<br/>
This is my backing bean. That is just assigning value to persist into database.
private UploadedFile uploadFile;
public UploadedFile getUploadFile() {
return uploadFile;
}
public void setUploadFile(UploadedFile uploadFile) {
this.uploadFile = uploadFile;
}
public void handleFileUpload(FileUploadEvent event){
uploadFile = event.getFile();
System.out.println("UploadFileName is" + uploadFile.getFileName());
byte[]image = event.getFile().getContents();
productCode.setImage(image);
}
This is my web.xml
<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>
<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>server</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<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>

Update items for selectOneMenu

I'm trying to get the items for a selectOneMenu updated after a file has bee uploaded. To do it, I load the select items at the file upload listener and set the id for the selectOneMenu in the update attribute for the fileUpload component, but the file upload listener is not even called.
I've reproduced the error in a small project I uploaded here http://www.filedropper.com/file-upload
I'm working with PrimeFaces 3.5 on a JBoss AS7.1.3
I can't find what I'm missing. Any ideas?
Thanks in advance
This is the resulting web.xml, including the missing faces servlet declaration and mapping that solved my problem.
<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">
<description>File Upload</description>
<!-- This was missing -->
<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>*.jsf</url-pattern>
</servlet-mapping>
<!-- File upload -->
<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>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>glass-x</param-value>
</context-param>
</web-app>