I did some work with apache CXF (version 2.2.2) JAX-RS. I am trying to enter a data validation level in a CXF request handler before calling a business method. Fortunately :), I ran into the problem of processing input parameters in the request handler (DataValidationHandler). I can read the JSON object manually by following the lines of code in the request handler. But it is duplicated with JSONProvider registered under CXF. Since the input stream of JSON objects can be read only once, otherwise we will encounter the exception "java.io.EOFException: there is no content to map to the object due to the end of input". Moreover, duplicating deserialization of JSON objects will affect performance. The following code is a sample for your reference.
To read the JSON object from the HTTP body manually:
OperationResourceInfo ori = paramMessage.getExchange().get(OperationResourceInfo.class); MultivaluedMap<String, String> values = new MetadataMap<String, String>(); List<Object> objList = JAXRSUtils.processParameters(ori, values, paramMessage);
Register JSONProvider in CXF JAX-RS:
<bean id="JSONProvider" class="com.accela.govxml2.jaxrs.util.JSONProvider"></bean>
Read the JSON object for the Java object from the input stream:
public Object readFrom(......){ ObjectMapper objectMapper = new ObjectMapper(); Object result = objectMapper.readValue(entityStream, TypeFactory.defaultInstance().constructType(genericType)); Return result; }
I am dealing with a path parameter manually following the lines of code.
OperationResourceInfo ori = paramMessage.getExchange().get(OperationResourceInfo.class); URITemplate t1 = ori.getClassResourceInfo().getURITemplate(); URITemplate t2 = ori.getURITemplate(); UriInfo uriInfo = new UriInfoImpl(paramMessage, null); MultivaluedMap<String, String> map = new MetadataMap<String, String>(); t1.match(uriInfo.getPath(), map); String str = map.get(URITemplate.FINAL_MATCH_GROUP).get(0); t2.match(str, map); String pathParameter= null; if (map.containsKey("pathParam") && !ValidationUtil.isEmpty(map.get("pathParam"))) { pathParameter= map.get("pathParam").get(0); }
My questions are here:
- How to work with the POST / PUT input parameter of the http object in the request handler in general?
- How to avoid performance issues to efficiently read input parameters?
- Is there a way to implement the verification level (handler / interceptor) between the CXF parameter readings (JSONProvider) and the business method call?
- Is there an elegant way to handle the path parameter?
Thanks for your help. Any comments and suggestions would be appreciated.
Regards, Dylan
source share