Restful web service with maven – continuation

What is new

This post is continuation of previous one about restful web service:
First restful web service with maven.
Process of extending functionality is reflected in google.code repository. Each major code change is bound into a commit, and there are tags, make it easier to see which parts of code are responsible for which functionality.
I have created it basing on the tutorial on REST with Java (JAX-RS) using Jersey – Tutorial.
Project has also client unit tests included.
Below there is a list of changes that each tag introduce.


Tag v1.1.0

Under this tag project has changed structure – split into two projects: restful-service (server side implementation) and test-client (client side unit tests implementation).
Some unit tests are written but they fail.


Tag v1.1.1

Extending NotesHandler so all the test comes through.
Implementing better way of handling GET requests using JAXB. By this change all XML and JSON requests could be handled in one method.

@XmlRootElement
public class Note {

	private String id;
	private String name;
	private String content;
...
...
	private Note note;

	public NotesHandler() {
		note = new Note("1");
		note.setName("Hello world");
		note.setContent("This is the first note");
	}

	@GET
	@Produces({ MediaType.TEXT_XML, MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
	public Note answer() {
		return note;
	}
...


Tag v1.1.2

Implementing Rest PUT and DELETE with unit test covering all the functionality.

	@PUT
	@Consumes(MediaType.APPLICATION_XML)
	public Response putContent(JAXBElement<Note> element) {
		Note note = element.getValue();
		Response res;
		if (NotesDao.getInstance().containsKey(note.getId())) {
			res = Response.noContent().build();
		} else {
			res = Response.created(uriInfo.getAbsolutePath()).build();
		}
		NotesDao.getInstance().put(note);
		return res;
	}
	
	@DELETE
	public void deleteContent() {
		Note note = NotesDao.getInstance().remove(id);
		if (note == null){
			throw new RuntimeException("Delete: Note with " + id + " not found");
		}
	}


Tag v1.1.3

Implementing Rest POST method that can be invoked by Form.

	@POST
	@Produces(MediaType.TEXT_HTML)
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public void newNote(@FormParam("id") String id, @FormParam("name") String name,
			@FormParam("content") String content, @Context HttpServletResponse servletResponse) throws IOException {
		Note note = new Note(id);
		note.setName(name);
		note.setContent(content);
		NotesDao.getInstance().put(note);
	}

Adding unit test to cover this functionality.


Important links

  • REST with Java (JAX-RS) using Jersey – Tutorial – Lars Vogel’s article about creating a restful web service.
  • First restful web service with maven – previous post on my blog, to which this one is a continuation
  • restful-webservice-training – git repository on google.code from where you can clone a project

  • Reklamy

    2 uwagi do wpisu “Restful web service with maven – continuation

    Skomentuj

    Wprowadź swoje dane lub kliknij jedną z tych ikon, aby się zalogować:

    Logo WordPress.com

    Komentujesz korzystając z konta WordPress.com. Wyloguj / Zmień )

    Zdjęcie z Twittera

    Komentujesz korzystając z konta Twitter. Wyloguj / Zmień )

    Facebook photo

    Komentujesz korzystając z konta Facebook. Wyloguj / Zmień )

    Google+ photo

    Komentujesz korzystając z konta Google+. Wyloguj / Zmień )

    Connecting to %s