BAEL-4736 - Convert JSONArray to List of Object using camel-jackson (#10316)
* BAEL-4706 - Spring Boot with Spring Batch * BAEL-3948 - Fix test(s) in spring-batch which leaves repository.sqlite changed * BAEL-4736 - Convert JSONArray to List of Object using camel-jackson Co-authored-by: Jonathan Cook <jcook@sciops.esa.int>
This commit is contained in:
parent
eb29a0a084
commit
5e8cf2618a
@ -47,6 +47,17 @@
|
|||||||
<artifactId>camel-spring-javaconfig</artifactId>
|
<artifactId>camel-spring-javaconfig</artifactId>
|
||||||
<version>${env.camel.version}</version>
|
<version>${env.camel.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.camel</groupId>
|
||||||
|
<artifactId>camel-jackson</artifactId>
|
||||||
|
<version>${env.camel.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.camel</groupId>
|
||||||
|
<artifactId>camel-test</artifactId>
|
||||||
|
<version>${env.camel.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.baeldung.camel.jackson;
|
||||||
|
|
||||||
|
public class Fruit {
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
private int id;
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.baeldung.camel.jackson;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class FruitList {
|
||||||
|
|
||||||
|
private List<Fruit> fruits;
|
||||||
|
|
||||||
|
public List<Fruit> getFruits() {
|
||||||
|
return fruits;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFruits(List<Fruit> fruits) {
|
||||||
|
this.fruits = fruits;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,60 @@
|
|||||||
|
package com.baeldung.camel.jackson;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.camel.builder.RouteBuilder;
|
||||||
|
import org.apache.camel.component.jackson.ListJacksonDataFormat;
|
||||||
|
import org.apache.camel.component.mock.MockEndpoint;
|
||||||
|
import org.apache.camel.test.junit4.CamelTestSupport;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class FruitArrayJacksonUnmarshalUnitTest extends CamelTestSupport {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenJsonFruitArray_whenUnmarshalled_thenSuccess() throws Exception {
|
||||||
|
MockEndpoint mock = getMockEndpoint("mock:marshalledObject");
|
||||||
|
mock.expectedMessageCount(1);
|
||||||
|
mock.message(0).body().isInstanceOf(List.class);
|
||||||
|
|
||||||
|
String json = readJsonFromFile("/json/fruit-array.json");
|
||||||
|
template.sendBody("direct:jsonInput", json);
|
||||||
|
assertMockEndpointsSatisfied();
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
List<Fruit> fruitList = mock.getReceivedExchanges().get(0).getIn().getBody(List.class);
|
||||||
|
assertNotNull("Fruit lists should not be null", fruitList);
|
||||||
|
|
||||||
|
assertEquals("There should be two fruits", 2, fruitList.size());
|
||||||
|
|
||||||
|
Fruit fruit = fruitList.get(0);
|
||||||
|
assertEquals("Fruit name", "Banana", fruit.getName());
|
||||||
|
assertEquals("Fruit id", 100, fruit.getId());
|
||||||
|
|
||||||
|
fruit = fruitList.get(1);
|
||||||
|
assertEquals("Fruit name", "Apple", fruit.getName());
|
||||||
|
assertEquals("Fruit id", 101, fruit.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected RouteBuilder createRouteBuilder() throws Exception {
|
||||||
|
return new RouteBuilder() {
|
||||||
|
@Override
|
||||||
|
public void configure() throws Exception {
|
||||||
|
|
||||||
|
from("direct:jsonInput").unmarshal(new ListJacksonDataFormat(Fruit.class))
|
||||||
|
.to("mock:marshalledObject");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private String readJsonFromFile(String path) throws URISyntaxException, IOException {
|
||||||
|
URL resource = FruitArrayJacksonUnmarshalUnitTest.class.getResource(path);
|
||||||
|
return new String(Files.readAllBytes(Paths.get(resource.toURI())));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,59 @@
|
|||||||
|
package com.baeldung.camel.jackson;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.camel.builder.RouteBuilder;
|
||||||
|
import org.apache.camel.component.jackson.JacksonDataFormat;
|
||||||
|
import org.apache.camel.component.mock.MockEndpoint;
|
||||||
|
import org.apache.camel.test.junit4.CamelTestSupport;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class FruitListJacksonUnmarshalUnitTest extends CamelTestSupport {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenJsonFruitList_whenUnmarshalled_thenSuccess() throws Exception {
|
||||||
|
MockEndpoint mock = getMockEndpoint("mock:marshalledObject");
|
||||||
|
mock.expectedMessageCount(1);
|
||||||
|
mock.message(0).body().isInstanceOf(FruitList.class);
|
||||||
|
|
||||||
|
String json = readJsonFromFile("/json/fruit-list.json");
|
||||||
|
template.sendBody("direct:jsonInput", json);
|
||||||
|
assertMockEndpointsSatisfied();
|
||||||
|
|
||||||
|
FruitList fruitList = mock.getReceivedExchanges().get(0).getIn().getBody(FruitList.class);
|
||||||
|
assertNotNull("Fruit lists should not be null", fruitList);
|
||||||
|
|
||||||
|
List<Fruit> fruits = fruitList.getFruits();
|
||||||
|
assertEquals("There should be two fruits", 2, fruits.size());
|
||||||
|
|
||||||
|
Fruit fruit = fruits.get(0);
|
||||||
|
assertEquals("Fruit name", "Banana", fruit.getName());
|
||||||
|
assertEquals("Fruit id", 100, fruit.getId());
|
||||||
|
|
||||||
|
fruit = fruits.get(1);
|
||||||
|
assertEquals("Fruit name", "Apple", fruit.getName());
|
||||||
|
assertEquals("Fruit id", 101, fruit.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected RouteBuilder createRouteBuilder() throws Exception {
|
||||||
|
return new RouteBuilder() {
|
||||||
|
@Override
|
||||||
|
public void configure() throws Exception {
|
||||||
|
from("direct:jsonInput").unmarshal(new JacksonDataFormat(FruitList.class))
|
||||||
|
.to("mock:marshalledObject");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private String readJsonFromFile(String path) throws URISyntaxException, IOException {
|
||||||
|
URL resource = FruitListJacksonUnmarshalUnitTest.class.getResource(path);
|
||||||
|
return new String(Files.readAllBytes(Paths.get(resource.toURI())));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
10
spring-apache-camel/src/test/resources/json/fruit-array.json
Normal file
10
spring-apache-camel/src/test/resources/json/fruit-array.json
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"id": 100,
|
||||||
|
"name": "Banana"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 101,
|
||||||
|
"name": "Apple"
|
||||||
|
}
|
||||||
|
]
|
12
spring-apache-camel/src/test/resources/json/fruit-list.json
Normal file
12
spring-apache-camel/src/test/resources/json/fruit-list.json
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
{
|
||||||
|
"fruits": [
|
||||||
|
{
|
||||||
|
"id": 100,
|
||||||
|
"name": "Banana"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 101,
|
||||||
|
"name": "Apple"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user