[英]Equivalent of Spring MVC @ResponseStatus(HttpStatus.CREATED) in Jersey?
What's the Jersey equivalent of this Spring MVC code? 这个Spring MVC代码的Jersey相当于什么? I need the response to return 201 along with the resource URL, following successful POST:
在成功发布POST之后,我需要响应以返回201以及资源URL:
@RequestMapping(method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
Widget create(@RequestBody @Valid Widget wid) {
return service.create(wid);
}
This is the shortest example I found in Jersey. 这是我在泽西岛发现的最短的例子。 Is it required to build the response manually for successful POST/201?
是否需要手动为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();
}
I don't think there is an annotation like that in Jersey. 我不认为泽西岛有这样的注释。 You could create one using Name Binding .
您可以使用名称绑定创建一个。
Basically, you create an annotation and add the @NameBinding
meta-annotation: 基本上,您创建一个注释并添加
@NameBinding
元注释:
@NameBinding
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ResponseStatusCreated {}
Next you create an filter which will override the status. 接下来,您将创建一个覆盖状态的过滤器。
@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);
}
}
Then use the same annotation on your resource methods. 然后在资源方法上使用相同的注释。
@POST
@Path("widget")
@ResponseStatusCreated
Object create(@RequestBody @Valid Widget wid) {
return ... // return whatever you need to build the
// correct header fields in the filter
}
You could also make it more generic by creating an annotation that will accept the status as an argument, ie @ResponseStatus(Status.CREATED)
and get the status in the filter using responseContext.getAnnotations()
. 您还可以通过创建将状态作为参数接受的注释
@ResponseStatus(Status.CREATED)
即@ResponseStatus(Status.CREATED)
并使用responseContext.getAnnotations()
获取过滤器中的状态来使其更通用。
Example of comment, per request of OP: 根据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.还有一个
Response.created(...)
,这将自动设置状态。 It accepts the URI or String as an argument, and sets the location header with that argument.它接受URI或String作为参数,并使用该参数设置位置标头。 Also You can use
UriInfo
togetAbsolutePathBuilder()
then just append the created id.你也可以使用
UriInfo
来获取getAbsolutePathBuilder()
然后只需附加创建的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();
}
}
This is the general pattern I use for my create methods. 这是我用于创建方法的一般模式。 The collection path will be the absolute path obtained from
uriInfo.getAbsolutePath(Builder)
, then you just append the created id to the path. 收集路径将是从
uriInfo.getAbsolutePath(Builder)
获得的绝对路径,然后您只需将创建的id附加到路径。 So if the collection path is http://blah.com/widgets
, and the id is someId
, then the location header will be Location: http://blah.com/widgets/someId
(which is the location of the new resource), and the status will get set to 201 Created
因此,如果收集路径是
http://blah.com/widgets
,并且id是someId
,那么位置标题将是Location: http://blah.com/widgets/someId
someId
(这是新资源的位置) ),状态将设置为201 Created
Response.created(..)
returns Response.ResponseBuilder
, just like Response.status
, so you can do the usual method chaining. Response.created(..)
返回Response.ResponseBuilder
,就像Response.status
一样,所以你可以做通常的方法链接。 There are a number of static method on Response
that have default settings, like ok
, noContent
. Response
上有许多静态方法都有默认设置,比如ok
, noContent
。 Just do through the API. 只需通过API。 Their names pretty much match up with the status name.
他们的名字几乎与状态名称相匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.