blob: a3b62a77f7d70e5580426aec4bfa6a3d1c9c5a5e [file] [log] [blame]
:virgo-homepage: https://eclipse.org/virgo
:guide-short-name: rest-service
:recipe-name: Building RESTful Web Service
:recipe-short-name: recipe-{guide-short-name}
include::../../../../recipe-template/src/docs/asciidoc/00_title.adoc[]
include::../../../../recipe-template/src/docs/asciidoc/01_acknowledgements.adoc[]
== {recipe-name}
The original guide uses https://projects.spring.io/spring-boot/[Spring Boot] to bootstrap the demo application. This guide shows what needs to be done to get the "{recipe-name}" up and running on Virgo.
include::../../../../recipe-template/src/docs/asciidoc/03_shopping-list.adoc[]
include::../../../../recipe-template/src/docs/asciidoc/04_ingredients.adoc[]
* Spring configuration
* Web application configuration
* OSGi manifest
== Preparations
include::../../../../recipe-template/src/docs/asciidoc/051_get-the-code.adoc[]
include::../../../../recipe-template/src/docs/asciidoc/052_create-recipe-runtime.adoc[]
include::../../../../recipe-template/src/docs/asciidoc/053_create-eclipse-project-metadata.adoc[]
include::../../../../recipe-template/src/docs/asciidoc/054_prepare-virgo-tooling.adoc[]
include::../../../../recipe-template/src/docs/asciidoc/055_import-the-code.adoc[]
include::../../../../recipe-template/src/docs/asciidoc/056_create-new-virgo-server.adoc[]
== Directions
Virgo has no support for Spring's `@EnableAutoConfiguration`, so you have to be more explicit than in the Spring Boot version of this guide - after all, Virgo is an OSGi runtime and every bundle needs to declare its needs in the `META-INF/MANIFEST.MF`.
Basically, you have to specify necessary dependencies (via `Import-Package` or `Import-Bundle` headers) and the `Web-ContextPath` the REST service application wants to be published on.
.template.mf
[source,txt]
----
include::../../../org.eclipse.virgo.samples.recipe.restservice/template.mf[]
----
You can mix and match the Virgo specific header `Import-Bundle` and OSGi standard header `Import-Package` within the `MANIFEST.MF`. In the sample the fine-tuning of the generated `MANIFEST.MF` is done within `template.mf`.
NOTE: If you intend to run your bundles in different OSGi containers you have to limit yourself to the OSGi standard headers.
Next step is to configure the servlet container via `WEB-INF/web.xml`:
[source,xml]
.web.xml
----
include::../../../org.eclipse.virgo.samples.recipe.restservice/src/main/resources/WEB-INF/web.xml[]
----
NOTE: The most important parameter is the `contextClass` which is *not* the default from the Spring Framework, but `org.eclipse.virgo.web.dm.ServerOsgiBundleXmlWebApplicationContext`.
[source,xml]
----
<context-param>
<param-name>contextClass</param-name>
<param-value>org.eclipse.virgo.web.dm.ServerOsgiBundleXmlWebApplicationContext</param-value>
</context-param>[]
----
Finally the Spring web application context.:
[source,xml]
----
include::../../../org.eclipse.virgo.samples.recipe.restservice/src/main/resources/WEB-INF/spring/appServlet/servlet-context.xml[]
----
Within the `servlet-context.xml` we define a single message converter `jacksonMessageConverter` using Jackson 2:
[source,xml]
.servlet-context.xml
----
<message-converters register-defaults="false">
<beans:bean id="jackson2MessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</message-converters>
----
== Let's taste
Start the Virgo server inside the Springsource Tool Suite to appreciate what you build.
With `curl` and the command line simply run the following command within the project's directory:
----
$ curl -i -H "Accept: application/json" 'http://localhost:8080/restservice-guide/greeting?name=test'
----
You should see an output like:
....
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/json
Transfer-Encoding: chunked
Date: Mon, 12 Oct 2015 11:06:50 GMT
{"id":2,"content":"Hello, test!"}%
....
include::../../../../recipe-template/src/docs/asciidoc/08_dockerize_recipe.adoc[]