您需要注销所有默认的异常映射器,然后注册自己来处理你想要的例外:
例如,在你的YAML,您需要:
server:
registerDefaultExceptionMappers: false
rootPath: /api/*
requestLog:
appenders: []
applicationConnectors:
- type: http
port: 9085
logging:
level: INFO
注:registerDefaultExceptionMappers: false
会告诉DW不注册任何ExceptionMappers。
然后,你可以自己实现它们。就我而言,我将只是做一个包罗万象的处理程序:
public class MyExceptionMapper implements ExceptionMapper<Exception> {
@Override
public Response toResponse(Exception exception) {
return Response.status(400).entity("This makes no sense").build();
}
}
这反应任何异常,并有400和串做出响应。
最后,登记在主类:
environment.jersey().register(MyExceptionMapper.class);
,并证明了一个试验:
[email protected]:~/dev/eclipse/eclipse_jee$ curl -v "http://localhost:9085/api/viewTest"
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 9085 (#0)
> GET /api/viewTest HTTP/1.1
> Host: localhost:9085
> User-Agent: curl/7.47.0
> Accept: */*
>
< HTTP/1.1 400 Bad Request
< Date: Wed, 12 Oct 2016 10:16:44 GMT
< Content-Type: text/html
< Content-Length: 19
<
* Connection #0 to host localhost left intact
This makes no sense
希望帮助,
- 阿图尔
OMG谢谢。我需要做到这一点,但是我确信我需要在不久的将来实现这样的事情。我回来了 – MedicineMan