jom*_*ora 13 java xml exception-handling jax-rs xmlhttprequest
我正在使用已经正常工作的JAX-RS上的Web服务.现在我正在寻找捕获一些异常的方法,以便向用户发送带有自定义消息的40X错误.
我有一个Web服务和一个ExceptionMapper.
这是我的网络服务:
@Path( value = "/test/")
public interface ServiceTest {
@Path(value = "{rrf}")
@GET
@Produces(MediaType.TEXT_XML)
public ObjectDTO getDealer(@PathParam("rrf") String rrf){
ObjectDTO objectDTO = new ObjectDTO();
if( verifyRRFSintax(rrf) ) {
//Get the objet, this part works fine
} else {
throw new IllegalArgumentException("Custom message");
}
return dwsDTO;
}
private boolean verifyRRFSintax(String rrf) {
return rrf.matches("[0-9]{8}");
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的ExceptionMapper
@Provider
@Produces(MediaType.TEXT_XML)
public class IllegalArgumentExceptionMapper
implements ExceptionMapper<IllegalArgumentException> {
@Override
public Response toResponse(IllegalArgumentException e) {
return Response.status(Response.Status.BAD_REQUEST).build();
}
}
Run Code Online (Sandbox Code Playgroud)
这就是它在application-context.xml文件中的注册方式
<bean id="serviceTest" class="ServiceTest"/>
<jaxrs:server id="Server" address="/ws">
<jaxrs:serviceBeans>
<ref bean="serviceTest"/>
</jaxrs:serviceBeans>
<jaxrs:providers>
<bean id="rffErrorException" class="IllegalArgumentExceptionMapper"/>
</jaxrs:providers>
</jaxrs:server>
Run Code Online (Sandbox Code Playgroud)
当我调试时,IllegalArgumentExceptionMapper捕获我抛出的异常但我没有在浏览器上显示的黄色网页上看到该消息.我总是有一个
Erreur d'analyze XML:aucunélémenttrouvé/ XML解析错误:找不到任何元素(英文)
如何在浏览器上显示此自定义消息?为什么,即使我更改了响应状态(NOT_FOUND,BAD_REQUEST,FORBIDDEN)的类型,这个黄页总是一样的?
PD:在控制台上我有一条消息"out.handlemessage",当Mapper捕获异常时会打印出来.
谢谢.
小智 16
throw new WebApplicationException(Response.status(Status.NOT_FOUND)// Or another Status
.entity("Error Message").build());
Run Code Online (Sandbox Code Playgroud)