{"id":1407,"date":"2023-03-19T11:32:16","date_gmt":"2023-03-19T03:32:16","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1407"},"modified":"2023-04-28T21:08:14","modified_gmt":"2023-04-28T13:08:14","slug":"spring-boot-receives-and-responds-to-xml","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/19\/spring-boot-receives-and-responds-to-xml\/","title":{"rendered":"Spring Boot\u63a5\u6536\u5e76\u54cd\u5e94xml"},"content":{"rendered":"<h2>\u4f9d\u8d56<\/h2>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\n    &lt;groupId&gt;com.fasterxml.jackson.dataformat&lt;\/groupId&gt;\n    &lt;artifactId&gt;jackson-dataformat-xml&lt;\/artifactId&gt;\n    &lt;version&gt;2.9.7&lt;\/version&gt;\n&lt;\/dependency&gt;<\/code><\/pre>\n<p><!-- more --><\/p>\n<h2>\u914d\u7f6eXML\u8f6c\u6362\u5668<\/h2>\n<pre><code class=\"language-java\">@Configuration\npublic class MessageConverterConfig extends WebMvcConfigurationSupport {\n\n    @Override\n    public void configureMessageConverters(List&lt;HttpMessageConverter&lt;?&gt;&gt; converters) {\n        Jackson2ObjectMapperBuilder builder = Jackson2ObjectMapperBuilder.xml();\n        builder.indentOutput(true);\n        converters.add(new MappingJackson2XmlHttpMessageConverter(builder.build()));\n    }\n}<\/code><\/pre>\n<h2>Controller\u5c42<\/h2>\n<pre><code class=\"language-java\">@RequestMapping(value = &quot;\/creditLoanApplyNotify&quot;, method = RequestMethod.POST, consumes = MediaType.APPLICATION_XML_VALUE, produces = MediaType.APPLICATION_XML_VALUE)\n@ResponseBody\npublic String creditLoanApplyNotify(@RequestBody CreditLoanApplyNotifyDto req) {\n    \/\/ \u81ea\u5df1\u903b\u8f91<\/code><\/pre>\n<h2>\u54cd\u5e94\u7684XML\u6ca1\u6709xml\u5934<\/h2>\n<p>\u4f8b\u5982\uff1a<code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<\/code><br \/>\n\u8d77\u4f5c\u7528\u7684\u662f\uff1a<code>xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);<\/code><\/p>\n<pre><code class=\"language-java\">XmlMapper xmlMapper = new XmlMapper();\nxmlMapper.setDefaultUseWrapper(false);\nxmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);\nString requestBody = xmlMapper.writeValueAsString(req);<\/code><\/pre>\n<h2>\u5bf9\u4e8e\u590d\u6742\u7684xml<\/h2>\n<p>\u6bd4\u5982xml\uff1a<\/p>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\n&lt;document&gt;\n    &lt;response&gt;\n        &lt;head&gt;\n            &lt;appId&gt;wx0001&lt;\/appId&gt;\n            &lt;function&gt;apply.notify&lt;\/function&gt;\n            &lt;inputCharset&gt;UTF-8&lt;\/inputCharset&gt;\n            &lt;reqMsgId&gt;201803300309471966&lt;\/reqMsgId&gt;\n            &lt;reserve&gt;1.0.0.20180621&lt;\/reserve&gt;\n            &lt;respTime&gt;201909191&lt;\/respTime&gt;\n            &lt;respTimeZone&gt;UTC+8&lt;\/respTimeZone&gt;\n            &lt;signType&gt;RSA&lt;\/signType&gt;\n            &lt;version&gt;1.0.0&lt;\/version&gt;\n        &lt;\/head&gt;\n        &lt;body&gt;\n            &lt;applyNo&gt;2018033&lt;\/applyNo&gt;\n            &lt;requestId&gt;A&lt;\/requestId&gt;\n            &lt;resultInfo&gt;\n                &lt;resultCode&gt;000&lt;\/resultCode&gt;\n                &lt;resultMsg&gt;sign check fail&lt;\/resultMsg&gt;\n                &lt;retry&gt;N&lt;\/retry&gt;\n            &lt;\/resultInfo&gt;\n        &lt;\/body&gt;\n    &lt;\/response&gt;\n    &lt;signature&gt;arfLohOxOIL0BOry==&lt;\/signature&gt;\n&lt;\/document&gt;<\/code><\/pre>\n<p>\u5904\u7406\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-java\">@JacksonXmlRootElement(localName = &quot;document&quot;)\n@Data\npublic class CreditLoanApproveAckNotifyDto {\n    @JacksonXmlElementWrapper(localName =&quot;response&quot;)\n    private Response response;\n    @JacksonXmlProperty(localName = &quot;signature&quot;)\n    private String signature;\n\n    @Data\n    public class Response {\n        @JacksonXmlElementWrapper(localName =&quot;head&quot;)\n        private Header head;\n        @JacksonXmlElementWrapper(localName =&quot;body&quot;)\n        private NotifyDomain body;\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4f9d\u8d56 &lt;dependency&gt; &lt;groupId&gt;com.fasterxml.jack [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[],"class_list":["post-1407","post","type-post","status-publish","format-standard","hentry","category-spring-boot"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1407","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1407"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1407\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}