在使用JAX-RS进行发布请求时,我已经看到了一些关于响应状态代码的约定。
我已经看到了:
Response.ok(//content or object).build();
我最初的想法是,这是错误的,因为状态码200表示正常,这是一个广义术语,但是由于我们已“发布”,因此我认为201会更好,因为它还返回了URI的URI。标头中的元素
Response.created(//content or object).build();
这是我想要实现的目标:
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response createUser(String content) throws CityNotFoundException
{
ReceivedCreateUser receivedUser = gson.fromJson(content, ReceivedCreateUser.class);
User createdUser = userFacade.createUser(receivedUser.name,
receivedUser.email,
receivedUser.password,
receivedUser.city,
receivedUser.gender,
receivedUser.dateOfBirth);
if(createdUser == null){
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
return Response.ok(gson.toJson(UserDTO.basic(createdUser))).build();
}
我想将实体添加到响应中,但是created
仅接受uri而不是实体。
答案 0 :(得分:1)
Response.ok()并非始终都正常,它可能表明请求已完成,但实际上未创建任何资源。您可能要使用Response.created或Response.accepted。与this link中一样,您可以看到这完全取决于后端的业务逻辑。
答案 1 :(得分:1)
是否在POST请求上返回201,很大程度上取决于其背后的功能。如果您的端点在系统中创建了一个新实体,那么201将是您的理想之选,但是POST也常用于通过大型选项集执行搜索请求。在这种情况下,如果有结果或没有结果,则最好使用200或204。
但是要返回带有POST和响应201的实体,您可以执行以下操作:
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response createUser(String content) throws CityNotFoundException
{
ReceivedCreateUser receivedUser = gson.fromJson(content, ReceivedCreateUser.class);
User createdUser = userFacade.createUser(receivedUser.name,
receivedUser.email,
receivedUser.password,
receivedUser.city,
receivedUser.gender,
receivedUser.dateOfBirth);
if(createdUser == null){
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
return Response
.created(new Uri("http://example.org"))
.entity(gson.toJson(UserDTO.basic(createdUser)))
.build();
}
答案 2 :(得分:0)
响应类型不是最好的返回给客户端和像Swagger这样的自动文档的方法,因为它没有太多信息。我特别喜欢直接将实体退还给以下人员:
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public UserDTO createUser(String content) throws CityNotFoundException
{
ReceivedCreateUser receivedUser = gson.fromJson(content, ReceivedCreateUser.class);
User createdUser = userFacade.createUser(receivedUser.name,
receivedUser.email,
receivedUser.password,
receivedUser.city,
receivedUser.gender,
receivedUser.dateOfBirth);
if(createdUser == null){
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
return UserDTO.basic(createdUser);
}
这将返回Status Code = 200
,但是有很多方法可以更改为201。我最喜欢的是将javax.ws.rs.container.ContainerResponseFilter.filter(...)
覆盖如下:
import static org.springframework.http.HttpMethod.POST;
import static org.springframework.http.HttpStatus.CREATED;
import static org.springframework.http.HttpStatus.OK;
import java.io.IOException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.ext.Provider;
@Provider
public class LocalContainerResponseFilter implements ContainerResponseFilter {
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
throws IOException {
if (POST.name().equals(requestContext.getMethod()) && OK.value() == responseContext.getStatus()) {
responseContext.setStatus(CREATED.value());
}
}
}
这将更改JAX-RS行为,使所有POST都返回201。
更多2条提示:
您可以将参数(字符串内容)更改为(ReceivedCreateUser内容);和
您可以将代码“ throw new WebApplicationException
”放入userFacade.createUser(...)