【问题标题】:Add message error to response body in Jersey在泽西岛的响应正文中添加消息错误
【发布时间】:2015-11-04 10:15:38
【问题描述】:

我有这个端点:

    @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();

【问题讨论】:

    标签: java jersey


    【解决方案1】:

    使用实体(对象数据)方法:

    Response.status(Response.Status.UNAUTHORIZED).entity("Some message here").build();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      相关资源
      最近更新 更多