![](/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.