2014-03-06 39 views
0

当由于帐户被阻止而导致登录失败时,我想显示在后端设置的自定义消息。但我无法在响应主体中访问它,即使设置正确也无法确定。Java休息,如何设置和显示自定义错误消息

@POST 
@Path(/login) 
public Token logon() { 
    ... 

    String errorMsg = "This account is blocked please contact to Admin" 
    throw new NotAuthorizedException(errorMsg); 
} 

,然后在JavaScript方面,当我得到我想要做的错误消息:

if (rejection.status === 401) { 
    //rejection can be because of blocked account or invalid password, 
    //they will both return 401, but need to have different messages 

    $scope.message ="Show the error msg that I get from backend" ; 
} 

这里返回的排斥对象包括配置,数据,状态和标题对象。

但我不能看到我在后台任何地方设置的味精。

+0

不知道这一点,但接收或不返回错误的错误信息消息可以取决于容器本身以及响应的实现方式。 –

回答

0

创建NotAuthorizedException,而不只是一个消息时,您可以通过一个响应:

@POST 
@Path(/login) 
public Token logon() { 
    ... 

    String errorMsg = "This account is blocked please contact to Admin" 
    throw new NotAuthorizedException(Response.status(Response.Status.UNAUTHORIZED).entity(errorMsg).build()) 
} 

这应该会在回应主体

相关问题