Hi Sergey,
I'm using CXF_2.7.3.
With your help I've found the solution using this BodyReader added clientSide:

@Provider
public class StringArrayBodyReader implements MessageBodyReader<Object> {
     
        private static ObjectMapper jsonMapper = new ObjectMapper();

        @Override
        public boolean isReadable(Class<?> arg0, Type arg1, Annotation[] arg2,
                MediaType arg3) {
            return String[].class.isAssignableFrom(arg0);
        }

        @Override
        public String[] readFrom(Class<Object> arg0, Type arg1,
                Annotation[] arg2, MediaType arg3,
                MultivaluedMap<String, String> arg4, InputStream arg5)
                throws IOException, WebApplicationException {
            // get the content as a string
            String toSplit = streamToString(arg5);
            String[] splitted = null;
            if(toSplit!=null){
                // try to split the String in a String[]
                splitted = jsonMapper.readValue(toSplit, String[].class);
            }else{
                log.error("Failure splitting the content into a String[]");
            }
            return splitted;
        }
        
        private String streamToString(InputStream in) throws IOException {
            try{
                StringBuilder out = new StringBuilder();
                BufferedReader br = new BufferedReader(new 
InputStreamReader(in));
                for(String line = br.readLine(); line != null; line = 
br.readLine()) 
                    out.append(line);
                br.close();
                return out.toString();
            }catch(Exception e){
                log.error("Failure getting the String corresponding to the 
InputStream",e);
                return null;
            }
        }
}

Now it works good!! Thanks a lot!
I've a latest question for you:
if from the server I return a String[] with "normal" Strings, the String arrive 
properly formatted to the BodyReader; if from the server I return "special" 
Strings, like °C, in the BodyReader the String arrives modified: °C
I can add a cleaning of the Strings in the BodyReader at client side but I 
prefer to return always a cleaned String from the Server: do you know if I can 
add some encoding at the endPoints publication, or  if it is better to add an 
interceptor server side performing the cleaning, or something else?
Thanks,

Andrea

Date: Thu, 4 Jul 2013 06:31:24 -0700
From: [email protected]
To: [email protected]
Subject: Re: java.lang.ClassCastException: [B cannot be cast to 
[Ljava.lang.Object



        Hi, can you temp make that provider typed on Object, or if it is CXF 

2.7.5, set "org.apache.cxf.jaxrs.providers.ignore.typevars" to true ?


It is a bug: the runtime does not currently deal at all with 

GenericArrayType


I'll be fixing it shortly

Many thanks, Sergey


On 04/07/13 12:44, eanbiso wrote:

> Hi Sergey,

> unfortunately the StringArrayBodyReader is the only one provider I've added 
> loading the endPoint at client side.

> If it can be useful I do a quick recap of the situation.

> - Using swagger u.i that expects a json format the response body is:

>      ["°C"]

> and no problem occurs

> - This is the reponse I see from web browser when I call the REST web method 
> http://*****/DMConfigRest/getUnitsOfMeasure/0/12/0/10/0:
>      ["°C"]

> (there is an unexpected char: Â, but no problem occurs at the invocation)

> - Calling from java code the "No message body reader..." exception occurs 
> client side.

> - In all the cases no exception occurs server side.

> - The annotations I've used to tag the ws interface are these:

>

> @WebService(targetNamespace = JettyConstants.DMConfigNameSpace)

> @Api(value = "/DMConfigRest", description = "Operations about 
> DataModelConfigurator service, used from all the actors which want to edit 
> the XML configuration file used by WSNDataModel")

> @Produces({"application/json"})

> public interface IDataModelConfig extends IPublishable { ...

>

>

>      @GET

>      
> @Path("/getUnitsOfMeasure/{appGardenID}/{protType}/{profileID}/{objectIDhigh}/{objectIDlow}")

>      @ApiOperation(value = "Tells you what are the units of measure for the 
> object.",

>                      notes = "Returns the unit of measure of the current 
> object.", response = String.class, 
> produces="application/xml,application/json")

>      public String[] getUnitsOfMeasure(

>              @ApiParam(value = "appGardenID", required = true) @WebParam(name 
> = "appGardenID") @PathParam("appGardenID") 
> @CxfWSAuthGrain(type=authType.appGarden) int appGardenID,

>              @ApiParam(value = "protType", required = true) @WebParam(name = 
> "protType") @PathParam("protType") short protType,

>              @ApiParam(value = "profileID", required = true) @WebParam(name = 
> "profileID") @PathParam("profileID") int profileID,

>              @ApiParam(value = "objectIDhigh", required = true) 
> @WebParam(name = "objectIDhigh") @PathParam("objectIDhigh") int objectIDhigh,

>              @ApiParam(value = "objectIDlow", required = true) @WebParam(name 
> = "objectIDlow") @PathParam("objectIDlow") int objectIDlow) throws 
> InstantiationException;

>

> ....

> }

>

> - Now I've added the body reader at client side but I'm not able to arrive in 
> its readFrom method.

>

>

> Thanks,

> Andrea

>

> Date: Thu, 4 Jul 2013 03:23:44 -0700

> From: [hidden email]

> To: [hidden email]

> Subject: Re: java.lang.ClassCastException: [B cannot be cast to 
> [Ljava.lang.Object

>

>

>

>       

> Hi Andrea,

>

>

> Re your earlier email: no problems about the questions, please keep them

>

> coming,

>

>

> Re this issue: do you have other providers registered ? Other than that,

>

> I can't think of why readFrom is not called

>

>

> Thanks, Sergey

>

>

> On 04/07/13 11:15, eanbiso wrote:

>

>> Hi Sergey,

>

>> I've tried this solution: I've add a BodyReader at clientSide to manage the

>

>> response from the server.

>

>> I've added this provider:

>

>>

>

>> @Provider

>

>> public class StringArrayBodyReader implements MessageBodyReader<String[]> {

>

>>      

>

>>              private static final Plat1Logger log = new

>

>> Plat1Logger(StringArrayBodyReader.class);

>

>>

>

>>              @Override

>

>>              public boolean isReadable(Class<?> arg0, Type arg1, 
>> Annotation[] arg2,

>

>>                              MediaType arg3) {

>

>>                      return String[].class.isAssignableFrom(arg0);

>

>>              }

>

>>

>

>>              @Override

>

>>              public String[] readFrom(Class<String[]> arg0, Type arg1,

>

>>                              Annotation[] arg2, MediaType arg3,

>

>>                              MultivaluedMap<String, String> arg4, 
>> InputStream arg5)

>

>>                              throws IOException, WebApplicationException {

>

>>                      // TODO Auto-generated method stub

>

>>                      return null;

>

>>              }

>

>> }

>

>>

>

>> at the loading of the REST endPoints:

>

>>

>

>>              JAXRSClientFactoryBean proxyFactory = new 
>> JAXRSClientFactoryBean();

>

>>              proxyFactory.setServiceClass(clazz);

>

>>              proxyFactory.setAddress(address);

>

>>              proxyFactory.setProvider(new StringArrayBodyReader());

>

>>

>

>> by the client application.

>

>> Debugging I've seen that when I call a REST web method from the client I

>

>> arrive in the isReadable method of the provider and it returns true only if

>

>> the return type is a String[].... but I' m not yet able to arrive in the

>

>> readFrom method of the provider (this even if the isReadable has returned a

>

>> true value).

>

>> Do you know why this happens?

>

>> And how can I fix it and force to call the readFrom method when isReadable

>

>> returns true?

>

>> In this way I hope to solve the problem.

>

>> Thanks a lot,

>

>>

>

>> Andrea

>

>>

>

>>

>

>>

>

>>

>

>>

>

>>

>

>> --

>

>> View this message in context: 
>> http://cxf.547215.n5.nabble.com/java-lang-ClassCastException-B-cannot-be-cast-to-Ljava-lang-Object-tp5730141p5730271.html
>> Sent from the cxf-user mailing list archive at Nabble.com.

>

>>

>

>



        
        
        
        

        

        
        
                If you reply to this email, your message will be added to the 
discussion below:
                
http://cxf.547215.n5.nabble.com/java-lang-ClassCastException-B-cannot-be-cast-to-Ljava-lang-Object-tp5730141p5730306.html
        
        
                
                To unsubscribe from java.lang.ClassCastException: [B cannot be 
cast to [Ljava.lang.Object, click here.

                NAML
                                                  



--
View this message in context: 
http://cxf.547215.n5.nabble.com/java-lang-ClassCastException-B-cannot-be-cast-to-Ljava-lang-Object-tp5730141p5730309.html
Sent from the cxf-user mailing list archive at Nabble.com.

Reply via email to