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

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

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.

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

Getting output -

  1. <out> {
  2. "applicationId": "1",
  3. "applicationName": "NetInfo",
  4. "serviceNoticeCount": "13",
  5. "operationalStatus": {
  6. "id": "2",
  7. "status": "red",
  8. "statusLevel": "3"
  9. }
  10. }
  11. </out>

Desired output -

  1. {
  2. "applicationId": "1",
  3. "applicationName": "NetInfo",
  4. "serviceNoticeCount": "13",
  5. "operationalStatus": {
  6. "id": "2",
  7. "status": "red",
  8. "statusLevel": "3"
  9. }
  10. }
英文:

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.

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

Getting output -

  1. &lt;out&gt; {
  2. &quot;applicationId&quot;: &quot;1&quot;,
  3. &quot;applicationName&quot;: &quot;NetInfo&quot;,
  4. &quot;serviceNoticeCount&quot;: &quot;13&quot;,
  5. &quot;operationalStatus&quot;: {
  6. &quot;id&quot;: &quot;2&quot;,
  7. &quot;status&quot;: &quot;red&quot;,
  8. &quot;statusLevel&quot;: &quot;3&quot;
  9. }
  10. }
  11. &lt;/out&gt;

Desired output -

  1. {
  2. &quot;applicationId&quot;: &quot;1&quot;,
  3. &quot;applicationName&quot;: &quot;NetInfo&quot;,
  4. &quot;serviceNoticeCount&quot;: &quot;13&quot;,
  5. &quot;operationalStatus&quot;: {
  6. &quot;id&quot;: &quot;2&quot;,
  7. &quot;status&quot;: &quot;red&quot;,
  8. &quot;statusLevel&quot;: &quot;3&quot;
  9. }
  10. }

答案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

  1. &lt;dependency&gt;
  2. &lt;groupId&gt;org.apache.camel&lt;/groupId&gt;
  3. &lt;artifactId&gt;camel-jackson&lt;/artifactId&gt;
  4. &lt;/dependency&gt;
  5. ```"
  6. <details>
  7. <summary>英文:</summary>
  8. 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}");

  1. Dependency for unmarshalling needs to be added in pom.xml

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

  1. </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:

确定