我正在做一个 REST 应用程序。我做了 GET
方法没有问题,但是,当我实现 POST
时方法,它说我没有 OPTIONS
为它实现的方法。我在做OPTIONS
URI的方法:
http://192.168.1.26:8080/sellAppWeb/api/object/
我有 POST
和 OPTIONS
方法:
@OPTIONS
@Produces("application/json; charset=UTF-8")
public Response options() {
return Response.ok().build();
}
@Override
@POST
public Response save(CervejaDTO cervejaDTO) {
cervejaController.register(cervejaDTO);
return Response.ok(cervejaDTO).build();
}
然后我就成了
DELETE
方法,它再次说我没有 OPTIONS
方法。然后我需要再做一个 OPTIONS
方法,它有一个 ID
在 URI 端。例如删除一个对象 id = 3
:http://192.168.1.26:8080/sellAppWeb/api/object/3
我需要另一个
OPTIONS
与 DELETE
的结构相同网址:@OPTIONS
@Path("/{id}")
@Produces("application/json; charset=UTF-8")
public Response optionsDelete(@PathParam("id") Integer id) {
return Response.ok().build();
}
@Override
@POST
public Response save(CervejaDTO cervejaDTO) {
cervejaController.register(cervejaDTO);
return Response.ok(cervejaDTO).build();
}
有没有人有办法做一个通用的
OPTIONS
对于所有 REST 请求?web.xml:
<display-name>Testes de serviços REST</display-name>
<description>Testes de serviços REST</description>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>resteasy.servlet.mapping.prefix</param-name>
<param-value>/api</param-value>
</context-param>
<context-param>
<param-name>resteasy.providers</param-name>
<param-value>br.com.sell.app.exception.handler.DefaultExceptionHandler</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>/api/*</url-pattern>
</servlet-mapping>
最佳答案
您不需要实现 OPTIONS
在这种情况下是 HTTP 动词。由于您使用的是 RESTEasy,它是 Wildfly 使用的 JAX-RS 实现,我遇到的问题是由于 web.xml
上的 servlet 映射引起的。 .
我在 Eclipse 上添加 JAX-RS 方面并告诉它更新 web.xml
时遇到了这个问题。 .默认生成web.xml
包含 Restful 应用程序映射不会将您的应用程序正确映射到您的 RESTful 资源路径。
如果您还没有创建自己的自定义 Application
,这就是 web.xml 的样子。 .
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>My REST API</display-name>
<description>My REST API</description>
<servlet>
<description>JAX-RS Tools Generated - Do not modify</description>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>/jaxrs/*</url-pattern>
</servlet-mapping>
</web-app>
确保您的
<servlet-name>
和 <servlet-mapping>
如上例所示进行映射。如果您扩展了 Application
类,只需在您的 web.xml
中指定它而不是默认 Application
如上图所示。另外,您的
@POST
resource 方法,建议使用 @Consumes
指定 RESTful 数据的资源类型(在您的情况下,您的 DTO)注解。例如。
@POST
@Path("/save")
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response save(CervejaDTO cervejaDTO)
}
关于java - 在 REST 服务中实现方法 OPTIONS 的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32356151/