Kevin Zheng created CXF-8314:
--------------------------------
Summary: Consumes Multiple Heterogeneous Media Types with Resource
Method Paramters
Key: CXF-8314
URL: https://issues.apache.org/jira/browse/CXF-8314
Project: CXF
Issue Type: New Feature
Components: JAX-RS
Reporter: Kevin Zheng
Hi, I got a use case that we were trying to employ one API to support request
with either JSON or FORM media type. On the server side, we don't want to
manually populate the name/key-value pairs with MessageContext. Instead we'd
like to deserialize the request body to the resource method parameter by CXF
itself.
Something unexpected happened when I was using MultivalueMap to consume body in
JSON or using POJO to consume body in Form. Please refer to the following.
######First trial: resource method parameter type is MultivaluedMap######
@POST
@Consumes({MediaType.APPLICATION_FORM_URLENCODED, MediaType.APPLICATION_JSON})
@Produces(MediaType.APPLICATION_JSON)
public void authorize(MultivaluedMap<String, String> parameter) { }
######First trial: resource method parameter type is MultivaluedMap######
######Secon trial: resource method parameter type is POJO######
@POST
@Consumes({MediaType.APPLICATION_FORM_URLENCODED, MediaType.APPLICATION_JSON})
@Produces(MediaType.APPLICATION_JSON)
public void authorize(TestPojo pojo) { }
######Secon trial: resource method parameter type is POJO######
######################request with json begin######################
Address: http://localhost:5759/gw/v1/test
Encoding: UTF-8
Http-Method: POST
Content-Type: application/json; charset=UTF-8
Headers: {Accept=[*/*], accept-encoding=[gzip,deflate],
connection=[keep-alive], Content-Length=[15], content-type=[application/json;
charset=UTF-8], Host=[localhost:5759], User-Agent=[Apache-HttpClient/4.5.2
(Java/1.8.0_201)]}
Payload: {"data":"xxxx"}
######################request with json end######################
######################request with form begin######################
Address: http://localhost:3471/gw/v1/test?data=yyyy
Encoding: ISO-8859-1
Http-Method: POST
Content-Type: application/x-www-form-urlencoded; charset=ISO-8859-1
Headers: {Accept=[*/*], accept-encoding=[gzip,deflate],
connection=[keep-alive], Content-Length=[0],
content-type=[application/x-www-form-urlencoded; charset=ISO-8859-1],
Host=[localhost:3471], User-Agent=[Apache-HttpClient/4.5.2 (Java/1.8.0_201)]}
######################request with json begin######################
######################jax-rs configuration######################
<jaxrs:server id="serviceContainer" address="/">
<jaxrs:serviceBeans>
<ref bean="testService"/>
</jaxrs:serviceBeans>
<jaxrs:providers>
<bean
class="com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider"/>
</jaxrs:providers>
</jaxrs:server>
<bean id="testService"
class="com.ericsson.ngin.oauthserver.service.testServiceImpl"/>
######################jax-rs configuration######################
--
This message was sent by Atlassian Jira
(v8.3.4#803005)