Java HTTPException返回状态代码

时间:2017-09-06 16:17:55

标签: java

请告知以下情况 我该如何处理Http状态代码

try {
    validateUser(a,b);
} catch(HttpException he){
    response = new ResponseEntity<>(status code);
}

....

validateUser(String a, String b) throws HttpException {
    if(some condition) {
        throw new HttpException(); // I want throw set status code to 401 so that I can catch it up there in catch block 
    }
}

1 个答案:

答案 0 :(得分:-1)

import java.xml.ws.http.HTTPException; 

try {     
    validateUser(a,b); 
} catch(HTTPException he){     
    response = new ResponseEntity<>(HttpStatus.valueOf(he.getStatusCode())); 
}

private void validateUser(String a, String b) throws HTTPException {     
    if(some condition) {         
        throw new HTTPException(HttpStatus.UNAUTHORIZED.value());      
    } 
}