/jax-rs-js

mirrors JAX-RS annotated services in JavaScript

Primary LanguageJava

What is jax-rs-js?

jax-rs-js aims to help accessing JAX-RS annotated services from within a browser application. Complete coverage of JAX-RS features is not a goal of jax-rs-js. Instead some common and straightforward patterns are covered. See ch.mbae.notes.services.NotesService in the test source directory to see whats currently covered.

jax-rs-js currently relies on jquery on the browser side.

jax-rs-js is shipped with a Servlet to be included in a web application and to serve JavaScript files for JAX-RS services:

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Note addNote(Note note) {
    return note;
}

is called in JavaScript like this:

jaxjs.services.NotesService.addNote(
  {
    title: "Sample note",
    note: "push changes on github"
  },
  function(note) {
    // do something with service response in callback function
  }
);

See https://github.com/marcbaechinger/jax-rs-js-web for a working example (integration tests).

Build and install

Build with maven

mvn clean install

Include dependency in your pom.xml

<dependency>
  <groupId>ch.mbae</groupId>
  <artifactId>jax-rs-js</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>

or copy jax-rs-js-1.0-SNAPSHOT.jar to your environment.

Servlet configuration in web.xml

A servlet generates and serves a JavaScript file for classes defined with init params:


<servlet>
    <servlet-name>JaxRsJsServlet</servlet-name>
    <servlet-class>ch.mbae.jaxjs.container.JaxRsJsServlet</servlet-class>
    <!-- required: path to the JSX-RS servlet -->
    <init-param>
        <param-name>jaxrs.servlet.path</param-name>
        <param-value>/resources</param-value>
    </init-param>
    <init-param>
        <param-name>library.default</param-name>
        <param-value>
            ch.mbae.notes.services.NotesService, com.foo.services.BarService
        </param-value>
    </init-param>
    <init-param>
        <param-name>config.minification</param-name>
        <param-value>false</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>JaxRsJsServlet</servlet-name>
    <url-pattern>/resources-js/*</url-pattern>
</servlet-mapping>

Include JavaScript in the HTML page

<script src="<%= request.getContextPath() %>/resources-js/"> </script>

<script>
$(function() {
  jaxjs.services.NotesService.getAll(function(notes) {
     // render notes...
  });
);
</script>