草庐IT

java - JAXB 解码对嵌套对象始终具有空值

coder 2024-06-30 原文

我有一个网络服务,通过编写它的 WSDL 和底层 XSD 来定义,并且 java 服务器代码类/java 绑定(bind)是使用 JAXB/xjc 生成的。

一切看起来都很好服务正在正常运行......但是对于每个请求(在查看日志输出时收到后看起来格式正确)嵌套元素在通过我的 java 代码访问时似乎总是空的。

有人能弄清楚为什么 customerId.getCustomer() 总是返回 null 吗?

我的 XSD(部分):

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns:tip="http://example.org/tip" xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://example.org/tip/pro">

<complexType name="id">
  <attribute name="id" type="int" use="required"/>
  <attribute name="name" type="string" use="optional"/>
</complexType>

<complexType name="customer_id">
  <sequence>
    <element name="customer" type="tip:id" minOccurs="0"/>
  </sequence>
</complexType>

<element name="get_customer_request" type="tip:customer_id"/>

</schema>

生成的类CustomerId:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "customer_id", propOrder = {"customer"})
public class CustomerId {
  protected Id customer;

  public Id getCustomer() {
    return customer;
  }

  public void setCustomer(Id value) {
    this.customer = value;
  }
}

Id 生成的类看起来很相似,我认为没有什么特别的。 在我的请求处理程序中,我得到了以下摘录:

处理程序:

JAXBElement<?> request = requestHandler.unmarshallRequest(inputStream);
Object jaxbClass = request.getDeclaredType();
expectedClass = CustomerId.class;
// next line does not throw exception with given XML
if (jaxbClass != expectedClass) throw new IllegalArgumentException();

CustomerId customerId = (CustomerId)request.getValue();
if (customerId == null) {
  logInfo("customerId: null");
} else if (customerId.getCustomer() == null) {
  // this is the part that always will be executed... why?
  logInfo("customerId.customer: null");
} else {
  logInfo("customer id: " + customerId.getCustomer().getId());
  // return mbean.getCustomer(customerId);
}

最后是一个示例请求 XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<m:get_customer_request xmlns:m="http://example.org/tip/pro">
  <customer id="0" name="help"/>
</m:get_customer_request>

我去掉了 SOAP 信封和正文标签,因为这不会造成任何麻烦。 谁能看到我做错了什么? (我很确定,我确实……) 感谢您的努力!

最佳答案

第 1 部分

When I create a new Id and set customerId.customer with this, the full output is

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<get_customer_request xmlns="example.com/tip/pro">
    <customer name="xy" id="1"/>
</get_customer_request>

根据此信息,您的 JAXB 映射似乎希望 customer 元素位于 example.com/tip/pro 命名空间中,并且您的请求文档应该是:

<?xml version="1.0" encoding="ISO-8859-1"?>
<m:get_customer_request xmlns:m="http://example.org/tip/pro">
  <m:customer id="0" name="help"/>
</m:get_customer_request>

第 2 部分

When putting m: prefix to customer element in my request, the parser complains that he found m:customer and expected customer.

这意味着您的 XML 架构与您的映射不匹配。如果您希望 customer 元素位于命名空间中,您可以将 XML 架构更改为以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<schema 
    xmlns:tip="http://example.org/tip" 
    xmlns="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://example.org/tip/pro"
    elementFormDefault="qualified">

    ...

</schema>

有关 JAXB 和 namespace 的更多信息,请参阅:

关于java - JAXB 解码对嵌套对象始终具有空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12572998/

有关java - JAXB 解码对嵌套对象始终具有空值的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby-on-rails - 按天对 Mongoid 对象进行分组 - 2

    在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev

  3. ruby - 具有身份验证的私有(private) Ruby Gem 服务器 - 2

    我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..

  4. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  5. ruby - 将散列转换为嵌套散列 - 2

    这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[

  6. ruby-on-rails - 如何验证非模型(甚至非对象)字段 - 2

    我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss

  7. Ruby 写入和读取对象到文件 - 2

    好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信

  8. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  9. ruby-on-rails - 未在 Ruby 中初始化的对象 - 2

    我在Rails工作并有以下类(class):classPlayer当我运行时bundleexecrailsconsole然后尝试:a=Player.new("me",5.0,"UCLA")我回来了:=>#我不知道为什么Player对象不会在这里初始化。关于可能导致此问题的操作/解释的任何建议?谢谢,马里奥格 最佳答案 havenoideawhythePlayerobjectwouldn'tbeinitializedhere它没有初始化很简单,因为你还没有初始化它!您已经覆盖了ActiveRecord::Base初始化方法,但您没有调

  10. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

随机推荐