servlet - web service rest java eclipse
El método Http GET muestra un error cuando se usa jersey (5)
Estoy siguiendo un tutorial para aprender REST api. A continuación está mi código que tiene la anotación GET y usa el servidor de Weblogic para implementar mi aplicación. Por algún motivo, muestra el siguiente error:
HTTP method GET is not supported by this URL
Sample.java
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
@Path("/v1")
public class V1_status {
@GET
@Produces(MediaType.TEXT_HTML)
public String returnTitle(){
return "<p> Yess </p>";
}
}
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" 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>com.glasschecker.rest</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>Jersey REST Service</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.glasschecker.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
Este es el tutorial:
https://www.youtube.com/watch?v=4DY46f-LZ0M&list=PLu47tUtKqNlwfR- nqjiWUaIWOYEi9FyW0 & index = 2
Utilizo la siguiente URL http: // localhost: 7001 / com.glasschecker.rest / api / v1. El patrón de URL se agrega al servlet.
Cuando intento acceder a http: // localhost: 7001 / com.glasschecker.rest , muestra el archivo index.html correctamente (primer archivo en mi web.xml). Estoy seguro de algo en mi archivo.
NOTA la clase java está dentro de este paquete:
com.glasschecker.rest.status
Este es el error que estoy recibiendo:
weblogic.application.ModuleException: weblogic.management.DeploymentException: [HTTP:101170]The servlet org.foo.rest.MyApplication is referenced in servlet-mapping /api but not defined in web.xml.
at weblogic.application.internal.ExtensibleModuleWrapper.start(ExtensibleModuleWrapper.java:140)
at weblogic.application.internal.flow.ModuleListenerInvoker.start(ModuleListenerInvoker.java:124)
at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:237)
at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:232)
at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:45)
Truncated. see log file for complete stacktrace
Caused By: weblogic.management.DeploymentException: [HTTP:101170]The servlet org.foo.rest.MyApplication is referenced in servlet-mapping /api but not defined in web.xml.
at weblogic.servlet.internal.WebAppServletContext.verifyServletMappings(WebAppServletContext.java:1566)
at weblogic.servlet.internal.WebAppServletContext.start(WebAppServletContext.java:3066)
at weblogic.servlet.internal.WebAppModule.startContexts(WebAppModule.java:1830)
at weblogic.servlet.internal.WebAppModule.start(WebAppModule.java:875)
at weblogic.application.internal.ExtensibleModuleWrapper$StartStateChange.next(ExtensibleModuleWrapper.java:360)
Anote su clase con la anotación @Path, ya que JAX-RS runtime lo requiere para tratar su clase como un recurso.
Usted definió la ruta /v1 pero solicitó /api/v1 . tratar:
@Path("/api")
public class V1_status {
@Path("v1")
@GET
@Produces(MediaType.TEXT_HTML)
public String returnTitle(){
return "<p> Yess </p>";
}
}
Aquí está el código de trabajo completo:
@Path("/ws")
public class V1_status {
@GET
@Path("/title") //this one is optional
@Produces(MediaType.TEXT_HTML)
public String returnTitle(){
return "<p> Yess </p>";
}
}
- use anotation @Path ("path_name_here") para declarar una ruta de reposo a un recurso.
- acceda a su punto final en el uri: http: // localhost: 8080 / ws / title (suponiendo que la ruta del servidor base es: http: // localhost: 8080 )
- compruebe la instalación weblogic jax -rs-2.0.war
- compruebe que su clase de recurso de servicio web de Rest esté en paquete (declarado con
com.sun.jersey.config.property.package)
La propiedad "com.sun.jersey.config.property.package" solo necesita establecerse como el paquete que contiene las clases de servicio web.
En su caso es el paquete de clase "V1_status": "com.glasschecker.rest.status"
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.glasschecker.rest.status</param-value>
</init-param>
Como se especifica en la documentación de Jersey ServletContainer
if the initialization parameter "com.sun.jersey.config.property.resourceConfigClass" is not present and a initialization parameter "com.sun.jersey.config.property.packages" is present a new instance of PackagesResourceConfig is created. The initialization parameter "com.sun.jersey.config.property.packages" MUST be set to provide one or more package names.
PackagesResourceConfig busca dinámicamente las clases de recurso raíz y proveedor. En este caso, "V1_status" es su recurso raíz.
- Elimine el archivo
web.xmlo borre todos los elementos del mapeo servlet y servlet. - Debería haber una clase
ApplicationConfigque extiende laApplication. AnotaApplicationPath(''/'')o como quieras.
Aquí hay un ejemplo de la clase ApplicationConfig
@javax.ws.rs.ApplicationPath("/")
public class ApplicationConfig extends Application {
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new java.util.HashSet<>();
addRestResourceClasses(resources);
return resources;
}
/**
* Do not modify addRestResourceClasses() method.
* It is automatically populated with
* all resources defined in the project.
* If required, comment out calling this method in getClasses().
*/
private void addRestResourceClasses(Set<Class<?>> resources) {
resources.add(com.testrest.StuffResource.class);
}
}