使用我非常簡單的JAX-RS服務,我使用Tomcat和JDBC領域進行身份驗證,因此我正在使用JSR 250註釋。使用JAX-RS(Jersey)和@RolesAllowed自定義HTTP狀態響應
問題是我想在HTTP狀態響應中返回一個自定義消息正文。狀態碼(403)應該保持不變。例如,我的服務如下所示:
@RolesAllowed({ "ADMIN" })
@Path("/users")
public class UsersService {
@GET
@Produces(MediaType.TEXT_PLAIN)
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public String getUsers() {
// get users ...
return ...;
}
}
如果不是以「ADMIN」不同角色的用戶訪問該服務,我想改變的響應消息,類似的東西(根據媒體類型[ XML/JSON):
<error id="100">
<message>Not allowed.</message>
</error>
目前新澤西返回了以下機身:
HTTP Status 403 - Forbidden
type Status report
message Forbidden
description Access to the specified resource (Forbidden) has been forbidden.
Apache Tomcat/7.0.12
如何更改默認的郵件正文?有沒有辦法來處理(也許拋出)異常來建立我自己的HTTP狀態響應?
完整的示例:https://www.bhaveshthaker.com/25/customize-handling-server-side-exceptions-with-error-codes-using-exceptionmapper-with-jersey-jax-rs-in-java/ – RAS