我正在我的网络服务上尝试一些自定义错误处理。在我的网络服务中,我创建了一个扩展 WebApplicationException 的自定义异常类,如 JAX-RS / Jersey how to customize error handling? 中所述。 :
public class InternalServerErrorException extends WebApplicationException {
public InternalServerErrorException(String message) {
super(Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.header("errorMessage", message).type(MediaType.TEXT_PLAIN).build());
System.out.println(message);
}
}
为了测试,我有一个简单的 Country 服务,它提供了一个国家列表。我确保会发生错误以测试 InternalServerErrorException 的抛出:
@GET
@Override
@Path("countries")
@Produces({"application/xml"})
public List<Country> findAll() {
try {
int i = Integer.parseInt("moo"); //<-- generate error
List<Country> countries = super.findAll();
return countries;
} catch (Exception ex) {
throw new InternalServerErrorException("I want this message to show at client side");
}
}
在我的客户处,我有一个 CountryClient,方法如下:
public List<Country> findAll() throws ClientErrorException {
WebTarget resource = webTarget;
resource = resource.path("countries");
return resource.request(javax.ws.rs.core.MediaType.APPLICATION_XML).get(new GenericType<List<Country>>(){});
}
在我的 Controller 中,我是这样使用它的:
try {
CountryClientSSL cc = new CountryClientSSL();
cc.setUsernamePassword(USERNAME, PASSWORD);
ObservableList<Country> olCountries = FXCollections.observableArrayList(cc.findAll());
tblCountries.setItems(olCountries);
tcCountry.setSortType(SortType.ASCENDING);
tblCountries.getSortOrder().add(tcCountry);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
因此,每次我在客户端中使用 findAll() 方法时,我都会在输出窗口中看到以下信息:Info: I want this message to show at client side
在服务器端,HTTP 500 Internal Server Error
在客户端。
有没有办法在不使用 Response
的情况下在客户端(在 catch block 中)获取 errorMessage作为返回类型?
我想要实现的是:当客户端发生错误时,用户可以向我的 JIRA 实例发送调试报告,其中包含大量信息,如客户端的堆栈跟踪、有关系统,谁登录了,...如果能在其中附加服务器的堆栈跟踪信息就好了。还是有更好的方法来实现这一点?
最佳答案
Is there a way to get the errorMessage on the client side (in the catch block) without using
Response
as a return type?
您可以使用 response.readEntity(new GenericType<List<Country>>(){})
.这样您仍然可以访问 Response
.尽管在这种情况下不会有堆栈跟踪。当您尝试使用 get(ParsedType)
时,客户端上只有一个异常(exception).原因是 get(ParsedType)
,没有其他方法可以处理错误状态。但是使用 Response
,开发人员应该根据状态进行检查。所以这个方法看起来更像
public List<Country> findAll() throws ClientErrorException {
WebTarget resource = webTarget;
resource = resource.path("countries");
Response response = resource.request(javax.ws.rs.core.MediaType.APPLICATION_XML).get();
if (response.getStatus() != 200) {
System.out.println(response.getHeaderString("errorResponse"));
return null;
} else {
return response.readEntity(new GenericType<List<Country>>(){});
}
}
虽然不是在 header 中,我只是将消息作为响应正文发送出去。这就是我
super(Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity( message).type(MediaType.TEXT_PLAIN).build());
客户端
if (response.getStatus() != 200) {
System.out.println(response.readEntity(String.class));
return null;
}
关于java - 自定义 Jersey 错误处理,如何在客户端捕获响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33238546/