parent
2d07aff6b2
commit
5ba204bd59
@ -1,2 +1,5 @@
|
||||
server:
|
||||
port: 9090
|
||||
camel:
|
||||
springboot:
|
||||
main-run-controller: true
|
@ -0,0 +1,91 @@
|
||||
== Spring Boot Example with Camel exposing REST services using Apache CXF
|
||||
|
||||
=== Introduction
|
||||
|
||||
This example illustrates how to use https://projects.spring.io/spring-boot/[Spring Boot] with http://camel.apache.org[Camel]. It provides a simple REST service that is created using https://cxf.apache.org/[Apache CXF].
|
||||
|
||||
|
||||
=== Build
|
||||
|
||||
You can build this example using:
|
||||
|
||||
$ mvn package
|
||||
|
||||
=== Run
|
||||
|
||||
You can run this example using:
|
||||
|
||||
$ mvn spring-boot:run
|
||||
|
||||
After the Spring Boot application is started, you can open the following URL in your web browser to access the list of services: http://localhost:8080/services/ including WADL definition
|
||||
|
||||
You can also access the REST endpoint from the command line:
|
||||
|
||||
List all the users
|
||||
[source,text]
|
||||
----
|
||||
$ curl http://localhost:8080/services/api/user -s | jq .
|
||||
----
|
||||
|
||||
The command will produce the following output:
|
||||
|
||||
[source,json]
|
||||
----
|
||||
[ {
|
||||
"id" : 1,
|
||||
"name" : "John Coltrane"
|
||||
}, {
|
||||
"id" : 2,
|
||||
"name" : "Miles Davis"
|
||||
}, {
|
||||
"id" : 3,
|
||||
"name" : "Sonny Rollins"
|
||||
} ]
|
||||
----
|
||||
|
||||
Retrieve a specific user
|
||||
[source,text]
|
||||
----
|
||||
$ curl http://localhost:8080/services/api/user/1 -s | jq .
|
||||
----
|
||||
|
||||
The command will produce the following output:
|
||||
|
||||
[source,json]
|
||||
----
|
||||
{
|
||||
"id": 1,
|
||||
"name": "John Coltrane"
|
||||
}
|
||||
----
|
||||
|
||||
Insert/update user
|
||||
|
||||
[source,text]
|
||||
----
|
||||
$ curl -X PUT http://localhost:8080/services/api/user --data '{"id":4,"name":"Charlie Parker"}' -H 'Content-Type: application/json' -v
|
||||
----
|
||||
|
||||
The http status code of the response will be https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml#http-status-codes-1[201]
|
||||
|
||||
Moreover, the input user is validated according to the annotations on the link:src/main/java/org/apache/camel/example/springboot/cxf/User.java[User bean]
|
||||
|
||||
[source,text]
|
||||
----
|
||||
$ curl -X PUT http://localhost:8080/services/api/user --data '{"id":4,"name":"C"}' -H 'Content-Type: application/json'
|
||||
----
|
||||
|
||||
will produce a validation error
|
||||
|
||||
|
||||
The Spring Boot application can be stopped pressing `[CTRL] + [C]` in the shell.
|
||||
|
||||
=== Help and contributions
|
||||
|
||||
If you hit any problem using Camel or have some feedback, then please
|
||||
https://camel.apache.org/community/support/[let us know].
|
||||
|
||||
We also love contributors, so
|
||||
https://camel.apache.org/community/contributing/[get involved] :-)
|
||||
|
||||
The Camel riders!
|
@ -1,90 +0,0 @@
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns:camel="http://camel.apache.org/schema/spring"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
|
||||
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
|
||||
|
||||
<camel:camelContext xmlns="http://camel.apache.org/schema/spring">
|
||||
<route id="example-http-inbound">
|
||||
<from uri="jetty:http://0.0.0.0:9090/example" />
|
||||
<convertBodyTo type="java.lang.String" />
|
||||
<log message="[EXAMPLE INBOUND] Received: ${body}" />
|
||||
<choice>
|
||||
<when>
|
||||
<simple>${headers.CamelHttpMethod} == 'POST'</simple>
|
||||
<setHeader name="type">
|
||||
<jsonpath>$.notification.type</jsonpath>
|
||||
</setHeader>
|
||||
<choice>
|
||||
<when>
|
||||
<simple>${header.type} == 'email'</simple>
|
||||
<log message="[EXAMPLE INBOUND] Received email notification" />
|
||||
<to uri="direct:email" />
|
||||
<setHeader name="Exchange.HTTP_RESPONSE_CODE">
|
||||
<constant>200</constant>
|
||||
</setHeader>
|
||||
</when>
|
||||
<when>
|
||||
<simple>${header.type} == 'http'</simple>
|
||||
<log message="[EXAMPLE INBOUND] Received http notification" />
|
||||
<to uri="direct:http" />
|
||||
<setHeader name="Exchange.HTTP_RESPONSE_CODE">
|
||||
<constant>200</constant>
|
||||
</setHeader>
|
||||
</when>
|
||||
<otherwise>
|
||||
<log message="[EXAMPLE INBOUND] Unknown notification" />
|
||||
<setBody>
|
||||
<constant>{ "status": "reject", "type": "unknown" }</constant>
|
||||
</setBody>
|
||||
<setHeader name="Exchange.HTTP_RESPONSE_CODE">
|
||||
<constant>400</constant>
|
||||
</setHeader>
|
||||
</otherwise>
|
||||
</choice>
|
||||
</when>
|
||||
<otherwise>
|
||||
<log
|
||||
message="[EXAMPLE INBOUND] only POST is accepted (${headers.CamelHttpMethod})" />
|
||||
<setBody>
|
||||
<constant>{ "error": "only POST is accepted" }</constant>
|
||||
</setBody>
|
||||
<setHeader name="Exchange.HTTP_RESPONSE_CODE">
|
||||
<constant>500</constant>
|
||||
</setHeader>
|
||||
</otherwise>
|
||||
</choice>
|
||||
</route>
|
||||
<route id="example-email">
|
||||
<from uri="direct:email" />
|
||||
<log message="[EXAMPLE EMAIL] Sending notification email" />
|
||||
<setHeader name="to">
|
||||
<jsonpath>$.notification.to</jsonpath>
|
||||
</setHeader>
|
||||
<setHeader name="subject">
|
||||
<constant>Notification</constant>
|
||||
</setHeader>
|
||||
<setBody>
|
||||
<jsonpath>$.notification.message</jsonpath>
|
||||
</setBody>
|
||||
<!-- <to uri="smtp://localhost"/> -->
|
||||
<setBody>
|
||||
<simple>{ "status": "email sent", "to": "${header.to}", "subject":
|
||||
"${header.subject}" }</simple>
|
||||
</setBody>
|
||||
</route>
|
||||
<route id="example-http">
|
||||
<from uri="direct:http" />
|
||||
<log message="[EXAMPLE HTTP] Sending http notification" />
|
||||
<setHeader name="service">
|
||||
<jsonpath>$.notification.service</jsonpath>
|
||||
</setHeader>
|
||||
<!-- <to uri="jetty:..." /> -->
|
||||
<setBody>
|
||||
<simple>{ "status": "http requested", "service": "${header.service}"
|
||||
}</simple>
|
||||
</setBody>
|
||||
</route>
|
||||
</camel:camelContext>
|
||||
</beans>
|
Loading…
Reference in new issue