gpt4 book ai didi

java - 以纯文本形式获取 XML

转载 作者:搜寻专家 更新时间:2023-11-01 02:57:07 26 4
gpt4 key购买 nike

我有这个 Spring Rest API 端点:

@PostMapping(value = "/v1/", consumes = { MediaType.APPLICATION_XML_VALUE,
MediaType.APPLICATION_JSON_VALUE }, produces = { MediaType.APPLICATION_XML_VALUE,
MediaType.APPLICATION_JSON_VALUE })
public PaymentResponse handleMessage(@RequestBody PaymentTransaction transaction, HttpServletRequest request) throws Exception {

// get here plain XML

}

XML 模型。

@XmlRootElement(name = "payment_transaction")
@XmlAccessorType(XmlAccessType.FIELD)
public class PaymentTransaction {
public enum Response {
failed_response, successful_response
}

@XmlElement(name = "transaction_type")
public String transactionType;
.........
}

如何获取纯 XML 文本的 XML 请求?

我也尝试过使用 Spring 拦截器:我试过这段代码:

@SpringBootApplication
@EntityScan("org.plugin.entity")
public class Application extends SpringBootServletInitializer implements WebMvcConfigurer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
........

@Bean
public RestTemplate rsestTemplate() {
List<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
RestTemplate restTemplate = new RestTemplate(
new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));
restTemplate.setInterceptors(interceptors);
return restTemplate;
}
}

记录组件:

@Component
public class RestTemplateHeaderModifierInterceptor implements ClientHttpRequestInterceptor {

@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
throws IOException {

StringBuilder sb = new StringBuilder();
sb.append("[ ");
for (byte b : body) {
sb.append(String.format("0x%02X ", b));
}
sb.append("]");

System.out.println("!!!!!!!!!!!!!!!");
System.out.println(sb.toString());

ClientHttpResponse response = execution.execute(request, body);

InputStream inputStream = response.getBody();

String result = IOUtils.toString(inputStream, StandardCharsets.UTF_8);

System.out.println("!!!!!!!!!!!!!!!");
System.out.println(result);

return response;
}
}

但是控制台中没有打印任何内容。知道我哪里错了吗?可能这个组件没有注册?

最佳答案

从 HttpServletRequest 获取它不应该像下面那样容易吗,除非我遗漏了什么。我认为没有必要使用拦截器等。

@PostMapping(value = "/v1/", consumes = { MediaType.APPLICATION_XML_VALUE,
MediaType.APPLICATION_JSON_VALUE }, produces = { MediaType.APPLICATION_XML_VALUE,
MediaType.APPLICATION_JSON_VALUE })
public PaymentResponse handleMessage(HttpServletRequest request) throws Exception {

String str, wholeXML = "";
try {
BufferedReader br = request.getReader();
while ((str = br.readLine()) != null) {
wholeXML += str;
}
System.out.println(wholeXML);
//Here goes comment question, to convert it into PaymentTransaction
JAXBContext jaxbContext = JAXBContext.newInstance(PaymentTransaction.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

StringReader reader = new StringReader(wholeXML);
PaymentTransaction paymentTransaction = (PaymentTransaction) unmarshaller.unmarshal(reader);
}

关于java - 以纯文本形式获取 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53450695/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com