And*_*w M 6 java http jersey dropwizard
我们使用Dropwizard/Jersey来构建Web服务.资源具有路径,并且该方法具有子路径.当返回创建的响应(201)时,我们获得的方法的路径被添加到我们提供的位置之前.当一个位置(我知道的设计)返回状态OK时,一切都很好,并且就像我们提供的那样返回位置.
我们如何返回不是我们方法位置的子路径的位置?
在下面的示例中:到"http:// localhost/foo/bar"(创建状态)的响应位置为"http:// localhost/foo/bar/wibble"(注意/ foo/bar)
到达"http:// localhost/foo/baz"(ok状态)响应"http:// localhost/wibble"的位置,这就是我们想要的.
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import java.net.URI;
@Path("/foo")
public class FooResource {
@POST
@Path("/bar")
public Response bar() {
URI uriOfCreatedResource = URI.create("/wibble");
return Response.created(uriOfCreatedResource).build();
}
@POST
@Path("/baz")
public Response baz() {
URI uriOfCreatedResource = URI.create("/wibble");
return Response.ok().location(uriOfCreatedResource).build();
}
}
Run Code Online (Sandbox Code Playgroud)
小智 8
如果有人偶然发现这件事; 我挖了泽西的代码,看看为什么会这样.这应该可以解释您的问题和Carlo的解决方法.
com.sun.jersey.spi.container.ContainerResponse包含此gem:
private void setHeaders(MultivaluedMap<String, Object> headers) {
this.headers = headers;
Object location = headers.getFirst(HttpHeaders.LOCATION);
if (location != null) {
if (location instanceof URI) {
final URI locationUri = (URI)location;
if (!locationUri.isAbsolute()) {
final URI base = (statusType.getStatusCode() == Status.CREATED.getStatusCode())
? request.getAbsolutePath() // WHY!?
: request.getBaseUri();
location = UriBuilder.fromUri(base).
path(locationUri.getRawPath()).
replaceQuery(locationUri.getRawQuery()).
fragment(locationUri.getRawFragment()).
build();
}
headers.putSingle(HttpHeaders.LOCATION, location);
}
}
}
Run Code Online (Sandbox Code Playgroud)
换句话说:出于某种原因,有人认为如果响应状态代码是201,则以不同方式处理位置标题是个好主意.像Carlo注意到的那样,使用绝对路径可以避免这个问题.
我在 GlassFish (JavaEE6) 上发生过这种情况。我认为这是一个错误,但我从未设法将代码挖掘到实际的 URI 转换......
不过我找到了一个解决方法:
public Response bar(@Context UriInfo info) {
URI absoluteURI=info.getBaseUriBuilder().path("/wibble").build();
return Response.created(absoluteURI).build();
}
Run Code Online (Sandbox Code Playgroud)