rest请求响应压缩
So far when we send a response to the client we have always send the status code as 200
in case of successful response but a good REST API design API sends different status code in response for different situations.
到目前为止,当我们向客户端发送响应时,在成功响应的情况下,我们始终将状态代码发送为200
,但是良好的REST API设计API会针对不同情况发送不同的状态代码。
For example, we should send the status code of 201
in case of POST
method, which broadcasts to the client that the resource has been successfully created.
例如,在采用POST
方法的情况下,我们应发送状态代码201
,该状态代码向客户端广播资源已成功创建。
public Response getAllStudents(@Context HttpHeaders headers)
{
List<Student> students = studentService.getAllStudents();
GenericEntity<List<Student>> entity = new GenericEntity<List<Student>>(students){};
return Response.status(Status.OK).entity(entity).build();
}
The response class contains the HTTP line, HTTP headers and the HTTP message body.
响应类包含HTTP行,HTTP标头和HTTP消息正文。
@POST
@Produces(MediaType.APPLICATION_XML)
public Response addStudent(Student student)
{
String response = studentService.addStudent(student);
return Response.status(Status.CREATED).entity(response).build();
}
So for the POST
method the 201, Created status is sent. Hence, with the use of the Response
class, more generic and informative response can be sent to the client.
因此,对于POST
方法,发送201,创建状态。 因此,通过使用Response
类,可以将更通用,更有意义的响应发送给客户端。
翻译自: https://www.studytonight.com/rest-web-service/building-a-generic-response
rest请求响应压缩