Java 将jaxbxmlpojo映射到postregressql XML类型column

Java 将jaxbxmlpojo映射到postregressql XML类型column,java,xml,hibernate,postgresql,jaxb,Java,Xml,Hibernate,Postgresql,Jaxb,我有一个javapojo,它有JAXB注释,这个类在一个控制器中被引用,我正在使用这个控制器尝试将XML(发布到端点)添加到postgresdb列中,我相信它是XML类型的 请注意,该项目使用Java7和Springboot JAXB POJO(订单类) 订单项类 OrderEntity是数据库列的Postgres表示形式 RestController 和订单存储库 公共接口OrderRepository扩展了JpaRepository{ } 我有一个application.propertie

我有一个javapojo,它有JAXB注释,这个类在一个控制器中被引用,我正在使用这个控制器尝试将XML(发布到端点)添加到postgresdb列中,我相信它是XML类型的

请注意,该项目使用Java7和Springboot

JAXB POJO(订单类) 订单项类 OrderEntity是数据库列的Postgres表示形式 RestController 和订单存储库 公共接口OrderRepository扩展了JpaRepository{ } 我有一个application.properties文件,其中包含我可以启动的本地postgres db的详细信息

然而,当发布到端点时,我就能够编译上面的代码了——http:localhost:8080/orders

curl -X POST --header "Content-Type: application/xml" --header "Accept: */*" -d 
<order>
    <customer>Bob Smith</customer>
    <orderItem>
        <sku>SB123</sku>
        <price>99.99</price>
    </orderItem>
</order>
" "localhost:8080/orders"
curl-X POST--header“Content-Type:application/xml”--header“Accept://*”-d
鲍勃·史密斯
SB123
99.99
“本地主机:8080/订单”
我得到以下信息

无法为类[class com.mands.springboot.jpapostgres.example.persistence.Order]实例化JAXBContext:1个IllegalAnnotationExceptions计数;嵌套异常为com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException:1个IllegalAnnotationExceptions计数\n类有两个同名属性“orderItem”“此问题与以下位置有关:public com.mands.springboot.jpapostgres.example.domain.OrderItem com.mands.springboot.jpapostgres.example.persistence.Order.getOrderItem()


欢迎任何建议-提前感谢。

您是否尝试将@XmlAccessorType(XmlAccessType.FIELD)添加到您的订单类中?是的,我已经这样做并重试了。我还从OrderEntity中的Order字段中删除了columnDefinition=“xml”,现在在尝试发布时看到这条消息:status:“500”,error:“Internal Server error”,“exception:“java.lang.NoSuchMethodError”,“message:“com.mands.springboot.jpapostgres.example.domain.OrderItem:”,“path:“orders”:“}看起来他试图调用一个没有参数的构造函数。能否尝试在OrderItem类中添加一个?另外,在OrderItem类中添加一个@XmlAccessorType(XmlAccessType.FIELD)。@DimpreJean-Sébastien您好,谢谢您的审阅!谢谢!我已经按照您的建议进行了尝试,重新编译并通过POST请求再次发送到端点(http:localhost:8080/orders),不过现在似乎出现了一个稍有不同的错误,即:status:500,“error”:“Internal Server error”,“exception”:“java.lang.ClassCastException”,“message”:不能将com.mands.springboot.jpapostgres.example.persistence.Order转换为java.lang.String,“path”:“/orders”我认为这是因为在OrderEntity中我定义了以下类型定义:@TypeDefs(value={@TypeDef(name=“xml”,typeClass=com.github.thealchesman.pg_hibernate.XMLType.class)})我试图将typeClass更改为定制的OrderUserType类,在该类中,我转换为Order而不是字符串。为了便于查看,我将代码放在Github上-感兴趣的文件是:src/main/java/com/mands/springboot/jpapostgres/example/domain/OrderEntity.java OrderItem.java和OrderUserType.java(仍然是WIP)
public class OrderItem {

  private String sku;

  private Double price;

  public OrderItem(final String sku, final Double price) {
    this.sku = sku;
    this.price = price;
  }

  public String getSku() {
    return sku;
  }

  public Double getPrice() {
    return price;
  }

}
import javax.persistence.*;

@TypeDefs(value =  {
        @TypeDef(name="xml", typeClass =     com.github.thealchemist.pg_hibernate.XMLType.class)
})
@Entity(name = "t_order")
public class OrderEntity {

public static final String NAME = "t_order";

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(name = "customer", nullable = false)
private String customer;

//    @Lob
//    @Column(name = "orderXml", nullable = false)
//    private String orderXml;

@Type(type = "xml")
@Column(name = "order_xml", columnDefinition = "xml")
private Order order;

public OrderEntity(final Order order) {
    this.customer = order.getCustomer();
    this.order = order;
}

public Long getId() {
  return id;
}

public Order getOrder() {
    return order;
}

public void setId(Long id) {
    this.id = id;
}

public String getCustomer() {
  return customer;
}

public void setCustomer(String customer) {
  this.customer = customer;
}
@RestController
@RequestMapping(value = "/", produces = MediaType.APPLICATION_JSON_VALUE)
public class HomeController {

@Autowired
OrderRepository orderRepository;

@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String sayHello() {
    return "Hello there !";
}

@RequestMapping(value = "/orders", method = RequestMethod.POST)
public OrderEntity create(@RequestBody final Order order) throws JAXBException   {
    StringWriter orderXml = new StringWriter();
    JAXB.marshal(order, orderXml);

    return orderRepository.save(new OrderEntity(order));
}
}
public interface OrderRepository extends JpaRepository<OrderEntity,Long> {

    }
curl -X POST --header "Content-Type: application/xml" --header "Accept: */*" -d 
<order>
    <customer>Bob Smith</customer>
    <orderItem>
        <sku>SB123</sku>
        <price>99.99</price>
    </orderItem>
</order>
" "localhost:8080/orders"