Java string array as param

Java – Sending array of string as a parameter to web service method using JAXRPC

I’ve got problem sending an array of string as parameter to a web service method, given in a specific wsdl. When am trying to send an array of strings, I get the following error.

Error:

AxisFault faultCode: Server.userException faultSubcode: faultString: org.xml.sax.SAXException: Bad types (class java.util.ArrayList > class usdjws65.ArrayOfString) faultActor: faultNode: faultDetail: hostname:SSLSPSD001 org.xml.sax.SAXException: Bad types (class java.util.ArrayList -> class usdjws65.ArrayOfString) at org.apache.axis.message.SOAPFaultBuilder.createFault(SOAPFaultBuilder.java:222) at org.apache.axis.message.SOAPFaultBuilder.endElement(SOAPFaultBuilder.java:129) at org.apache.axis.encoding.DeserializationContext.endElement(DeserializationContext.java:1087) 

Code written:

Call call1 = objService1.createCall(port1); call1.setTargetEndpointAddress(targetEndPoint); call1.addParameter("int_1", XMLType.XSD_INT, Integer.class, ParameterMode.IN); call1.addParameter("String_1", QNAME_TYPE_STRING, ParameterMode.IN); call1.addParameter("String_2", QNAME_TYPE_STRING_ARRAY, java.lang.String[].class, ParameterMode.IN); call1.addParameter("String_3", QNAME_TYPE_STRING_ARRAY, java.lang.String[].class, ParameterMode.IN); call1.addParameter("String_4", QNAME_TYPE_STRING, ParameterMode.IN); call1.addParameter("String_5", QNAME_TYPE_STRING_ARRAY, java.lang.String[].class, ParameterMode.IN); call1.addParameter("String_6", QNAME_TYPE_STRING, ParameterMode.IN); call1.addParameter("String_7", QNAME_TYPE_STRING, ParameterMode.IN); // --- Done adding PARAM's String[] attrVals = < "description", "test from soapUI", "customer", ticketHandle, "type", "I" >; String[] attributes = < "status", "ref_num" >; Object[] params1 = < new Integer(sid), ticketHandle, attrVals, "", "cr_tpl:400005", attributes, "", "" >; String res = null; try < call1.invoke(params1); 

Update-1:

I added a class named ArrayOfString with following code in it.
protected java.lang.String[] string;

public ArrayOfString() < >public ArrayOfString(java.lang.String[] string) < this.string = string; >public java.lang.String[] getString() < return string; >public void setString(java.lang.String[] string)

and thus did the following,
ArrayOfString attrVals = new ArrayOfString();
attrVals.setString(new String[] < "customer", "test from soapUI",
"customer", ticketHandle, "type", "I" >);

Similarly, for attributes variable of type 'ArrayOfString'.

But now, I get the following error::

AxisFault
faultCode: Server.userException
faultSubcode:
faultString: java.io.IOException: No serializer found for class ArrayOfString in registry org.apache.axis.encoding.TypeMappingDelegate@ef2c60
faultActor:
faultNode:
faultDetail:
stackTrace:java.io.IOException: No serializer found for class ArrayOfString in registry org.apache.axis.encoding.TypeMappingDelegate@ef2c60
at org.apache.axis.encoding.SerializationContext.serializeActual(SerializationContext.java:1507)
at org.apache.axis.encoding.SerializationContext.serialize(SerializationContext.java:980)
at org.apache.axis.encoding.SerializationContext.outputMultiRefs(SerializationContext.java:1055)
at org.apache.axis.message.SOAPBody.outputImpl(SOAPBody.java:145)
at org.apache.axis.message.SOAPEnvelope.outputImpl(SOAPEnvelope.java:478)
at org.apache.axis.message.MessageElement.output(MessageElement.java:1208)
at org.apache.axis.client.Call.invoke(Call.java:2757)
at org.apache.axis.client.Call.invoke(Call.java:2443)
at org.apache.axis.client.Call.invoke(Call.java:2366)
at org.apache.axis.client.Call.invoke(Call.java:1812)

Update-2:

Here is an update on the problem that am facing. In the WSDL file, I found something like this,

complexType name="ArrayOfString" sequence element maxOccurs="unbounded" name="string" type="xsd:string" / /sequence /complexType 

Well, now that am meant to use this method,

Now, I tried sending the parameters 'attrVals','attibutes' like this

ArrayOfstring attrVals = new ArrayOfstring(); ArrayOfstring attributes = new ArrayOfstring(); attrVals.setString(new String[] < "customer", "test from soapUI", "customer", ticketHandle, "type", "I" >); attributes.setString(new String[] < "status", "ref_num" >); 

Its throwing the following exception

AxisFault faultCode: Server.userException faultSubcode: faultString: java.io.IOException: No serializer found for class org.tempuri.complex.data.arrays.xsd.ArrayOfstring in registry org.apache.axis.encoding.TypeMappingDelegate@11e1e67 faultActor: faultNode: faultDetail: stackTrace:java.io.IOException: No serializer found for class org.tempuri.complex.data.arrays.xsd.ArrayOfstring in registry org.apache.axis.encoding.TypeMappingDelegate@11e1e67 at org.apache.axis.encoding.SerializationContext.serializeActual(SerializationContext.java:1507) at org.apache.axis.encoding.SerializationContext.serialize(SerializationContext.java:980) 

Best Solution

The above error has been solved. I simply registered the ArrayOfString class, String[] with the TypeRegistryMapping class. Now, it doesn't throw the above serialize error. Code edited is:

 ServiceFactory factory1 = ServiceFactory.newInstance(); QName qnTick = new QName("http://soapinterop.org/xsd", "ArrayOfString"); Service serviceTickReq = factory1.createService(qnTick); // Service serviceTickReq = new org.apache.axis.client.Service(); TypeMappingRegistry tmr = (TypeMappingRegistry) serviceTickReq .getTypeMappingRegistry(); TypeMapping tm = (TypeMapping) tmr.getDefaultTypeMapping(); tm.register(ArrayOfString.class, qnTick, new BeanSerializerFactory( ArrayOfString.class, qnTick), new BeanDeserializerFactory( ArrayOfString.class, qnTick)); TypeMappingRegistry tmr1 = (TypeMappingRegistry) serviceTickReq .getTypeMappingRegistry(); TypeMapping tm1 = (TypeMapping) tmr1.getDefaultTypeMapping(); tm1.register(String[].class, qnTick, new BeanSerializerFactory( String[].class, qnTick), new BeanDeserializerFactory( String[].class, qnTick)); 
Java – How to create Axis Client without wsdl’s url

I had the same problem as yours. After digging for hours, it seems like I've almost solved this problem. This exception occurs because of missing set target endpoint address Here is my code

 Call call = service.createCall(); call.setPortTypeName(portQName); call.setOperationName(new QName(namespace, operation)); call.setProperty(Call.ENCODINGSTYLE_URI_PROPERTY, "http://schemas.xmlsoap.org/soap/encoding/"); call.setProperty(Call.OPERATION_STYLE_PROPERTY, "rpc"); call.addParameter("in0", org.apache.axis.Constants.XSD_STRING ,ParameterMode.IN); call.addParameter("in1", org.apache.axis.Constants.XSD_STRING ,ParameterMode.IN); call.setReturnType(serviceQName); String targetEndpoint = "http://113.160.19.218:8312/axis/services/WeatherForecastTest1"; call.setTargetEndpointAddress(targetEndpoint); String result = (String) call.invoke(params); out.println(result); 

The value of targetEndpoint agument is the value of location attribute of address element inside port element. Here is an example

Читайте также:  Css text decoration underline and underline

You can get this value by retrieving wsdl document using some wsdlParser (I use Axis's WSDL4J) (Note that in the code example above, I have hardcoded the targetEndpoint value)

Moreover, I set OPERATION_STYLE_PROPERTY to rpc style and ENCODINGSTYLE_URI_PROPERTY to http://schemas.xmlsoap.org/soap/encoding/ (this is default value) Here is the document I found to solve this problem

Hope that help you! Sorry for my bad English.

Java – getting exception while invoking a SOAP service on HTTPS with web security

You probably have checked the below link discussing the mustUnderstand error specifically in the context of Axis http://wso2.org/library/tutorials/understand-famous-did-not-understand-mustunderstand-header-s-error

Have you confirmed the entire SOAP envelope from your code and the one used by SOAP is the same? is mustUnderstand set to 1 in both the cases?

Related Question

Источник

Java take string array as parameter in java

Solution 2: You can even use variable arguments, like: Variable arguments are used to pass any number of arguments as parameters. I am quite sure that using Object is not the best way to go and you should use multiple methods with different arguments.

Passing Integer or String array as an argument in java

In java you can only pass the data defined in by the method. A way around this can be to use Object as an argument. Besides that you can create many methods with the same name, but accepting different arguments. Example:

public void example(String str, Object obj) < /* code */ >

When using Object you need to make sure to check what type it is, so you don't end up trying to use an integer as a string array.

Читайте также:  Nginx php 104 connection reset by peer

I am quite sure that using Object is not the best way to go and you should use multiple methods with different arguments.

You can overload the method to accept different parameters.

 void doSomething(String x, int[] y) <> void doSomething(String x, String[] y) <> void doSomething(String x, String y) <> 

That way, you still have type-safety (which an Object argument does not give you).

I suppose this is what you want

public void method1(String str, Object . val)

Inside the method body, you will have to handle different objects accordingly

Program to convert Array to Set in Java, Way 2: Using Arrays.asList() method: In this method, the Array is passed as the parameter into the Set constructor in the form of an Array with

Passing an Array as an Argument to a Method in Java By Example

When a single element of an array is passed to a method, it is handled like any other variable Duration: 4:09

How to Pass Array of Objects as Parameter in Java (Example)

How to pass array of objects as parameter in java (example) - We will see, How to pass array
Duration: 14:00

Invoking method that takes string array as parameter results in IllegalArgumentException

Observe that invoke has this signature:

public Object invoke(Object obj, Object. args) 

args is a variable-arity parameter to allow you to pass each parameter of the invoked method as a parameter of invoke , e.g.

someStaticMethod.invoke(null, param1, param2, param3); 
someStaticMethod(param1, param2, param3); 

On the other hand, the type of args is really just Object[] , and there exists a conversion from String[] to Object[] , since arrays are covariant.

So when you pass your String[] of strArr to invoke , one of two things could happen:

  • strArr is converted to an Object[] , and each String is treated as a parameter to the method to be invoked.
  • the whole strArr is treated as one of the parameter of the method to be invoked, just like a non-array type would.

The compiler just so happens to prefer the first one (see here. Note how variable-arity invocation has the lowest priority), so you are actually passing the parameters "1" , "2" , "3" to the method, rather than one single String[] .

Читайте также:  Iterator java для чего

On the other hand, int[] can't be converted to Object[] directly, because int is primitive, so the compiler can only choose the second option above.

One way to force the second option is to cast to Object :

clazz.getMethod("bar", String[].class).invoke(null, (Object)strArr); 

Another way is to create another Object[] wrapping the String[] :

clazz.getMethod("bar", String[].class).invoke(null, new Object [] < strArr >); 

You have to pass as first parameter of the invoke() method the initialized object that is going to execute that method.

Foo foo = new Foo(); Method method = clazz.getMethod("method");

The invoke() needs the object what is going to execute the method.

Java String Array, We can use Arrays.toString() method to convert String array to String. Note that array doesn't implement toString() method, so if you will try

How do I pass String Arrays between methods in Java?

Make public Lab(String input) to public Lab(String[] input) to take String array as input parameter.

You can even use variable arguments, like:

Variable arguments are used to pass any number of arguments as parameters. Though variable arguments are used when you give any number of arguments, use it carefully as it has certain limitations:

i. If you are going to have two or more arguments of same or different type, you must get the variable argument as the last parameter.

ii. You can not have more than one variable argument in your method or constructor.

You can not use variable args as return type, instead which you can use arrays. You can have variable arguments for any primitive types or any objects(like int. a or float. b or Employee. emp ). You can always use them just like arrays,i.e., you can access the elements by their index. I always use var args in my main method( public static void main(String. args)<> ).

Why we Pass String array as argument to main() method, why not, Also, command line arguments are Strings which is why that is the data type. Collections did not exist in Java 1 so they were not an option.

How to pass an array as argument in java (one line only)?

myfunction(new String[],"other argument"); 

How convert an array of Strings in a single String in java?, The toString() method of the Arrays class accepts a String array (in fact any array) and returns it as a String. Pass your String array to this

Источник

Оцените статью