等同于 Jersey 中的 Spring MVC @ResponseStatus(HttpStatus.CREATED)?
Equivalent of Spring MVC @ResponseStatus(HttpStatus.CREATED) in Jersey?
这个 Spring MVC 代码的 Jersey 等价物是什么?在成功 POST:
之后,我需要对 return 201 的响应以及资源 URL
@RequestMapping(method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
Widget create(@RequestBody @Valid Widget wid) {
return service.create(wid);
}
这是我在泽西岛找到的最短的例子。是否需要手动构建响应才能成功 POST/201?
@POST @Path("widget")
Response create(@RequestBody @Valid Widget wid) {
return Response
.status(Response.Status.CREATED)
.entity("new widget created")
.header("Location","http://localhost:7001/widget"+wid)
.build();
}
我认为 Jersey 中没有这样的注释。您可以使用 Name Binding.
创建一个
基本上,您创建一个注释并添加 @NameBinding
元注释:
@NameBinding
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ResponseStatusCreated {}
接下来创建一个将覆盖状态的过滤器。
@ResponseStatusCreated
@Provider
class StatusCreatedFilter implements ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext requestContext,
ContainerResponseContext responseContext) throws IOException {
responseContext.setStatusInfo(Response.Status.CREATED)
String location = "..."; // set based on responseContext.getEntity()
// or any other properties
responseContext.getHeaders().putSingle("Location", location);
}
}
然后在您的资源方法上使用相同的注释。
@POST
@Path("widget")
@ResponseStatusCreated
Object create(@RequestBody @Valid Widget wid) {
return ... // return whatever you need to build the
// correct header fields in the filter
}
您还可以通过创建一个将状态作为参数接受的注释使其更通用,即 @ResponseStatus(Status.CREATED)
并使用 responseContext.getAnnotations()
.
在过滤器中获取状态
评论示例,根据 OP 的要求:
I don't think there is an equivalent, but personally, I like creating my own response. I have more control. Also there is a Response.created(...)
, this will automatically set the status. It accepts the URI or String as an argument, and sets the location header with that argument. Also You can use UriInfo
to getAbsolutePathBuilder()
then just append the created id. That's generally the way I go about it.
@Path("/widgets")
public class WidgetResource {
@Inject
WidgetService widgetService;
@POST
@Consumes(...)
public Response createWidget(@Context UriInfo uriInfo, Widget widget) {
Widget created = widgetService.createWidget(widget);
UriBuilder builder = uriInfo.getAbsolutePathBuilder();
URI uri = builder.path(created.getId()).build();
return Response.created(uri).build();
}
}
这是我用于创建方法的一般模式。 collection 路径将是从 uriInfo.getAbsolutePath(Builder)
获得的绝对路径,然后您只需将创建的 id 附加到路径即可。所以如果 collection 路径是 http://blah.com/widgets
,id 是 someId
,那么位置 header 将是 Location: http://blah.com/widgets/someId
(这是新资源的位置),状态将设置为 201 Created
Response.created(..)
returns Response.ResponseBuilder
,就像Response.status
一样,所以你可以进行通常的方法链接。 Response
上有许多具有默认设置的静态方法,例如 ok
、noContent
。只需完成 API。他们的名字与状态名称非常匹配。
这个 Spring MVC 代码的 Jersey 等价物是什么?在成功 POST:
之后,我需要对 return 201 的响应以及资源 URL@RequestMapping(method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
Widget create(@RequestBody @Valid Widget wid) {
return service.create(wid);
}
这是我在泽西岛找到的最短的例子。是否需要手动构建响应才能成功 POST/201?
@POST @Path("widget")
Response create(@RequestBody @Valid Widget wid) {
return Response
.status(Response.Status.CREATED)
.entity("new widget created")
.header("Location","http://localhost:7001/widget"+wid)
.build();
}
我认为 Jersey 中没有这样的注释。您可以使用 Name Binding.
创建一个基本上,您创建一个注释并添加 @NameBinding
元注释:
@NameBinding
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ResponseStatusCreated {}
接下来创建一个将覆盖状态的过滤器。
@ResponseStatusCreated
@Provider
class StatusCreatedFilter implements ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext requestContext,
ContainerResponseContext responseContext) throws IOException {
responseContext.setStatusInfo(Response.Status.CREATED)
String location = "..."; // set based on responseContext.getEntity()
// or any other properties
responseContext.getHeaders().putSingle("Location", location);
}
}
然后在您的资源方法上使用相同的注释。
@POST
@Path("widget")
@ResponseStatusCreated
Object create(@RequestBody @Valid Widget wid) {
return ... // return whatever you need to build the
// correct header fields in the filter
}
您还可以通过创建一个将状态作为参数接受的注释使其更通用,即 @ResponseStatus(Status.CREATED)
并使用 responseContext.getAnnotations()
.
评论示例,根据 OP 的要求:
I don't think there is an equivalent, but personally, I like creating my own response. I have more control. Also there is a
Response.created(...)
, this will automatically set the status. It accepts the URI or String as an argument, and sets the location header with that argument. Also You can useUriInfo
togetAbsolutePathBuilder()
then just append the created id. That's generally the way I go about it.
@Path("/widgets")
public class WidgetResource {
@Inject
WidgetService widgetService;
@POST
@Consumes(...)
public Response createWidget(@Context UriInfo uriInfo, Widget widget) {
Widget created = widgetService.createWidget(widget);
UriBuilder builder = uriInfo.getAbsolutePathBuilder();
URI uri = builder.path(created.getId()).build();
return Response.created(uri).build();
}
}
这是我用于创建方法的一般模式。 collection 路径将是从 uriInfo.getAbsolutePath(Builder)
获得的绝对路径,然后您只需将创建的 id 附加到路径即可。所以如果 collection 路径是 http://blah.com/widgets
,id 是 someId
,那么位置 header 将是 Location: http://blah.com/widgets/someId
(这是新资源的位置),状态将设置为 201 Created
Response.created(..)
returns Response.ResponseBuilder
,就像Response.status
一样,所以你可以进行通常的方法链接。 Response
上有许多具有默认设置的静态方法,例如 ok
、noContent
。只需完成 API。他们的名字与状态名称非常匹配。