في بعض الحالات ، تحتاج الواجهة إلى إرجاع بيانات XML. في Springboot ، لا يحتاج إلى تحويل تنسيق البيانات في كل مرة ، فقط إجراء بعض التعديلات الطفيفة.
قم بإنشاء مشروع جديد لـ Springboot ، وأضف التبعية Jackson-Dataformat-XML ، ورمز ملف POM كما يلي:
<؟ XSI: schemalocation = "http://maven.apache.org/pom/4.0.0 <soph> 0.0.1-snapshot </version> <packaging> جرة </packaging> <name> springboot_xml </same> <speciped> springboot_xml </speciped> <parent> <roupiD> org.springframework.boot </rougeid> spring-boot-parent </splistid> <REPERANTERPARPHAPT/> <!-LANEUP PARTER من المستودع-> </inpart> <ferties> <project.build.sourceencoding> utf-8 </project.build.sourceencoding> <Project.reporting.outputing> uTF-8 </proport.reporting.outppouting> </properties> <ependencies> <redence> <roupiD> org.springframework.boot </rougeid> <intifactid> spring-boot-starter-web </stifactid> </redenced> <redenced> <roupiD> org.springframework.boot </groupid> <scope> وقت التشغيل </scope> </sependency> <redency> <roupiD> org.springframework.boot </groupiD> <StifactId> spring-boot-starter-test </stifactid> <StifactId> jackson-dataformat-xml </stifactid> </reperence> </reperencies> <build> <uccedins> <ultipin> <roupled> org.springframework.boot </ropraction> </sterfactid> spring-boot-boot-plugin </trictifactid>
فئة بدء التشغيل جاهزة بشكل افتراضي ، لا يتم إجراء أي تعديلات.
إنشاء فئة مستخدم جديدة ، الرمز كما يلي:
حزمة com.dalaoyang.entity ؛ استيراد javax.xml.bind.annotation.xmlelement [email protected] * date 2018/4/8 */@xmlrootelementpublic user {String username ؛ استخدام السلسلة ؛ سلسلة useraddress ؛ المستخدم العام (اسم المستخدم ، userage string ، string userAddress) {this.userName = username ؛ this.userage = userage ؛ this.useraddress = userAddress ؛ } xmlelement string public getUserName () {return username ؛ } public void setusername (string username) {this.userName = username ؛ } xmlelement string public getUserage () {return userage ؛ } public void setUserage (string userage) {this.userage = userage ؛ } xmlelement string public getUseraddress () {return userAddress ؛ } public void setUserAddress (String userAddress) {this.userAdDress = userAdDress ؛ }}
أخيرًا ، هناك وحدة تحكم ، الرمز كما يلي:
package com.dalaoyang.controller ؛ استيراد com.dalaoyang.entity.user ؛ استيراد org.springframework.http.mediaType ؛ استيراد org.springframework.web.bind.antmapping ؛ dalaoyang * description * project springboot_learn * package com.dalaoyang.controller * @eMail [email protected] * date 2018/4/8 */ @restControllerPublic class usercontroller "/json" ، تنتج = mediaType.application_json_value) فهرس المستخدم العام () {user user = new user ("dalaoyang" ، "26" ، "Beijing") ؛ إرجاع المستخدم ؛ } // http: // localhost: 8080/xmlgetMapping (value = "/xml" ، reduces = mediaType.application_xml_value) المستخدم العام xml () {user user = مستخدم جديد ("dalaoyang" ، "26" ، "Beijing") ؛ إرجاع المستخدم ؛ }}
يمكنك بدء المشروع هنا. تفضل بزيارة http: // localhost: 8080/json ويمكنك رؤية الصورة التالية
تفضل بزيارة http: // localhost: 8080/xml ، كما هو موضح في الشكل التالي
رمز المصدر تنزيل: https://gitee.com/dalaoyang/springboot_learn
ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.