生活随笔
收集整理的這篇文章主要介紹了
Java后端返回通用接口设计
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
我印象中,通用返回接口設計是有兩種方式,第一種是自定義返回數據的通用類,另一種是springboot中實現ResponseBodyAdvice接口,實現返回數據格式的統一,如我說的有錯,請大佬指出。本篇文章主要講解通過springboot實現的方式。
一、自定義返回數據的通用類的方式
這里我推薦這篇文章
二、springboot實現ResponseBodyAdvice接口的方式
Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User { private long id
; private String name
; private Integer age
;
}
通用返回類
@Data
@AllArgsConstructor
@NoArgsConstructor
public final class CommonResult < T> { private int status
= 1 ; private String code
= "" ; private String msg
= "" ; private T resultBody
; public CommonResult ( T resultBody
) { this . resultBody
= resultBody
; } public static < T> CommonResult
< T> successResult ( String successCode
, String successMsg
, T resultBody
) { CommonResult
< T> commonResult
= new CommonResult < > ( ) ; commonResult
. code
= successCode
; commonResult
. msg
= successMsg
; commonResult
. resultBody
= resultBody
; commonResult
. status
= 1 ; return commonResult
; } public static < T> CommonResult
< T> errorResult ( String errorCode
, String errorMsg
) { CommonResult
< T> commonResult
= new CommonResult < > ( ) ; commonResult
. code
= errorCode
; commonResult
. msg
= errorMsg
; commonResult
. status
= - 1 ; return commonResult
; }
}
自定義處理業務異常類,注意繼承自RuntimeException,用于手動控制拋出異常.
@EqualsAndHashCode ( callSuper
= true )
@Data
@AllArgsConstructor
public final class ResponseException extends RuntimeException { private String errorCode
; private String errorMsg
;
}
實現ResponseBodyAdvice接口
@EnableWebMvc
@Configuration
public class UnifiedReturnConfig { @RestControllerAdvice ( "com.commontest.demo.controller" ) static class CommonResultResponseAdvice implements ResponseBodyAdvice < Object> { @Override public boolean supports ( MethodParameter methodParameter
, Class
< ? extends HttpMessageConverter < ? >> aClass
) { boolean isIntercept
= true ; Method method
= methodParameter
. getMethod ( ) ; assert method
!= null
;
AnnotatedElement annotatedElement
= methodParameter
. getAnnotatedElement ( ) ; NoResponseAdvice noResponseAdvice
= AnnotationUtils
. findAnnotation ( annotatedElement
, NoResponseAdvice
. class ) ; if ( noResponseAdvice
!= null
) { isIntercept
= false ; } return isIntercept
; } @Override public Object
beforeBodyWrite ( Object body
, MethodParameter methodParameter
, MediaType mediaType
, Class
< ? extends HttpMessageConverter < ? >> aClass
, ServerHttpRequest serverHttpRequest
, ServerHttpResponse serverHttpResponse
) { if ( body
instanceof CommonResult ) { return body
; } return CommonResult
. successResult ( StatusAndMsg
. SUCCESS
. getCode ( ) , StatusAndMsg
. SUCCESS
. getMsg ( ) , body
) ; } }
}
繼承ResponseEntityExceptionHandler,封裝異常(400,404,500)處理
@Slf4j
@ControllerAdvice
public class RestResponseExceptionHandler extends ResponseEntityExceptionHandler { public RestResponseExceptionHandler ( ) { super ( ) ; } @Override protected ResponseEntity
< Object> handleExceptionInternal ( Exception e
, @Nullable Object body
, HttpHeaders headers
, HttpStatus status
, WebRequest request
) { if ( e
instanceof MissingServletRequestParameterException ) { return new ResponseEntity ( CommonResult
. errorResult ( "400" , e
. getMessage ( ) ) , headers
, status
) ; } if ( e
instanceof NoHandlerFoundException ) { return new ResponseEntity ( CommonResult
. errorResult ( "404" , e
. getMessage ( ) ) , headers
, status
) ; } if ( e
instanceof HttpRequestMethodNotSupportedException ) { return new ResponseEntity ( CommonResult
. errorResult ( "405" , e
. getMessage ( ) ) , headers
, status
) ; } return new ResponseEntity ( CommonResult
. errorResult ( "500" , e
. getMessage ( ) ) , headers
, status
) ; }
}
捕獲自定義的業務異常類
@RestControllerAdvice ( "com.commontest.demo.controller" )
public class ResponseExceptionHandler { @ExceptionHandler ( ResponseException
. class ) public CommonResult
< Void> handleResponseException ( ResponseException responseException
) { return CommonResult
. errorResult ( responseException
. getErrorCode ( ) , responseException
. getErrorMsg ( ) ) ; }
}
用于注解不經由ResponseAdvice處理的類方法
@Target ( { ElementType
. METHOD
} )
@Retention ( RetentionPolicy
. RUNTIME
)
@Documented
public @
interface NoResponseAdvice {
}
狀態碼信息枚舉類
public enum StatusAndMsg
{ SUCCESS ( "1000" , "SUCCESS" ) , METHODFAIL ( "2000" , "ENCOUNTER AN ERROR WHEN EXECUTE METHOD" ) , UNKNOWEXCEPTION ( "3000" , "THIS IS AN UNKNOW EXCEPTION" ) ; ERROR404 ( "404" , "There was an unexpected error (type=Bad Request, status=400)" ) , ERROR500 ( "500" , "There was an unexpected error (type=Internal Server Error, status=500)" ) ; private String code
; private String msg
; StatusAndMsg ( String code
, String msg
) { this . code
= code
; this . msg
= msg
; } public String
getCode ( ) { return code
; } public String
getMsg ( ) { return msg
; }
}
控制器類
@RequestMapping ( "/users" )
@RestController
public class UserController { @GetMapping ( "/get" ) public List
< User> getUserList ( ) { List
< User> userList
= new ArrayList < > ( ) ; userList
. add ( new User ( 1 l
, "messi" , 23 ) ) ; userList
. add ( new User ( 2 l
, "cristiano" , 23 ) ) ; return userList
; } @GetMapping ( "/{id}" ) public User
getUserById ( @PathVariable Long id
) { throw new ResponseException ( StatusAndMsg
. METHODFAIL
. getCode ( ) , StatusAndMsg
. METHODFAIL
. getMsg ( ) ) ; } @GetMapping ( "/getStr" ) @NoResponseAdvice public String
paramTest ( ) { return "str" ; } @GetMapping ( "/testVoid" ) @NoResponseAdvice public void testVoid ( ) { System
. out
. println ( "test void" ) ; }
}
注意,不要直接使用Exception捕捉全局異常,不然會導致雖然json返回了404狀態碼,但是http請求的狀態碼仍然是200。參考這篇文章
三、測試
如果有啥寫的不好的,麻煩各位大佬指出。 參考文章 參考文章 參考文章 詳細代碼
總結
以上是生活随笔 為你收集整理的Java后端返回通用接口设计 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。