Uploaded image for project: 'CXF'
  1. CXF
  2. CXF-8314

Consumes Multiple Heterogeneous Media Types with Resource Method Paramters

    XMLWordPrintableJSON

    Details

    • Type: New Feature
    • Status: Resolved
    • Priority: Minor
    • Resolution: Information Provided
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: JAX-RS
    • Labels:
      None
    • Estimated Complexity:
      Unknown

      Description

      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######################

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              Kevin Zheng Kevin Zheng
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: