Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
mirrors JAX-RS annotated services in JavaScript
branch: master

This branch is even with marcbaechinger:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
src
.gitignore
.project
README.markdown
jax-rs-js-1.0-SNAPSHOT.jar
nbactions.xml
pom.xml

README.markdown

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>

Something went wrong with that request. Please try again.