rest请求响应压缩_REST:建立通用响应

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请求响应压缩

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值