![](/img/trans.png)
[英]JAX-RS exception mapper: What about wrapped exceptions and default case?
[英]JAX-RS Exception Mapper not working in Grizzly container
隨着項目的規模越來越大,與團隊一起工作在Jersey Web應用程序上,我們決定從Tomcat切換到Grizzly,以允許將項目的一部分部署在不同的端口號上。 我現在發現,我們擁有的自定義異常處理現在無法正常工作,相反,我總是得到灰白的html頁面。
示例異常:
public class DataNotFoundException extends RuntimeException{
private static final long serialVersionUID = -1622261264080480479L;
public DataNotFoundException(String message) {
super(message);
System.out.println("exception constructor called"); //this prints
}
}
制圖員:
@Provider
public class DataNotFoundExceptionMapper implements ExceptionMapper<DataNotFoundException>{
public DataNotFoundExceptionMapper() {
System.out.println("mapper constructor called"); //doesnt print
}
@Override
public Response toResponse(DataNotFoundException ex) {
System.out.println("toResponse called"); //doesnt print
ErrorMessage errorMessage = new ErrorMessage(ex.getMessage(), 404, "No documentation yet.");
return Response.status(Status.NOT_FOUND)
.entity(errorMessage)
.build();
//ErrorMessage is a simple POJO with 2 string and 1 int field
}
}
我不確定問題出在哪里,如果需要,我可以提供更多的信息/代碼。 有什么問題,我該怎么辦?
編輯 :
Main.class:
public class Main {
/**
* Main method.
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
...
List<ServerInfo> serverList = new ArrayList<ServerInfo>();
serverList.add(new ServerInfo(
"api",8450,
new ResourceConfig().registerClasses(
the.package.was.here.ApiResource.class)
));
for(ServerInfo server : serverList) {
server.start();
}
System.out.println("Press enter to exit...");
System.in.read();
for(ServerInfo server : serverList) {
server.stop();
}
}
}
EDIT2:基於此問題,我嘗試使用此ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR, "true"
屬性,該功能僅提供了一點幫助。 發生異常時,我仍然會看到html grizzly頁面,但是現在我在頁面的主體中看到了我的異常(+堆棧跟蹤)。
您只需要為整個應用程序注冊一個資源類
new ResourceConfig().registerClasses(
eu.arrowhead.core.api.ApiResource.class
)
映射器也需要注冊
new ResourceConfig().registerClasses(
eu.arrowhead.core.api.ApiResource.class,
YourMapper.class)
)
您還可以使用包掃描,如果使用@Path
或@Provider
進行注釋,它將掃描所有類並自動注冊它們
new ResourceConfig().packages("the.packages.to.scan")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.