HttpServletResponse对javax.ws.rs.core.Response

时间:2019-02-13 09:03:54

标签: java rest http

我有一个Rest Get api:

options.extend(['-ccbin', 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\bin\\cl.exe'])

如何将更新后的@GET @Path("myEndpoint") public Response getSomething(@Context HttpServletResponse response) { // do something with httpServletResponse } 返回为HttpServletResponse

3 个答案:

答案 0 :(得分:0)

您没有,您已经建立了Response并返回了该值:

return Response.ok(entityYouWantToReturn).build();

示例:

@GET
@Path("myEndpoint")
public Response getSomething(@Context HttpServletResponse response) {
    return Response.ok("it worked!").build();
}

这将导致200 OK正文为it worked!的响应。您可以使用@Produces@Consumes来使您的API接受和/或返回不同的数据类型(application / json等)。

答案 1 :(得分:0)

您可以通过在课程中添加HttpServletRequest来更新@Context

@Context
private HttpServletRequest request;

并在方法中进行检查

 // do something with httpServletResponse
 checkRequest(request);

答案 2 :(得分:0)

扩展@BlackSlash答案, 更好的语法是:

useEffect

仅记得正确设置媒体类型,例如:

Response.status(Response.Status.OK).entity(any object here).build();
Or another example:
Response.status(Response.Status.BAD_REQUEST).entity("Error message...").build();