將json對象轉為xml進行操作屬性
文章目錄
- 將json對象轉為xml進行操作屬性
- 前端發送json數據格式
- 寫入數據庫格式-content字段存儲(varchar(2000))
- Question實體類-接口映射對象
- QuestionContent 接收參數對象
- DAO持久層
- Mapper層
- Service層
- Controller控制層接收
- xml與對象互轉處理
- pom.xml引入
- 查詢功能-xml以json返回頁面
前端發送json數據格式
{"questionContent": {"title": "3. Fran正在構建一個取證分析工作站,并正在選擇一個取證磁盤控制器將其包含在設置中。 以下哪些是取證磁盤控制器的功能? (選擇所有適用的選項。)","choiceImgList": {},"choiceList": {"A": "A. 防止修改存儲設備上的數據","B": "B. 將數據返回給請求設備","C": "C. 將設備報告的錯誤發送給取證主機","D": "D. 阻止發送到設備的讀取命令"}}
}
寫入數據庫格式-content字段存儲(varchar(2000))
content字段落入庫中的格式
<QuestionContent><title>Lisa正在試圖防止她的網絡成為IP欺騙攻擊的目標,并防止她的網絡成為這些攻擊的源頭。 以下哪些規則是Lisa應在其網絡邊界配置的最佳實踐?(選擇所有適用的選項)
</title><titleImg></titleImg><choiceList><entry><string>A</string><string>阻止具有內部源地址的數據包進入網絡</string></entry><entry><string>B</string><string>阻止具有外部源地址的數據包離開網絡</string></entry><entry><string>C</string><string>阻止具有公共IP地址的數據包進入網絡</string></entry><entry><string>D</string><string> 阻止帶有私有IP地址的數據包離開網絡</string></entry></choiceList><choiceImgList/>
</QuestionContent>
Question實體類-接口映射對象
@XmlRootElement指定一個類為 XML 根元素。JAXB 是一種允許 Java 開發者將 Java 對象映射為 XML 表示形式,以及從 XML 還原為 Java 對象的技術。
Question 類被注解為 XML 根元素。當你使用 JAXB 序列化一個 Question 對象時,它將生成一個以 <Question> 為根元素的 XML 文檔import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Question implements Serializable {private String content;private QuestionContent questionContent;}
QuestionContent 接收參數對象
使用@XStreamAlias("QuestionContent")為類指定別名。例如,為QuestionContent類指定別名,當使用XStream序列化對象時,<QuestionContent類指定別名>將作為根元素,而<title>將作為name字段的元素名。import com.thoughtworks.xstream.annotations.XStreamAlias;@XStreamAlias("QuestionContent")
public class QuestionContent {@XStreamAlias("title")private String title;@XStreamAlias("titleImg")private String titleImg = "";@XStreamAlias("choiceList")private LinkedHashMap<String, String> choiceList;@XStreamAlias("choiceImgList")private LinkedHashMap<String, String> choiceImgList;public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getTitleImg() {return titleImg;}public void setTitleImg(String titleImg) {this.titleImg = titleImg;}public LinkedHashMap<String, String> getChoiceList() {return choiceList;}public void setChoiceList(LinkedHashMap<String, String> choiceList) {this.choiceList = choiceList;}public LinkedHashMap<String, String> getChoiceImgList() {return choiceImgList;}public void setChoiceImgList(LinkedHashMap<String, String> choiceImgList) {this.choiceImgList = choiceImgList;}}
DAO持久層
public interface QuestionMapper {public void insertQuestion(Question question) throws Exception;public void addQuestionKnowledgePoint(@Param("questionId") int questionId,@Param("pointId") int pointId) throws Exception;
}
Mapper層
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.extr.persistence.QuestionMapper"><insert id="addQuestionKnowledgePoint">insert into et_question_2_point(question_id,point_id)values(#{questionId},#{pointId})</insert><insert id="insertQuestion" parameterType="com.extr.domain.question.Question"useGeneratedKeys="true" keyProperty="id">insert into et_question(name,content,question_type_id,create_time,creator,answer,analysis,reference,examing_point,keygetQuestionListword,points)values(#{name},#{content},#{question_type_id},#{create_time},#{creator},#{answer},#{analysis},#{referenceName},#{examingPoint},#{keyword},#{points})</insert>
</mapper>
Service層
@Service("questionService")
public class QuestionServiceImpl implements QuestionService { @Autowiredprivate QuestionMapper questionMapper;@Override@Transactionalpublic void addQuestion(Question question) {// TODO Auto-generated method stubtry {questionMapper.insertQuestion(question);for (Integer i : question.getPointList()) {questionMapper.addQuestionKnowledgePoint(question.getId(), i);}} catch (Exception e) {throw new RuntimeException(e.getMessage());}}
}
Controller控制層接收
import com.extr.util.xml.Object2Xml;
@Controller
public class QuestionController {@RequestMapping(value = "/admin/questionAdd", method = RequestMethod.POST)public @ResponseBody Message addQuestion(@RequestBody Question question) {question.setContent(Object2Xml.toXml(question.getQuestionContent()));Message message = new Message();try {questionService.addQuestion(question);} catch (Exception e) {message.setResult("error");log.Info(e.getClass().getName());log.info(e);}return message;}
}
xml與對象互轉處理
package com.extr.util.xml;import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;public class Object2Xml {public static String toXml(Object obj){XStream xstream=new XStream();xstream.processAnnotations(obj.getClass());return xstream.toXML(obj);}public static <T> T toBean(String xmlStr,Class<T> cls){XStream xstream=new XStream(new DomDriver());xstream.processAnnotations(cls);@SuppressWarnings("unchecked")T obj=(T)xstream.fromXML(xmlStr);return obj;}
pom.xml引入
用于將Java對象序列化為XML,以及從XML反序列化為Java對象。它提供了一種直觀的方式來處理Java對象和XML之間的轉換,而無需編寫大量的映射代碼或配置。
<!-- Xstream --><dependency><groupId>com.thoughtworks.xstream</groupId><artifactId>xstream</artifactId><version>1.4.2</version></dependency>
查詢功能-xml以json返回頁面
public class QuestionAdapter {private QuestionContent questionContent;@GetMapping("/some-endpoint") public @ResponseBody String getQuestionContentAsJson((Question question)) { this.questionContent = Object2Xml.toBean(question.getContent(),QuestionContent.class);question.setQuestionContent(this.questionContent);try { return objectMapper.writeValueAsString(this.questionContent); } catch (Exception e) { // 處理異常并返回適當的錯誤響應 } // 如果沒有錯誤,但您仍然想返回一個默認的或空的JSON,您可以這樣做: return question; // 或任何其他您想要的默認JSON }
}