简体   繁体   English

发送 JSON 没有标识符的请求体 SPRING 引导

[英]Send JSON request body without identifiers SPRING Boot

I am consuming a legacy rest service that receives xml text in the body but is sent as json as shown in the following image我正在使用旧版 rest 服务,该服务在正文中接收 xml 文本,但作为 json 发送,如下图所示

Postman source request Postman 来源请求

I have done the process to transform the previous request to a request that receives a JSON in normal format New JSON request我已经完成了将以前的请求转换为以正常格式接收 JSON新请求 JSON 的请求的过程

And then I transform it into the format that asks me for the source request, my problem is that I do not know how to send my string request because I get the following error when I try to send it Request error new http request然后我将它转换成要求我提供源请求的格式,我的问题是我不知道如何发送我的字符串请求,因为当我尝试发送它时出现以下错误Request error new http request

I get the same error when I sent in my source request in plain text format Plain text error legacy http request当我以纯文本格式发送源请求时出现相同的错误Plain text error legacy http request

however in code I already transformed this text into JSON format but probably incorrectly,in the following sample code the http entity is the xml string object required by the legacy service但是在代码中我已经将此文本转换为 JSON 格式但可能不正确,在以下示例代码中 http 实体是遗留服务所需的 xml 字符串 object

@Value("${client.medExpInsuranceQuotation.uri}")
private String clientUri;

@Autowired
@Qualifier("restTemplategetPolicyWs")
private RestTemplate restTemplate;

@Override
public Object callMedicalExpenseInsuranceQuotation(MedicalExpenseInsuranceQuotationRequestClient requestClient) {
    
    Gson gson = new Gson();
    Object json = gson.toJson(requestClient.getXml(), String.class);
    System.out.println("Object: "+json); 
    
    HttpEntity<Object> entity = new HttpEntity<>(json);
    log.info(requestClient.getXml());
    ResponseEntity<ResponseBean<Object>> responseEntity = restTemplate.exchange(clientUri, HttpMethod.POST, entity, new ParameterizedTypeReference<ResponseBean<Object>>() {
    });
    
    return responseEntity.getBody().getPayload();
}

Notes: The legacy service only receives the body in the format indicated in image 1 (Postman source request)注意:遗留服务仅接收图像 1 中指示的格式的正文(邮递员来源请求)

if I try to send the body in traditional json format with an attribute identifier attemp send normal json format如果我尝试以传统的 json 格式发送带有属性标识符的正文尝试发送正常的 json 格式

I get the following error without information我在没有信息的情况下收到以下错误

{
    "Message": "An error has occurred."
}

I hope you can help me greetings我希望你能帮我问候

That rest service is consuming json format. rest 服务正在使用 json 格式。 Note the double quotes at the start and the end of the payload - this makes it json string .请注意有效负载开头和结尾的双引号 - 这使其成为json string Not json object , or json array , but json string .不是json objectjson array ,而是json string You need to transform the xml payload into json string and send the correct content type header - application/json .您需要将xml有效载荷转换为 json 字符串并发送正确的内容类型 header - application/json

Edit: To get json string first you need the xml as a string(it's already a string, i know that sounds strange, but can't explain it better).编辑:要首先获得json string ,您需要xml作为字符串(它已经是一个字符串,我知道这听起来很奇怪,但无法更好地解释)。 Then you call toJson() on that xml. Something similar to this:然后你在那个 xml 上调用toJson() 。类似这样的东西:

String xml = "<xml><tag></tag></xml>";
String jsonString = gson.toJson(xml);
System.out.println(jsonString);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM