我将路径变量作为枚举。如果productType
的值无效,则会在JBoss上部署HTML错误页面,该页面带有stacktraces。我想做的是处理此错误,并返回一个标准的GenericApiResponse
JSON对象。
@Path("product")
@Produces({ APPLICATION_JSON })
public class ProductEndpoint {
@GET
@Path("/{productType}/info")
public GenericApiResponse info(@PathParam("productType") ProductType productType) {
// Get product info and return
}
}
枚举类:
public enum ProductType {
GLOBAL,
INTERNAL
public static ProductType fromString(String value) {
for(ProductType t : ProductType.class.getEnumConstants()) {
if(t.name().toLowerCase().equals(value)) {
return t;
}
}
throw new InvalidProductTypeException();
}
}
HTML stacktrace(无用)
JBWEB000071: root cause
java.lang.NoSuchMethodError: javax.ws.rs.core.Response.hasEntity()Z
org.apache.cxf.jaxrs.utils.ExceptionUtils.convertFaultToResponse(ExceptionUtils.java:67)
org.apache.cxf.jaxrs.utils.JAXRSUtils.convertFaultToResponse(JAXRSUtils.java:1516)
org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.convertExceptionToResponseIfPossible(JAXRSInInterceptor.java:261)
org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.handleMessage(JAXRSInInterceptor.java:92)
org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:272)
org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:241)
org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:248)
org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:222)
org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:153)
org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:171)
org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:286)
org.apache.cxf.transport.servlet.AbstractHTTPServlet.doGet(AbstractHTTPServlet.java:211)
javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:262)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
因此,我尝试制作自己的ExceptionMapper
,仅此方法无效。永远不会调用我的班级,并且仍会显示HTML错误页面。
InvalidProductTypeExceptionMapper.java
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class InvalidProductTypeExceptionMapper implements ExceptionMapper<InvalidProductTypeException> {
@Override
public Response toResponse(InvalidProductTypeExceptione) {
// This is never being called
return Response.status(Response.Status.OK)
.entity(new GenericApiResponse(ResultCode.INVALID_PARAMETERS))
.build();
}
}
cxf.xml
<jaxrs:providers>
<bean class="com.my.mapper.InvalidProductTypeExceptionMapper "/>
</jaxrs:providers>
我的问题:如何处理此类错误并返回自定义响应?
答案 0 :(得分:0)
我不认为在您的枚举类中添加fromString
方法在CXF中没有任何作用。
要进行转换,请考虑使用ParamConverter和ParamConverterProvider:
public class ProductTypeConverter implements ParamConverter<ProductType> {
@Override
public ProductType fromString(String value) {
// throw your exception here.
...
}
@Override
public String toString(ProductType value) {
...
}
}
然后添加
@Provider
public class MyParamConverterProvider implements ParamConverterProvider{
@Override
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) {
if(rawType.equals(ProductType.class)) {
return (ParamConverter<T>) new ProductTypeConverter();
}
return null;
}
}
将提供程序与ExceptionMapper一起添加到端点。它应该为您带来所需的结果。