GWT RPC Service not found - gwt

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'>

Related

Java EE Servlet and REST path clashing

I am trying to write a Java web application that provides both HTML and REST interface. I would like to create a servlet that would provide the HTML interface using JSP, but data should also be accessible via REST.
What I already have is something like this for the REST:
#javax.ws.rs.Path("/api/")
public class RestAPI {
... // Some methods
}
and
#WebServlet("/servlet")
public class MyServlet extends HttpServlet {
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("Howdy at ");
}
}
Now, when I change the #WebServlet("/servlet") annotation to #WebServlet("/"), the servlet stops working probably due to path clash with the REST.
How can I provide REST on specific path and HTML in the root?
Thank you,
Lukas Jendele
This seems to work OK for me. What I did:
In my pom.xml, I have a dependency on org.wildfly.swarm:undertow (for Servlet API) and org.wildfly.swarm:jaxrs (for JAX-RS). And of course the Swarm Maven plugin.
For servlet, I have just this one class:
#WebServlet("/")
public class HelloServlet extends HttpServlet {
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().println("Hello from servlet");
}
}
For JAX-RS, I have these 2 classes:
#ApplicationPath("/api")
public class RestApplication extends Application {
}
#Path("/")
public class HelloResource {
#GET
public Response get() {
return Response.ok().entity("Hello from API").build();
}
}
To test, I run curl http://localhost:8080/ and curl http://localhost:8080/api. Results are as expected. (Maybe my example is too simple?)

Swagger API documentation could not be read due to filters

I am having the JAXRS application, where I want to expose the REST API via Swagger.
The configuration is the same as for the sample (Swagger JAX RS sample).
It had worked fine until I added several filters (javax.servlet.Filter and javax.ws.rs.container.ContainerRequestFilter) required for my application.
Filters work on /api/* path, and Swagger reads the documentation from /api/api-docs path.
Is it somehow possible to avoid filters for Swagger?
Found the solution. In web.xml I added the filter which handles the /api/api-docs/* request.
It is set before filters which handle /api/* requests.
<filter>
<filter-name>SwaggerDocRequestFilter</filter-name>
<filter-class>my.package.filters.SwaggerDocRequestFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SwaggerDocRequestFilter</filter-name>
<url-pattern>/api/api-docs/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
SwaggerDocRequestFilter looks like this:
public class SwaggerDocRequestFilter implements Filter {
#Override
public void init(FilterConfig fc) throws ServletException {
}
#Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
String path = ((HttpServletRequest) request).getRequestURI();
if (path.contains("/api-docs")) {
String forwardPath = ((HttpServletRequest) request).getServletPath() +
StringUtils.defaultString(((HttpServletRequest)request).getPathInfo());
request.getRequestDispatcher(forwardPath).
forward(request, response);
} else {
chain.doFilter(request, response);
}
}
#Override
public void destroy() {}
}
This overrides all javax.servlet.Filter implementations.
For overriding JAX RS filters, it is required to provide own implementation of javax.ws.rs.container.DynamicFeature interface. This implementation applies filters only when requests to specific resources occurrs, in my case for resources in a specific package.
public class MyDynamicFiltersRegister implements DynamicFeature {
#Override
public void configure(final ResourceInfo resourceInfo, final FeatureContext context) {
if (resourceInfo.getResourceClass().getPackage().getName().equals("my.package.resources)) {
//register filters on FeatureContext objec only if resource in specific package.
...
}
}
}
In this case, Swagger UI works fine.
On requests to specific resources, JAX RS filters are applied.

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.

Error 404 during GWT RPC jetty server

I just written simple RPC call, when i tried i get the below error, could you please help me out to fix this..
[WARN] 404 - POST /com.sribalajiele.gwt.client.SriBalajiEle/emailRpcService (127.0.0.1)
Email Failure404
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<title>Error 404 NOT_FOUND</title>
The code like below.
/*
* Copyright (c) Balaji electricals AG 2011, All Rights Reserved
*/
package com.sribalajiele.gwt.client.client;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
/**
* #author kdel.
* This interface provides Email Service.
*
*/
#RemoteServiceRelativePath("emailRpcService")
public interface EmailRpcService extends RemoteService {
public WriteToUsForm sendEmail(WriteToUsForm writeToUsForm) throws IllegalArgumentException;
}
/*
* Copyright (c) Balaji electricals 2011, All Rights Reserved
*/
package com.sribalajiele.gwt.client.client;
import com.google.gwt.user.client.rpc.AsyncCallback;
/**
* #author kdel
* Async service for Email.
*/
public interface EmailRpcServiceAsync {
void sendEmail(WriteToUsForm writeToUsForm, AsyncCallback<WriteToUsForm> asyncCallback)
throws IllegalArgumentException;
}
public final class EmailRpcServiceImpl extends RemoteServiceServlet implements EmailRpcService {
/**
* Default serialVersionUID.
*/
private static final long serialVersionUID = 1L;
#Override
public WriteToUsForm sendEmail(WriteToUsForm writeToUsForm) throws IllegalArgumentException {
System.out.println("send Email called");
}
}
In web.xml:
<servlet>
<servlet-name>emailService</servlet-name>
<servlet-class>com.sribalajiele.gwt.client.server.EmailRpcServiceImpl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>emailService</servlet-name>
<url-pattern>sriBalajiEle/emailRpcService</url-pattern>
</servlet-mapping>
Finally i could correct my self, may be this is use full for others.
1) #RemoteServiceRelativePath("emailRpcService")
public interface EmailRpcService extends RemoteService {
2) In *Module*.gwt.xml
<servlet class="com.sribalajiele.ui.server.EmailRpcServiceImpl" path="/emailRpcService"/>
3) Register your servlet in web.xml
<servlet>
<servlet-name>eamilService</servlet-name>
<servlet-class>com.sribalajiele.ui.server.EmailRpcServiceImpl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>eamilService</servlet-name>
<url-pattern>/com.sribalajiele.ui.SriBalaji/emailRpcService</url-pattern>
</servlet-mapping>
4) Usage:
final EmailRpcServiceAsync emailRpcServiceAsync = (EmailRpcServiceAsync) GWT.create(EmailRpcService.class);
ServiceDefTarget serviceDef = (ServiceDefTarget) emailRpcServiceAsync;
serviceDef.setServiceEntryPoint(GWT.getModuleBaseURL() + "emailRpcService");
emailRpcServiceAsync.sendEmail(parameter, new AsyncCall()) {
onSuccess() { }
onFailure() { }
}
Hope this will help...
The problem is that you have the servlet mapped to /sriBalajiEle/emailRpcService, but the request is being sent to /com.sribalajiele.gwt.client.SriBalajiEle/emailRpcService. The URL that the request is being sent to is generated by GWT in the form /${moduleName}/relativePath. If you include the following at the top of your GWT module, it should fix the 404.
<module rename-to="sriBalajiEle">
1) Include annotatation in your interface too.
#RemoteServiceRelativePath("emailRpcService")
public interface EmailRpcServiceAsync {
void sendEmail(WriteToUsForm writeToUsForm,
AsyncCallback asyncCallback)
throws IllegalArgumentException;
}
2) And change your url mapping to the following.
<url-pattern>com.sribalajiele.gwt.EmailRpcService/emailRpcService</url-pattern>
For my case, url mapping gave me headache for hours. Hope this helps.
the 404 error will site a url, I had to make sure the url sited in the 404 message was the url in my web.xml
<servlet-mapping>
<servlet-name>messageServiceImpl</servlet-name>
<url-pattern>/com.mbe.site.main/message</url-pattern>
</servlet-mapping>

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

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.