如何从Apache Camel中的Jetty端点发送JSON回复?

huangapple go评论84阅读模式
英文:

How to send JSON reply from a jetty endpoint in Apache Camel?

问题

I'm working on a pass-through REST service in Apache Camel. Exposed an end point with jetty component which invokes another REST end point which responds with JSON like below. But when I hit the exposed URL of Camel in a browser not getting desired output. Since I just started working on Camel, any help would be highly appreciated.

from("jetty:http://0.0.0.0:8080/api/camel/appoverview")
		.to("http4://10.150.60.237:80/api/itsb/applicationoverview?httpMethod=GET&bridgeEndpoint=true&throwExceptionOnFailure=false")
		.transform().simple("<out>${body}</out>")
		.log("Response sent -> ${body}");

Getting output -

<out> {
         "applicationId": "1",
         "applicationName": "NetInfo",
         "serviceNoticeCount": "13",
         "operationalStatus": {
            "id": "2",
            "status": "red",
            "statusLevel": "3"
         }
      }
</out>

Desired output -

 {
         "applicationId": "1",
         "applicationName": "NetInfo",
         "serviceNoticeCount": "13",
         "operationalStatus": {
            "id": "2",
            "status": "red",
            "statusLevel": "3"
         }
  }
英文:

I'm working on a pass-through REST service in Apache Camel. Exposed an end point with jetty component which invokes another REST end point which responds with JSON like below. But when I hit the exposed URL of Camel in a browser not getting desired output. Since I just started working on Camel, any help would be highly appreciated.

from(&quot;jetty:http://0.0.0.0:8080/api/camel/appoverview&quot;)
		.to(&quot;http4://10.150.60.237:80/api/itsb/applicationoverview?httpMethod=GET&amp;bridgeEndpoint=true&amp;throwExceptionOnFailure=false&quot;)
		.transform().simple(&quot;&lt;out&gt;${body}&lt;/out&gt;&quot;)
		.log(&quot;Response sent -&gt; ${body}&quot;);

Getting output -

&lt;out&gt; {
         &quot;applicationId&quot;: &quot;1&quot;,
         &quot;applicationName&quot;: &quot;NetInfo&quot;,
         &quot;serviceNoticeCount&quot;: &quot;13&quot;,
         &quot;operationalStatus&quot;: {
            &quot;id&quot;: &quot;2&quot;,
            &quot;status&quot;: &quot;red&quot;,
            &quot;statusLevel&quot;: &quot;3&quot;
         }
      }
&lt;/out&gt;

Desired output -

 {
         &quot;applicationId&quot;: &quot;1&quot;,
         &quot;applicationName&quot;: &quot;NetInfo&quot;,
         &quot;serviceNoticeCount&quot;: &quot;13&quot;,
         &quot;operationalStatus&quot;: {
            &quot;id&quot;: &quot;2&quot;,
            &quot;status&quot;: &quot;red&quot;,
            &quot;statusLevel&quot;: &quot;3&quot;
         }
  }

答案1

得分: 1

以下是您要翻译的部分:

"from("jetty:http://0.0.0.0:8080/api/camel/appoverview")
.to("http4://10.150.60.237:80/api/itsb/applicationoverview?httpMethod=GET&bridgeEndpoint=true&throwExceptionOnFailure=true")
.unmarshal().json(JsonLibrary.Jackson)
.transform().simple("${body}")
.log("Response sent -> ${body}");"

"unmarshalling needs to be added in pom.xml

&lt;dependency&gt;
			&lt;groupId&gt;org.apache.camel&lt;/groupId&gt;
			&lt;artifactId&gt;camel-jackson&lt;/artifactId&gt;
&lt;/dependency&gt;
```"

<details>
<summary>英文:</summary>

It worked after unmarshalling the JSON reply like below -

from("jetty:http://0.0.0.0:8080/api/camel/appoverview")
.to("http4://10.150.60.237:80/api/itsb/applicationoverview?httpMethod=GET&bridgeEndpoint=true&throwExceptionOnFailure=true")
.unmarshal().json(JsonLibrary.Jackson)
.transform().simple("${body}")
.log("Response sent -> ${body}");


Dependency for unmarshalling needs to be added in pom.xml

<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jackson</artifactId>
</dependency>


</details>



huangapple
  • 本文由 发表于 2020年8月5日 15:14:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63260164.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定