
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package es.com.blogspot.elblogdepicodev.resteasy; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.PathParam; | |
import javax.ws.rs.Produces; | |
import javax.ws.rs.core.MediaType; | |
@Path("/helloworld") | |
public interface HelloWorldResource { | |
@GET | |
@Path("/saluda") | |
public String getSaluda(); | |
@GET | |
@Path("/saluda/{nombre}") | |
public String getSaludaA(@PathParam("nombre") String nombre); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package es.com.blogspot.elblogdepicodev.resteasy; | |
import java.text.MessageFormat; | |
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.PathParam; | |
public class HelloWorldResourceImpl implements HelloWorldResource { | |
@Override | |
public String getSaluda() { | |
return "¡Hola mundo!"; | |
} | |
@Override | |
public String getSaludaA(String nombre) { | |
return MessageFormat.format("¡Hola {0}!", nombre); | |
} | |
} |
Apartir de esa interfaz podemos obtener el cliente y un programa que lo invoca con el siguiente código. La parte importante está en el uso de las clases RegisterBuiltin y ProxyFactory:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package es.com.blogspot.elblogdepicodev.resteasy; | |
import org.jboss.resteasy.client.ProxyFactory; | |
import org.jboss.resteasy.plugins.providers.RegisterBuiltin; | |
import org.jboss.resteasy.spi.ResteasyProviderFactory; | |
public class HelloWorldResourceClient implements HelloWorldResource { | |
private HelloWorldResource client; | |
public HelloWorldResourceClient() { | |
// Obtener el cliente a partir de la interfaz y de donde está localizado | |
client = ProxyFactory.create(HelloWorldResource.class, "http://localhost:8080/helloworld-resteasy/rest"); | |
} | |
@Override | |
public String getSaluda() { | |
return client.getSaluda(); | |
} | |
@Override | |
public String getSaludaA(String nombre) { | |
return client.getSaludaA(nombre); | |
} | |
public static void main(String[] args) { | |
// Inicializacion a realizar una vez por máquina virtual | |
RegisterBuiltin.register(ResteasyProviderFactory.getInstance()); | |
HelloWorldResourceClient client = new HelloWorldResourceClient(); | |
System.out.println(client.getSaluda()); | |
System.out.println(client.getSaludaA("picodotdev")); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="iso-8859-1"?> | |
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> | |
<display-name>HelloWorldRESTEasy</display-name> | |
<description>Aplicación Hola Mundo con RESTEasy</description> | |
<context-param> | |
<param-name>resteasy.servlet.mapping.prefix</param-name> | |
<param-value>/rest</param-value> | |
</context-param> | |
<servlet> | |
<servlet-name>resteasy</servlet-name> | |
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class> | |
<init-param> | |
<param-name>javax.ws.rs.Application</param-name> | |
<param-value>es.com.blogspot.elblogdepicodev.resteasy.Application</param-value> | |
</init-param> | |
<load-on-startup>1</load-on-startup> | |
</servlet> | |
<servlet> | |
<servlet-name>resteasy-jsapi</servlet-name> | |
<servlet-class>org.jboss.resteasy.jsapi.JSAPIServlet</servlet-class> | |
<load-on-startup>2</load-on-startup> | |
</servlet> | |
<servlet-mapping> | |
<servlet-name>resteasy</servlet-name> | |
<url-pattern>/rest/*</url-pattern> | |
</servlet-mapping> | |
<servlet-mapping> | |
<servlet-name>resteasy-jsapi</servlet-name> | |
<url-pattern>/rest-jsapi</url-pattern> | |
</servlet-mapping> | |
<session-config> | |
<session-timeout>30</session-timeout> | |
</session-config> | |
<welcome-file-list> | |
<welcome-file>index.jsp</welcome-file> | |
</welcome-file-list> | |
</web-app> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
java.lang.NullPointerException | |
org.jboss.resteasy.jsapi.ServiceRegistry.scanRegistry(ServiceRegistry.java:69) | |
org.jboss.resteasy.jsapi.ServiceRegistry.<init>(ServiceRegistry.java:63) | |
org.jboss.resteasy.jsapi.JSAPIServlet.scanResources(JSAPIServlet.java:75) | |
org.jboss.resteasy.jsapi.JSAPIServlet.init(JSAPIServlet.java:37) | |
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) | |
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) | |
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929) | |
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) | |
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002) | |
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585) | |
org.apache.tomcat.util.net.AprEndpoint$SocketWithOptionsProcessor.run(AprEndpoint.java:1771) | |
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110) | |
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603) | |
java.lang.Thread.run(Thread.java:679) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Ejemplo sencillo de web service con RESTEasy</title> | |
<script type="text/javascript" src="http://localhost:8080/helloworld-resteasy/rest-jsapi"></script> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
alert(HelloWorldResource.getSaluda()); | |
alert(HelloWorldResource.getSaludaA({nombre:'picodotdev'})); | |
</script> | |
</body> | |
</html> |
![]() |
Recursos que pide el cliente Javascript |
Referencia:
REST
Ejemplo sencillo de servicio web con RESTEasy
Código fuente completo del ejemplo Hola Mundo con RESTEasy