![](/img/trans.png)
[英]Adjust the object before returning the JSON response in Apache Camel with Jackson
[英]Camel SpringBoot POST returning Object NOT JSON
我对 Camel 完全陌生,我创建了两个简单的 REST 端点(使用 Camel 3.8.0 和 SpringBoot 2.4.3),一个 GET 和一个 POST,就像这样 -
@Component
public class CamelController extends RouteBuilder {
@Override
public void configure() throws Exception {
restConfiguration()
.component("servlet")
.port(8080)
.host("127.0.0.1")
.bindingMode(RestBindingMode.json);
rest().post("/order")
.produces(MediaType.APPLICATION_JSON_VALUE)
.type(Order.class)
.outType(Order.class)
.to("bean:orderService?method=addOrder(${body})");
rest().get("/order")
.produces(MediaType.APPLICATION_JSON_VALUE)
.to("bean:orderService?method=getOrders()");
}
}
当我在 http://localhost:8080/order 上调用 GET 时,我得到一个 JSON 数组(如预期的那样),如下所示 -
[
{
"id": 1,
"name": "Pencil",
"price": 100.0
},
{
"id": 2,
"name": "Pen",
"price": 300.0
},
{
"id": 3,
"name": "Book",
"price": 350.0
}
]
但是,当我在 http://localhost:8080/order 上使用输入发出 POST 请求时
{
"name": "A4 Papers",
"price": 55.5
}
然后它返回 Object ,比如 -
Order(id=6, name=A4 Papers, price=55.5)
我怎样才能让它返回 JSON? 喜欢 -
{
"id": 6,
"name": "A4 Papers",
"price": 55.5
}
我的 pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
<relativePath/>
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<camelVersion>3.8.0</camelVersion>
</properties>
<dependencies>
<!-- SpringBoot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Camel -->
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-bom</artifactId>
<version>${camelVersion}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jackson</artifactId>
<version>${camelVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-servlet-starter</artifactId>
<version>${camelVersion}</version>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>${camelVersion}</version>
</dependency>
<!-- Others -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.18</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
我的完整代码是他 - https://github.com/crsardar/hands-on-java/tree/master/hands-on-camel-springboot
如何使 POST API 返回 JSON?
您的 output 数据是当您使用outType
时,您的数据是 object 类型的 Order 将被映射到 arraylist。 您需要将数据显式转换为 json 格式。
rest()
.post("/order")
.produces(MediaType.APPLICATION_JSON_VALUE)
.type(Order.class)
.route()
.to("bean:orderService?method=addOrder(${body})")
.marshal().json(JsonLibrary.Jackson);
在点击 API 之前添加编组。
rest().post("/order")
.produces(MediaType.APPLICATION_JSON_VALUE)
.marshal().json(JsonLibrary.Jackson, Order.class)
.to("bean:orderService?method=addOrder(${body})");
我在引用的 Github url 上提取了您的项目: https://github.com/crsardar/hands-on-java/spring-boot/master/hands在编译时抛出错误camel-servlet-starter
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-servlet-starter</artifactId>
<version>${camel.version}</version>
</dependency>
然后我继续更新它以使用以下依赖项:
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-servlet-starter</artifactId>
<version>${camel.version}</version>
</dependency>
一旦我导入了这个依赖项,我就可以让项目运行,我测试了POST
端点http://localhost:8080/camel/order
并得到了预期的 output
导致我没有做任何代码更改但得到你想要的结果,我在你的 yml 文件中注意到的一件事你使用/camel
作为你的context-path
,但在你的上述问题中你没有在 rest 中使用打电话,让我相信有一个项目版本不匹配。
根据您的问题和您上面提到的 Github 项目以及我所做的调查,希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.