I have a REST service where in case of bad authorisation, I return 401 and some error message.
Example if I use postman or other rest client, the response status is 401 and payload:
{
"data": null,
"errors": [
{
"code": "REQUEST_NOT_AUTHORIZED",
"message": "Request not authorized"
}
]
}
If I use RestEasy client, then this exception is thrown automatically by the client:
EJB Invocation failed on component GatewayApi for method public com.example.AuthToken com.example.GatewayApi.authenticate(....): javax.ejb.EJBException: javax.ws.rs.NotAuthorizedException: HTTP 401 Unauthorized
Caused by: javax.ws.rs.NotAuthorizedException: HTTP 401 Unauthorized
If I try/catch the exception, then my payload is gone.
The way I am implementing is (for example):
ResteasyClient client = new ResteasyClientBuilder().build();
ResteasyWebTarget target = client.target(UriBuilder.fromPath(SERVICE_URL));
proxy = target.proxy(GatewayApiInterface.class);
Later edit - auth method
public AuthToken authenticate(String id, String name, String password) {
try {
ResponseEnvelope<AuthToken> authTokenResponseEnvelope = proxy.authenticate(id, name, password);
return authTokenResponseEnvelope.getData();
} catch (javax.ws.rs.NotAuthorizedException wae) {
return null;
}
}
Is any way to stop RestEasy throwing exception every time status != 200? Or some way to obtain my original payload from the Rest Server?
Fixed it :). This is a small test example that I used. Before I was not using Response (my bad)
Server Side
@GET
@Path("/test")
public Response test() {
return Response.status(Response.Status.UNAUTHORIZED)
.entity("TEST")
.build();
}
Client Side Proxy Class
@GET
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
Response test();
Client
Response response = proxy.test();
String test = response.readEntity(String.class);
System.out.println(test);
System.out.println(response.getStatus());
response.close();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.