I have a program that queries a REST service in python, and the Java server that processes the requests.
python
import requests
import json
from requests.auth import HTTPBasicAuth
headers={'Content-type':'application/json', 'Accept':'application/json'}
#Parametros peticion
parametros={
"llamada":"S",
"backup":{
"fecha":"2019-11-14T00:00:00",
"codigo":"1W78-55155W8S"
},
"usuario":"MK001"
}
r = requests.post(
'http://localhost:8080/appapi/calculo/eje1',
data=json.dumps(parametros),
auth=HTTPBasicAuth('user', 'pass'),
headers=headers
)
In the Java part:
@POST
@Path("/eje1")
@Consumes(MediaType.APPLICATION_JSON)
public Response setCalc(HashMap<String,String> object) {
System.out.println(object);
return Response.status(200).entity("").build();
}
When we execute the JSON
parametros={
"llamada":"S",
"backup":{
"fecha":"2019-11-14T00:00:00",
"codigo":"1W78-55155W8S"
},
"usuario":"MK001"
}
The parameters that come with a value are only llamada
and usuario
, but backup
it comes to me empty.
Do you know how to mount that particular Json? I can't modify the Java part.
In java the terms Map -> JSON are very confusing. I don't recommend you do that. You should transform it into an object using mapping or into a String, since you want to work in an open way and without being influenced by the model, I recommend the org.json library
https://www.baeldung.com/java-org-json
EDITION:
For the python part put a string of String, instead of the object, but then in the back of java you will have to reparse it.
If you can't modify the Java part, modify the data to match something digestible by that controller. Flatten the JSON, for example (only one level deep is supported):