372.3. Java DSL을 사용한 기본 사용
372.3.1. 명시적으로 데이터 형식 인스턴스화
org.apache.camel.dataformat.xmljson
패키지에서 ECDHE JsonDataFormat
을 인스턴스화하기만 하면 됩니다. camel-xmljson
기능 (OSGi에서 실행중인 경우)을 설치하거나 classpath에 camel-xmljson-7.11.jar
및 전송 종속 항목을 포함했는지 확인하십시오. 기본 구성을 사용한 초기화 예:
XmlJsonDataFormat xmlJsonFormat = new XmlJsonDataFormat();
위의 옵션에 따라 데이터 형식의 동작을 조정하려면 적절한 집합을 사용합니다.
XmlJsonDataFormat xmlJsonFormat = new XmlJsonDataFormat(); xmlJsonFormat.setEncoding("UTF-8"); xmlJsonFormat.setForceTopLevelObject(true); xmlJsonFormat.setTrimSpaces(true); xmlJsonFormat.setRootName("newRoot"); xmlJsonFormat.setSkipNamespaces(true); xmlJsonFormat.setRemoveNamespacePrefixes(true); xmlJsonFormat.setExpandableProperties(Arrays.asList("d", "e"));
데이터 형식을 인스턴스화한 후 다음 단계는 실제로 marshal()
및 unmarshal()
DSL 요소 내에서 해당 형식을 사용하는 것입니다.
// from XML to JSON from("direct:marshal").marshal(xmlJsonFormat).to("mock:json"); // from JSON to XML from("direct:unmarshal").unmarshal(xmlJsonFormat).to("mock:xml");
372.3.2. 데이터 형식 인라인 정의
또는 xmljson()
DSL 요소를 사용하여 인라인으로 데이터 형식을 정의할 수 있습니다.
// from XML to JSON - inline dataformat from("direct:marshalInline").marshal().xmljson().to("mock:jsonInline"); // from JSON to XML - inline dataformat from("direct:unmarshalInline").unmarshal().xmljson().to("mock:xmlInline");
원하는 경우 Map<String, String
>을 인라인 메서드에 전달하여 사용자 지정 옵션을 제공할 수 있습니다.
Map<String, String> xmlJsonOptions = new HashMap<String, String>(); xmlJsonOptions.put(org.apache.camel.model.dataformat.XmlJsonDataFormat.ENCODING, "UTF-8"); xmlJsonOptions.put(org.apache.camel.model.dataformat.XmlJsonDataFormat.ROOT_NAME, "newRoot"); xmlJsonOptions.put(org.apache.camel.model.dataformat.XmlJsonDataFormat.SKIP_NAMESPACES, "true"); xmlJsonOptions.put(org.apache.camel.model.dataformat.XmlJsonDataFormat.REMOVE_NAMESPACE_PREFIXES, "true"); xmlJsonOptions.put(org.apache.camel.model.dataformat.XmlJsonDataFormat.EXPANDABLE_PROPERTIES, "d e"); // from XML to JSON - inline dataformat w/ options from("direct:marshalInlineOptions").marshal().xmljson(xmlJsonOptions).to("mock:jsonInlineOptions"); // form JSON to XML - inline dataformat w/ options from("direct:unmarshalInlineOptions").unmarshal().xmljson(xmlJsonOptions).to("mock:xmlInlineOptions");