簡體   English   中英

評估簡單XPath表達式時發生異常

[英]Exception while evaluating simple XPath expression

嗨,我在嘗試編寫測試時遇到XPath表達式問題。 我有以下代碼片段。

final String resultCode = xPath.compile(
        "//*:Envelope/*:Body/ResultCode/text()")
        .evaluate(responseEntity.getBody());

responseEntity是由我的模擬返回的。 它由HttpStatus和xml格式的正確響應主體組成。 在執行測試時,出現此異常

由以下原因引起:javax.xml.xpath.XPathExpressionException:在net.sf處無法找到net.sf.saxon.xpath.XPathExpressionImpl.evaluate(XPathExpressionImpl.java:321)上類java.lang.String的節點的對象模型實現。 saxon.xpath.XPathExpressionImpl.evaluate(XPathExpressionImpl.java:396)...

我正在使用saxon來完成此任務,但是老實說,我對它不是很熟悉。 任何建議檢查什么都歡迎

好的,我知道了。 不幸的是,當您不得不在根本不了解的領域中修改某人的代碼時,就會發生這種情況。

問題是,在期望NodeInfoDOMDocument等之一的同時,將字符串傳遞給了evaluate方法。

也感謝@paul trmbrth修復了格式錯誤的xpath表達式。

我將代碼更改為如下所示:

InputSource source = new InputSource(new StringReader(responseEntity.getBody()));
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
final Document document = db.parse(source);

final String resultCode = xPath.compile(
        "//*[local-name()='Envelope']/*[local-name()='Body']/*[local-name()='ResultCode']/text()")
        .evaluate(document);

嘗試迭代列表時出現“異常評估 SpringEL 表達式”錯誤(<object> ) 在 Thymeleaf Spring 啟動<div id="text_translate"><p>我是 spring 引導的新手,並嘗試使用 JPA/JPQL 在兩個不相關的實體(事務和項目)之間建立連接。 但是在使用 thymeleaf 以表格格式顯示它時出現以下錯誤:</p><pre> 2020-04-08 21:42:42.463 ERROR 73816 --- [nio-8080-exec-4] oac.c.C.[.[.[/].[dispatcherServlet]: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "transactiondetail.tid" (template: "index" - line 27, col 21)] with root cause org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'tid' cannot be found on object of type 'java.lang.Object[]' - maybe not public or not valid?</pre><p> 以下是我的 thymeleaf 實現:</p><pre> &lt;tr th:each="transactiondetail: ${listTransactionDetail}"&gt; &lt;td th:text="${transactiondetail.tid}"&gt;TID&lt;/td&gt; &lt;td th:text="${transactiondetail.from_uid}"&gt;From UID&lt;/td&gt; &lt;td th:text="${transactiondetail.to_uid}"&gt;To UID&lt;/td&gt; &lt;td th:text="${transactiondetail.item_name}"&gt;Item Name&lt;/td&gt; &lt;td th:text="${transactiondetail.item_count}"&gt;Count&lt;/td&gt; &lt;td th:text="${transactiondetail.status}"&gt;Status&lt;/td&gt;</pre><p> 以下是 controller:</p><pre> @Autowired QueryService queryservice; @RequestMapping("/") public String viewHomePage(Model model) { List&lt;TransactionDetail&gt; listTransactionDetail = queryservice.JPQLQuery(); model.addAttribute("listTransactionDetail", listTransactionDetail); return "index"; }</pre><p> 當我簡單地返回queryservice.JPQLQuery()時,我能夠得到結果,這意味着加入和列表正在工作。 以下是 controller 正在調用的服務:</p><pre> @Service public class QueryService implements IQueryService { @Autowired EntityManagerFactory emf; @Override public List&lt;TransactionDetail&gt; JPQLQuery() { EntityManager em = emf.createEntityManager(); Query query = em.createQuery("Select s.tid,s.from_uid,s.to_uid,d.item_name,s.item_count,s.status from Transaction s inner join Item d on s.item_id=d.item_id"); @SuppressWarnings("unchecked") List&lt;TransactionDetail&gt; tranlist = (List&lt;TransactionDetail&gt;)query.getResultList(); em.close(); return tranlist; } }</pre><p> 下面顯示的是 TransactionDetail class,它正在接收連接的實體。 如您所見,我有具有正確名稱的構造函數和 getter/setter。 仍然無法正確顯示詳細信息。</p><pre> public class TransactionDetail { private Long tid; private int from_uid; private int to_uid; private String item_name; private int item_count; private int status; public TransactionDetail() { } public TransactionDetail(Long tid, int from_uid, int to_uid, String item_name, int item_count, int status) { super(); this.tid = tid; this.from_uid = from_uid; this.to_uid = to_uid; this.item_name = item_name; this.item_count = item_count; this.status = status; } public Long getTid() { return tid; } public void setTid(Long tid) { this.tid = tid; } public int getFrom_uid() { return from_uid; } public void setFrom_uid(int from_uid) { this.from_uid = from_uid; } public int getTo_uid() { return to_uid; } public void setTo_uid(int to_uid) { this.to_uid = to_uid; } public int getItem_count() { return item_count; } public void setItem_count(int item_count) { this.item_count = item_count; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } public String getItem_name() { return item_name; } public void setItem_name(String item_name) { this.item_name = item_name; } }</pre><p> 我猜這個問題與我將結果從query.getResultList()保存到List&lt;TransactionDetail&gt;的方式有關。 那么知道如何解決這個問題嗎? 提前致謝!</p></div></object>

[英]“Exception evaluating SpringEL expression” error while trying to iterate List(<Object>) in Thymeleaf Spring Boot

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 評估Xpath時InputStream異常 在 Java 中評估 XPath 表達式時出現異常 在Java中評估XPath表達式的問題 評估XPath XmlResourceParser導致異常 Spring 異常評估 SpringEL 表達式 將XPath表達式與簡單XML結合使用 嘗試迭代列表時出現“異常評估 SpringEL 表達式”錯誤(<object> ) 在 Thymeleaf Spring 啟動<div id="text_translate"><p>我是 spring 引導的新手,並嘗試使用 JPA/JPQL 在兩個不相關的實體(事務和項目)之間建立連接。 但是在使用 thymeleaf 以表格格式顯示它時出現以下錯誤:</p><pre> 2020-04-08 21:42:42.463 ERROR 73816 --- [nio-8080-exec-4] oac.c.C.[.[.[/].[dispatcherServlet]: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "transactiondetail.tid" (template: "index" - line 27, col 21)] with root cause org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'tid' cannot be found on object of type 'java.lang.Object[]' - maybe not public or not valid?</pre><p> 以下是我的 thymeleaf 實現:</p><pre> &lt;tr th:each="transactiondetail: ${listTransactionDetail}"&gt; &lt;td th:text="${transactiondetail.tid}"&gt;TID&lt;/td&gt; &lt;td th:text="${transactiondetail.from_uid}"&gt;From UID&lt;/td&gt; &lt;td th:text="${transactiondetail.to_uid}"&gt;To UID&lt;/td&gt; &lt;td th:text="${transactiondetail.item_name}"&gt;Item Name&lt;/td&gt; &lt;td th:text="${transactiondetail.item_count}"&gt;Count&lt;/td&gt; &lt;td th:text="${transactiondetail.status}"&gt;Status&lt;/td&gt;</pre><p> 以下是 controller:</p><pre> @Autowired QueryService queryservice; @RequestMapping("/") public String viewHomePage(Model model) { List&lt;TransactionDetail&gt; listTransactionDetail = queryservice.JPQLQuery(); model.addAttribute("listTransactionDetail", listTransactionDetail); return "index"; }</pre><p> 當我簡單地返回queryservice.JPQLQuery()時,我能夠得到結果,這意味着加入和列表正在工作。 以下是 controller 正在調用的服務:</p><pre> @Service public class QueryService implements IQueryService { @Autowired EntityManagerFactory emf; @Override public List&lt;TransactionDetail&gt; JPQLQuery() { EntityManager em = emf.createEntityManager(); Query query = em.createQuery("Select s.tid,s.from_uid,s.to_uid,d.item_name,s.item_count,s.status from Transaction s inner join Item d on s.item_id=d.item_id"); @SuppressWarnings("unchecked") List&lt;TransactionDetail&gt; tranlist = (List&lt;TransactionDetail&gt;)query.getResultList(); em.close(); return tranlist; } }</pre><p> 下面顯示的是 TransactionDetail class,它正在接收連接的實體。 如您所見,我有具有正確名稱的構造函數和 getter/setter。 仍然無法正確顯示詳細信息。</p><pre> public class TransactionDetail { private Long tid; private int from_uid; private int to_uid; private String item_name; private int item_count; private int status; public TransactionDetail() { } public TransactionDetail(Long tid, int from_uid, int to_uid, String item_name, int item_count, int status) { super(); this.tid = tid; this.from_uid = from_uid; this.to_uid = to_uid; this.item_name = item_name; this.item_count = item_count; this.status = status; } public Long getTid() { return tid; } public void setTid(Long tid) { this.tid = tid; } public int getFrom_uid() { return from_uid; } public void setFrom_uid(int from_uid) { this.from_uid = from_uid; } public int getTo_uid() { return to_uid; } public void setTo_uid(int to_uid) { this.to_uid = to_uid; } public int getItem_count() { return item_count; } public void setItem_count(int item_count) { this.item_count = item_count; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } public String getItem_name() { return item_name; } public void setItem_name(String item_name) { this.item_name = item_name; } }</pre><p> 我猜這個問題與我將結果從query.getResultList()保存到List&lt;TransactionDetail&gt;的方式有關。 那么知道如何解決這個問題嗎? 提前致謝!</p></div></object> 評估SpringEL表達式的異常,Spring Guru課程 評估SpringEL表達式的異常 - 由於集合而導致的錯誤? Spring Boot Exception評估SpringEL表達式
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM