0

I have an existing Java web service which will access an Java object and return a byte array after processing. It is currently consumed by another Java application and it is working fine.

Recently I planned to consume the same Java web service using Python application but keep getting "NONE" respond from the Java Web Service even though I'm setting the same attribute value as my Java application . I'm not sure what is the actual root cause but I suspect it is due to the object type.

Do I need to convert my Python object to Java object before calling the Web Service?

Below is my sample python code:

from zeep import Client
from TestObject import TestObject 

wsdl = "http://xxxxxxxx.com:8002/xxxx/xxxxx?WSDL"
client = Client(wsdl)

TestObject = TestObject()

TestObject.setAttribute1("1")
TestObject.setAttribute2("2")
TestObject.setAttribute3("3")

response = client.service.methodtocall(TestObject)

print(response)

I have no access to the Java Web service so the only changes I can make is on my Python code. Please let me know what options do I have.

CYLEE
  • 1
  • 1

3 Answers3

0

Found out zeep client actually have already done the conversion job. Zeep client knew what is the object and already break down into attributes. So we just have to pass in the attribute values as parameter.

We just need to mention the parameter name:

response = client.service.methodtocall(Attribute1 = "1", Attribute2 = "2", Attribute3 = "3")

CYLEE
  • 1
  • 1
0

the Cause

  1. it is conspicuous that the object which is sent to web service is the data of Json,so you can't send the python object directly to Java Service

solveing

Fistly,using python’s api convert the python object to json

json.dumps(your python ogject,other param...)

Secondly,using the json object to request the service;

huz.han
  • 11
  • 1
0

In the general case, the first thing you need to do is to look at the documentation for the web service's API to find out what it expects.

If the Python Zeep client has solved the problem for you, we can deduce that you are actually talking to a SOAP / WSDL based web service, and requests and responses should therefore be encoded as XML. Zeep works by querying the service's WSDL to figure out how request and response XML should be structured, and then maps your supplied request parameters to the corresponding XML.

This Q&A lists a number of alternative Python libraries for "doing" SOAP.


And to answer the question in the title.

How to convert Python class to Java object

It is in general not possible to do this.

A web service almost certainly does not expect a "Java object" ... because arbitrary Java objects are not in general transmissible. It simply does not make sense.

But if you were talking to a (non-web) service with an RMI-based API, then you are relying on Java's Object Serialization Protocol. Mapping the JOSP to directly to Python would be extremely challenging (and awkward / unnatural on the Python side). I don't think anyone has done it ... and since RMI is "yesterday's technology" it is probably better not to try. (A better idea would be to implement a protocol bridge service in Java that translated RMI to a RESTful API of some kind. Or replace the RMI-based service.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216