[ 
https://issues.apache.org/jira/browse/CXF-8314?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17154139#comment-17154139
 ] 

Andriy Redko commented on CXF-8314:
-----------------------------------

Hi [~Kevin Zheng] ,

Providing different resource endpoints based on media type is totally legit. It 
is unclear what kind of problem you have, at least with respect to endpoints 
you included, they both use same media types: 
*@Consumes(\{MediaType.APPLICATION_FORM_URLENCODED, 
MediaType.APPLICATION_JSON})*

@POST
@Consumes(\{MediaType.APPLICATION_FORM_URLENCODED, MediaType.APPLICATION_JSON})
@Produces(MediaType.APPLICATION_JSON)
public void authorize(MultivaluedMap<String, String> parameter) \{ }

@POST
@Consumes(\{MediaType.APPLICATION_FORM_URLENCODED, MediaType.APPLICATION_JSON})
@Produces(MediaType.APPLICATION_JSON)
public void authorize(TestPojo pojo) \{ }

There is no way for CXF to distinguish this endpoints. At least, media types 
should be distinct, it is desirable to use *@FormParam("")* annotation to hint 
CXF how parameter(s) should be treated (for form-based endpoint):

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public void authorize(MultivaluedMap<String, String> parameter) \{ }

@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
public void authorize(@FormParam("") TestPojo pojo) \{ }

PS: You may also need to provide deserializer for *MultivaluedMap*, not 
entirely sure about that.

> 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
>            Priority: Minor
>
> 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.service.testServiceImpl"/>
> ######################jax-rs configuration######################



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to