我有这个终点:
@POST
@Path("login")
@Consumes(MediaType.APPLICATION_JSON)
public Response doLogin(LoginCredentials loginCredentials) {
try {
usersService.validate(loginCredentials);
return Response.ok().build();
} catch (InvalidCredentialsException e) {
return Response.status(Response.Status.UNAUTHORIZED).build();
} catch (NotAuthorizedUserException e) {
return Response.status(Response.Status.UNAUTHORIZED).build();
}
}
在UNAUTHORIZED
个案例中,我想发送一条消息来指定具体错误。怎么办?
如果回复为ok
,我会这样做:
返回Response.ok("Some message here").build();
答案 0 :(得分:8)
使用实体(对象数据)方法:
Response.status(Response.Status.UNAUTHORIZED).entity("Some message here").build();