简体   繁体   English

骆驼 SpringBoot POST 返回 Object 不是 JSON

[英]Camel SpringBoot POST returning Object NOT JSON

I am completely new to Camel, I have created two simple REST end points(using Camel 3.8.0 and SpringBoot 2.4.3), one GET and one POST, like this -我对 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()");

    }
}

When I call GET on http://localhost:8080/order I am getting an array of JSON(as expected), like this -当我在 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
  }
]

But, when I make a POST request on http://localhost:8080/order with input但是,当我在 http://localhost:8080/order 上使用输入发出 POST 请求时

{
    "name": "A4 Papers",
    "price": 55.5
}

Then it is returning Object , like -然后它返回 Object ,比如 -

Order(id=6, name=A4 Papers, price=55.5) Order(id=6, name=A4 Papers, price=55.5)

How can I make it to return JSON?我怎样才能让它返回 JSON? Like -喜欢 -

{
    "id": 6,
    "name": "A4 Papers",
    "price": 55.5
}

My pom.xml我的 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>

My complete code is he - https://github.com/crsardar/hands-on-java/tree/master/hands-on-camel-springboot我的完整代码是他 - https://github.com/crsardar/hands-on-java/tree/master/hands-on-camel-springboot

How can I make the POST API to return JSON?如何使 POST API 返回 JSON?

Your output data is a As you are using outType , your data which is an object of type Order is getting mapped into an arraylist.您的 output 数据是当您使用outType时,您的数据是 object 类型的 Order 将被映射到 arraylist。 You need to convert your data explicitly into json format.您需要将数据显式转换为 json 格式。

rest()
  .post("/order")
  .produces(MediaType.APPLICATION_JSON_VALUE)
  .type(Order.class)
  .route()
    .to("bean:orderService?method=addOrder(${body})")
    .marshal().json(JsonLibrary.Jackson);

Add marshaling before hitting API.在点击 API 之前添加编组。

rest().post("/order")
.produces(MediaType.APPLICATION_JSON_VALUE)
.marshal().json(JsonLibrary.Jackson, Order.class)
.to("bean:orderService?method=addOrder(${body})");

I pulled your project on the referenced Github url: https://github.com/crsardar/hands-on-java/tree/master/hands-on-camel-springboot and found that it throws an error at compilation time due to the camel-servlet-starter我在引用的 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>

在此处输入图像描述

I then proceeded to update it to use the following dependency:然后我继续更新它以使用以下依赖项:

<dependency>
   <groupId>org.apache.camel.springboot</groupId>
   <artifactId>camel-servlet-starter</artifactId>
   <version>${camel.version}</version>
</dependency>

Once I imported this dependency I was able to get the project running, and I tested the POST endpoint http://localhost:8080/camel/order and was getting the expected output一旦我导入了这个依赖项,我就可以让项目运行,我测试了POST端点http://localhost:8080/camel/order并得到了预期的 output

在此处输入图像描述

resulting in me not doing any code changes and yet getting the result you wanted, also one thing I've noticed in your yml file you use the /camel as your context-path yet in your above question you don't use in your rest call, leading me to believe that there is a project version mismatch.导致我没有做任何代码更改但得到你想要的结果,我在你的 yml 文件中注意到的一件事你使用/camel作为你的context-path ,但在你的上述问题中你没有在 rest 中使用打电话,让我相信有一个项目版本不匹配。

Based on your question and the Github project you referenced above and also the investigation I did, hope this assists.根据您的问题和您上面提到的 Github 项目以及我所做的调查,希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM