2017-09-21 1 views
0

XML을 POJO로 구문 분석하는 데 Apache Camel을 사용하려고하는데 실제 POJO를 인쇄하는 데 문제가 있습니다. 대신 변환이 일어나지 않는 것처럼 일반 XML을 얻습니다. 예를 들어 고객의 고객 인스 트루먼트를 통과하면 정상적으로 작동합니다. 또한 콩 클래스에서 sout으로 Customers 클래스를 인쇄하는 것도 완벽하게 짖는다.Camel은 Java DSL 경로에서 POJO를 인쇄하지 않습니다.

MyRoute 
    @Autowired 
    private MyBean mb; 
    @Override 
    public void configure() throws Exception { 
     from("file:{{customer.path}}?noop=true") 
       .bean(mb) 
       .to("stream:out"); 
    } 


MyBean 

    @Handler 
    public Customers whatIsInBody(Customers body) { 
     return body; 
    } 

POJO 클래스 :

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "customer" 
}) 
@XmlRootElement(name = "customers") 
public @Data 
class Customers { 

    @XmlElement(required = true, nillable = true) 
    protected List<Customer> customer; 

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "customer", propOrder = { 
    "id", 
    "name", 
    "adress", 
    "countryCode", 
    "products" 
}) 
public @Data 
class Customer { 

    protected long id; 
    @XmlElement(required = true) 
    protected String name; 
    @XmlElement(required = true) 
    protected String adress; 
    @XmlElement(required = true) 
    protected String countryCode; 
    @XmlElement(required = true, nillable = true) 
    protected List<Product> products; 
} 

출력 예 :

<customers> 
    <customer> 
     <id>12345</id> 
     <name>str1234</name> 
     <adress>str1234</adress> 
     <countryCode>str1234</countryCode> 
     <products> 
      <id>12345</id> 
      <name>str1234</name> 
     </products> 
    </customer> 

원하는 출력 : 당신은 클래스 패스에 다음 POJO를 낙타 JAXB 한

Customers(customer=[Customer(id=12345, name=str1234, adress=str1234, countryCode=str1234, products=[Product(id=12345, name=str1234)]), 
+0

당신은 낙타의 데이터 형식을 보았 느냐? Jackson XML 및 자동 언 마샬링을 수행 할 수있는 데이터 형식이 내장되어 있습니다. http://camel.apache.org/jackson-xml.html –

+0

@SoucianceEqdamRashti 별도의 메인에서 jaxb를 사용해 보았습니다. jaxb가 JAXBContext 인 jc = JAXBContext 인 .unmarshal (jaxb)을 추가하려고하면 괜찮습니다. .newInstance (Customers.class); DataFormat jaxb = 새로운 JaxbDataFormat (jc); 하지만 경로에 추가하면 아무 일도 일어나지 않습니다 ... – Eugene

+0

당신이 여기에 설명 된 것을 좋아하니? https://stackoverflow.com/questions/26800182/jaxb-marshalling-in-apache-camel –

답변

0

JAXB 주석이있는 클래스 stream:out이 수행하는 것 인 String 유형으로 변환 할 때 유형이 XML로 변환됩니다. 당신이 정말로 스트리밍하려면

당신은 그래서 http://camel.apache.org/tracer

라우팅 중에 메시지에 포함 된 내용을 확인하기 위해 추적을 활성화 할 수 있습니다 : 메시지 본문의 toString, 당신은 호출하여 먼저 수동으로 변환 할 필요가 밖으로 그 toString 방법

.transform(simple("${body.toString()}")) 
+0

Bean의 서명을 변경하여 String을 반환하고 body.toString()을 호출하면 동일합니다. "to"의 구성 요소가 Java 객체와 함께 작동 할 수 있으면 대화가 없습니다. – Eugene

+0

네 Bean이 String을 반환하고'return body.toString()'을 호출하면'stream : out'이 그대로 사용하게된다 –

+0

왜이 heppens를 understending하는 것을 도와 줄 수 있습니까? https://stackoverflow.com/questions/46447191/cant-add-application-prop-to-camel-test – Eugene

관련 문제