Reputation: 6108
I am having a path variable as an enum. Deployed on JBoss, a HTML error page with stacktraces is displayed if the value of productType
is invalid. What I want to do is to handle this error and return a standard GenericApiResponse
JSON object instead.
@Path("product")
@Produces({ APPLICATION_JSON })
public class ProductEndpoint {
@GET
@Path("/{productType}/info")
public GenericApiResponse info(@PathParam("productType") ProductType productType) {
// Get product info and return
}
}
Enum class:
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 (nothing useful)
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)
So I tried to make my own ExceptionMapper
, only it does not work. My class is never called, and the HTML error page is still showing.
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>
My question: How can I handle such error and return a custom response?
Upvotes: 0
Views: 430
Reputation: 11
I don't think adding the fromString
method to your enum class does anything in CXF.
To make the conversion, consider use ParamConverter and ParamConverterProvider:
public class ProductTypeConverter implements ParamConverter<ProductType> {
@Override
public ProductType fromString(String value) {
// throw your exception here.
...
}
@Override
public String toString(ProductType value) {
...
}
}
Then add
@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;
}
}
Add the provider to your endpoint, in conjunction with your ExceptionMapper. It should get you the result you want.
Upvotes: 1