Java springboot如何基于Jax-RS响应Httpstatus在try块中抛出多个自定义异常

Java springboot如何基于Jax-RS响应Httpstatus在try块中抛出多个自定义异常,java,spring-boot,exception,jax-rs,try-catch,Java,Spring Boot,Exception,Jax Rs,Try Catch,我有这样的想法: @ResponseStatus(value = HttpStatus.UNAUTHORIZED) public class MyCustomExceptionA extends RuntimeException { public MyCustomExceptionA(String message) { super(message); } } @ResponseStatus(value = HttpStatus.NOT_FOUND) public

我有这样的想法:

@ResponseStatus(value = HttpStatus.UNAUTHORIZED)
public class MyCustomExceptionA extends RuntimeException {
    public MyCustomExceptionA(String message)  {
        super(message);
    }
}
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public class MyCustomExceptionB extends RuntimeException {
    public MyCustomExceptionA(String message){
        super(message);
    }
}
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public class MyCustomExceptionC extends RuntimeException {
    public MyCustomExceptionA(String message)  {
        super(message);
    }
}
@ControllerAdvice
public class SomeClass  {
    @ExceptionHandler(MyCustomExceptionA.class)
    public ResponseEntity<ExceptionResponse> method1(MyCustomExceptionA  ex){
        ExceptionResponse response = new ExceptionResponse(401, ex.getMessage())
    return new ResponseEntity<>(response, HttpStatus.UNAUTHORIZED);
  }

  method2 for MyCustomExceptionB

  method3 for MyCustomExceptionC
}
@ResponseStatus(值=HttpStatus.UNAUTHORIZED)
公共类MyCustomExceptionA扩展了RuntimeException{
公共MyCustomExceptionA(字符串消息){
超级(信息);
}
}
@ResponseStatus(值=HttpStatus.NOT_FOUND)
公共类MyCustomExceptionB扩展了RuntimeException{
公共MyCustomExceptionA(字符串消息){
超级(信息);
}
}
@ResponseStatus(值=HttpStatus.BAD_请求)
公共类MyCustomExceptionC扩展RuntimeException{
公共MyCustomExceptionA(字符串消息){
超级(信息);
}
}
@控制器建议
公共类{
@ExceptionHandler(MyCustomExceptionA.class)
公共响应方法1(MyCustomExceptionA ex){
ExceptionResponse=新的ExceptionResponse(401,例如getMessage())
返回新的ResponseEntity(响应,HttpStatus.UNAUTHORIZED);
}
MyCustomExceptionB的方法2
MyCustomExceptionC的方法3
}
我正在进行Jax-rsrest调用以获得响应

try{
    Response response = ClientBuilder.newCLient().target("someURL").path("somePath").get();

    if (response.getStatus() == 400){
        throw new MyCustomExceptionB("some Error message") <-- this don't get thrown
    }else if (response.getStatus() == 401){
        throw new MyCustomExceptionA("some Error message") <-- this don't get thrown
    }else if (response.getStatus() == 404){
        throw new MyCustomExceptionC("some Error message")  <-- this don't get thrown   
}catch(Exception ex){
    log.error("something happened ....")
    throw new Exception("message")                  <-- this overrides above exceptions
}
试试看{
Response Response=ClientBuilder.newCLient().target(“someURL”).path(“somePath”).get();
if(response.getStatus()==400){

抛出新的MyCustomExceptionB(“某些错误消息”)catch块捕获try块内的代码抛出的异常。这就是为什么在if-else链中抛出异常后,catch块内的代码会运行

我建议您在执行spring boot之前熟悉异常的基本知识

try{
  //potentially dangerous code here.
}catch(Exception ex){
  //Catches all Exceptions thrown in try block.
  //Handle ex.
}

您已经为Exception类定义了catch块,因此您在try块中抛出的任何异常都将在catch块中被捕获。这是预期的行为。请分享您试图实现的示例。