Guice and GWT problem - can't find GWT.rpc - gwt

I build a simple contact manager app with simple service and it did work.
Then I decided I want to use Guice for managing my services and implementations.
I also use mvp4g plugin for MVP design pattern.
I followed the exmaple of Eric Burke on his blog, and my code looks like that:
ContactService.java
#RemoteServiceRelativePath("GWT.rpc")
public interface ContactService extends RemoteService {
public void saveContact(Contact c);
public List<Contact> listContacts();
}
ContactServletModule.java:
#Singleton
public class ContactServletModule extends ServletModule{
private static String SQL_MAP_CONFIG = "org/yuri/SqlMapConfig.xml";
private SqlSessionFactory factory = null;
#Provides
public SqlSessionFactory getSqlSessionFactory(){
if(this.factory == null){
try {
/*
* Create new factory
*/
Reader r = Resources.getResourceAsReader(SQL_MAP_CONFIG);
this.factory = new SqlSessionFactoryBuilder().build(r);
} catch (IOException ex) {
/*
* do nothing, factory is null still
*/
} finally{
return this.factory;
}
}
else{
return this.factory;
}
}
#Override
protected void configureServlets() {
serve("/YuriContactManager/GWT.rpc").with(GuiceRemoteServiceServlet.class);
bind(ContactService.class).to(ContactServiceImpl.class);
}
}
MyGuiceContextListener.java
public class MyGuiceContextListener extends GuiceServletContextListener {
#Override
protected Injector getInjector() {
return Guice.createInjector(new ContactServletModule());
}
}
But when when I start my app and try to list contacts by calling listContacts(), tomcat tells me that GWT RPC can't be found (exactly: The requested resource (/YuriContactManager/org.yuri.ContactManager/GWT.rpc) is not available.) My web.xml looks like that:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<filter>
<filter-name>guiceFilter</filter-name>
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>guiceFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.yuri.server.MyGuiceContextListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>welcomeGWT.html</welcome-file>
</welcome-file-list>
</web-app>
Any one had similar problem or has any idea what might be wrong?

found the error :) In ContactServletModule serve path needs to be modified to "/org.yuri.YuriContactManager/GWT.rpc" - I think the reason why is that I'm also using mvp4g framework, but I'm not sure.

Related

#EJB annotation not working for REST API class file

#EJB not working, returning null pointer exception when used. I am using jersey. It is working in the similar project of jersey earlier.
Rest API class
#Path("")
public class Services
{
#EJB
UserSessionBeanLocal userBean;
#POST
#Produces(MediaType.APPLICATION_JSON)
#Path("register")
public Response register(#FormParam("userName") final String name, #FormParam("userPassword") final String pass, #FormParam("userEmail") String email)
{
if (userBean.isEmailRegistered(email)) // userBean is null here
{
// code
}
}
}
Stateless EJB
#Stateless
public class UserSessionBean implements UserSessionBeanLocal
{
static ArrayList<User> usersList = new ArrayList<User>();
static int userCount = 0;
User u = null;
#Override
public boolean isEmailRegistered(final String email)
{
return usersList.stream().anyMatch(d -> d.getEmail().equals(email));
}
}
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.enovate.assignment.ejb1</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/user/*</url-pattern>
</servlet-mapping>
</web-app>
Any help is appreciated. Thanks in advance.
I was expecting it should work fine. Now I have to use the JNDI lookup way to instantiate the bean.
As I mentioned it is working in almost similar project. I found something that #EJB does not work in non-managed bean if is related, but I don't understand it.

MessageBodyWriter not found for media type={application/xml, q=1000} - Jersey + Jaxb

I am writing a RESTful web service with Jersey. I want to return a custom object in XML form to consumer. The error I am getting is:
MessageBodyWriter not found for media type={application/xml, q=1000}, type=class com.test.ws.Employee, genericType=class com.test.ws.Employee.
Below is the code:
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>com.vogella.jersey.first</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<!-- Register resources and providers under com.vogella.jersey.first package. -->
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.test.ws</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>
Service Class
package com.test.ws;
#Path("/hello")
public class Hello {
#GET
#Path("/sayHello")
#Produces(MediaType.APPLICATION_XML)
public Employee sayHello() {
Employee employee = new Employee();
employee.setEmpId(1);
employee.setFirstName("Aniket");
employee.setLastName("Khadke");
return employee;
}
}
Employee.java
package com.test.ws;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name = "employee")
public class Employee {
public String firstName;
public String lastName;
public int empId;
public Employee(String firstName, String lastName, int empId) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.empId = empId;
}
public Employee() {
super();
}
#XmlElement
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
#XmlElement
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
#XmlElement
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
}
And here is the list of libraries added:
Can anyone help me?
I believe your error is in the web.xml. Try changing your part to this in your web.xml.
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<!-- Register resources and providers under com.vogella.jersey.first package. -->
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.test.ws</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
One way to solve your issue is to create a custom javax.ws.rs.core.Application or org.glassfish.jersey.server.ResourceConfig. It seems that your server does not detect your providers for the serialization. By implementing your own Application, you will be able to specify which provider you want to use. for your example, what you could have done is :
MyApplication.java
package com.test.ws;
public class MyApplication extends ResourceConfig {
public MyApplication() {
//register your resources
packages("com.test.ws");
//if you're using Jackson as your XMLProvider for example
register(JacksonJaxbXMLProvider.class);
}
}
And add the application in your deployment file :
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>com.vogella.jersey.first</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.test.ws.MyApplication</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>
Employee class should implements Serializable interface
I was able to resolve the issue myself. This was because of having conflicting jars included in build path. Here is the snap for jar files.
I manage a legacy project wich I needed to add a REST web service. This not have Maven.
For jersey 2.25, the last compiled with Java SDK 1.7, I solved adding jar
jersey-media-jaxb-2.25.jar

404 error in JAXRS service using jersey and tomcat

Coding a JAXRS service using jersey and deployed on tomcat. Application subclass
#ApplicationPath("/rest")
public class RestApplication extends Application {
public RestApplication() {
// TODO Auto-generated constructor stub
System.out.println("Inside RestApplication Constructor");
}
#Override
public Set<Class<?>> getClasses() {
// TODO Auto-generated method stub
System.out.println("Get Class");
Set<Class<?>> s=new HashSet<Class<?>>();
s.add(SupportDataService.class);
return s;
}
}
Resource class
#Path("/supportdata")
public class SupportDataService {
public SupportDataService() {
// TODO Auto-generated constructor stub
System.out.println("Inside SupportDataService Constructor");
}
#Path("/support")
#GET
#Produces(MediaType.TEXT_XML)
public String getSupportData(){
String xmlSupport=null;
xmlSupport="<SupportData><Support><key>path1</key><value>value1</value></Support><Support><key>path2</key><value>value2</value></Support></SupportData>";
return xmlSupport;
}
}
Added all jersey jar in WEB-INF/lib except javax.servlet-api-3.0.1.jar and hitting url
http://localhost:8080/RestConfigurator/rest/supportdata/support
but getting 404 error. Not specified any web.xml as subclassed Application.
So with Tomcat 6, which is a Servlet 2.5 implementation, we are required to have a web.xml declaring the ServletContainer. You can see more about it here. Basically, if you want to keep your RestApplication class, the web.xml would look something like
<?xml version="1.0" encoding="UTF-8"?>
<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>MyApplication</servlet-name>
<servlet-class>
org.glassfish.jersey.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>thepackge.of.RestApplication</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>MyApplication</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
Given you have added all the jars from Jersey JAX-RS 2.0 RI bundle (besides the servlet-api), this should work. I've tested with Maven on Tomcat 6 and it works fine. Maven just pulls in all most of the jars from that bundle. But the app deployment doesn't differ.

Restful service deployed on Jboss 7.1 always return 404

I have a problem deploying a RESTful web application (JAX-RS) on JBoss 7.1
This is the web.xml
<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>HEODWS</display-name>
<servlet>
<servlet-name>Resteasy</servlet-name>
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Resteasy</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>it.heod.ws.WSApplication</param-value>
</context-param>
<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>
While the class implementing the web service is:
#Path("/")
public class LoginService {
public LoginService() {
}
#GET
#Path("helloworld")
#Produces(MediaType.TEXT_PLAIN)
public Response helloWorld() {
Utils utils = Utils.getInstance();
utils.logExecutingMethod();
ResponseBuilder responseBuilder = null;
Response response = null;
responseBuilder = Response.ok();
responseBuilder.entity("Hello, world!");
response = utils.completeResponse(responseBuilder);
return (response);
}
}
The class WSApplication is:
public class WSApplication extends Application {
private Set<Object> singletons = new HashSet<Object>();
private Set<Class<?>> empty = new HashSet<Class<?>>();
public WSApplication(){
singletons.add(new LoginService());
}
#Override
public Set<Class<?>> getClasses() {
return empty;
}
#Override
public Set<Object> getSingletons() {
return singletons;
}
}
Now, if I deploy the WAR file on my local copy of JBoss 7.1 and I go to
http://localhost:8080/HEODWS/helloworld
the service behaves correctly and I get the desired response, while if I deploy it on another server, running JBoss 7.1, and I go to
http://anotherhost:8080/HEODWS/helloworld
I get a 404 not found.
Can anybody understand why, i.e. what is the difference between the two servers? Maybe I have configured (in the past) my local server in such a way that I can't recall now?
Thanks a lot in advance,
Gianluca
JBoss AS 7.1 provides you with Java EE 6 support, so you don't need to use the servlet dispatcher provided by RESTEasy (it's only necessary if you deploy on Tomcat or Jetty).
Then, you can remove the content from web.xml and declare your JAX-RS Activator in a pure Java form like this:
#ApplicationPath("/")
public class WSApplication extends Application {
private Set<Object> singletons = new HashSet<Object>();
private Set<Class<?>> empty = new HashSet<Class<?>>();
public WSApplication(){
singletons.add(new LoginService());
}
#Override
public Set<Class<?>> getClasses() {
return empty;
}
#Override
public Set<Object> getSingletons() {
return singletons;
}
}
You can even remove all the methods and fields in your WSApplication class (ie, just have an empty subclass of javax.ws.rs.core.Application) and annotate your LoginService class with #RequestScoped (or #Stateless).
HTH.
Xavier
I actually didn't know what happened, but by copying and pasting all the classes and the web.xml in a new project and deploying, it worked. I suppose it was just Eclipse that went crazy. Thanks everyone for the answers.

GWT RPC Service not found

I have searched the Web almost for hours, but I didn't find an answer.
The Problem is that i want to the test the gwt RPC.
So I generate with the Eclipse Plugin a GWT remote Service.
But everytime I get the following Failure: "[WARN] No file found for: /kuss_projekt/SpeicherService"
I have tryed a lot, but I dont knwo what is the Problem.
Thats my Code:
web.xml:
<web-app>
<servlet>
<servlet-name>SpeicherService</servlet-name>
<servlet-class>de.fhdo.kuss.server.SpeicherServiceImpl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SpeicherService</servlet-name>
<url-pattern>/kuss_projekt/SpeicherService</url-pattern>
</servlet-mapping>
<!-- Default page to serve -->
<welcome-file-list>
<welcome-file>Kuss_Projekt.html</welcome-file>
</welcome-file-list>
</web-app>
-
Speicherservice:
#RemoteServiceRelativePath("SpeicherService")
public interface SpeicherService extends RemoteService {
String getName(String name);
public static class Util {
private static SpeicherServiceAsync instance;
public static SpeicherServiceAsync getInstance(){
if (instance == null) {
instance = GWT.create(SpeicherService.class);
}
return instance;
}
}
}
-
SpeicherServiceAsync:
public interface SpeicherServiceAsync {
void getName(String name, AsyncCallback<String> callback);
}
-
SpeicherServiceImpl
public class SpeicherServiceImpl extends RemoteServiceServlet implements SpeicherService {
#Override
public String getName(String name) {
return("Server meldet sich " + name);
}
}
-
Test():
public void test() {
AsyncCallback<String> callback = new AsyncCallback<String>() {
#Override
public void onFailure(Throwable caught) {
// TODO Auto-generated method stub
}
#Override
public void onSuccess(String result) {
Window.alert(result);
}
};
SpeicherService.Util.getInstance().getName("test",callback);
}
Have you tried removing /kuss_projekt from servlet mapping to make it:
<servlet-mapping>
<servlet-name>SpeicherService</servlet-name>
<url-pattern>/SpeicherService</url-pattern>
</servlet-mapping>
GWT client is expecting the service to be available at the URL defined via #RemoteServiceRelativePath. When you are running in browser, the path is resolved relative to your module base url. As you have given:
#RemoteServiceRelativePath("SpeicherService")
the client will make request to the URL made by concatenating
GWT.getModuleBaseURL() + "SpeicherService"
If your servlet is not mapped at this url, the request will fail. Try printing GWT.getModuleBaseURL()+ "SpeicherService" on console to see what is the base url in your test case. Once you have got this, open the browser and go to that url. If the response says something like "Get method is not supported" everything is mapped correctly. On the other hand if you get a 404 you got to fix your servlet mapping
Does your application xml file contain
<module rename-to='kuss_projekt'>