开发运行环境:NetBeans7.3.1。
这里使用的是JAX-RS 2.0的特性,关于JAX-RS 2.0的介绍,请参考《JavaEE7 十大新特性》。
1. MovieClientBean.java
MovieClientBean.java是RESTful Web Service的Client端。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.glassfish.movieplex7.client;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import org.glassfish.movieplex7.entities.Movie;
import org.glassfish.movieplex7.json.MovieWriter;
/**
*
* @author pmma
*/
@Named
@RequestScoped
public class MovieClientBean {
Client client;
WebTarget target;
@Inject
MovieBackingBean bean;
@PostConstruct
public void init() {
client = ClientBuilder.newClient();
target = client.target(
"http://localhost:8989/movieplex7/webresources/movie/");
}
@PreDestroy
public void destroy() {
client.close();
}
public Movie[] getMovies() {
return target
.request()
.get(Movie[].class);
}
public Movie getMovie() {
Movie m = target
.path("{movie}")
.resolveTemplate("movie", bean.getMovieId())
.request()
.get(Movie.class);
return m;
}
public void deleteMovie() {
target
.path("{movieId}")
.resolveTemplate("movieId", bean.getMovieId())
.request()
.delete();
}
public void addMovie() {
Movie m = new Movie();
m.setId(bean.getMovieId());
m.setName(bean.getMovieName());
m.setActors(bean.getActors());
target
.register(MovieWriter.class)
.request()
.post(Entity.entity(m, MediaType.APPLICATION_JSON));
}
}
这里使用的是JAX-RS 2.0的特性,关于JAX-RS 2.0的介绍,请参考《JavaEE7 十大新特性》。
1. MovieClientBean.java
MovieClientBean.java是RESTful Web Service的Client端。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.glassfish.movieplex7.client;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import org.glassfish.movieplex7.entities.Movie;
import org.glassfish.movieplex7.json.MovieWriter;
/**
*
* @author pmma
*/
@Named
@RequestScoped
public class MovieClientBean {
Client client;
WebTarget target;
@Inject
MovieBackingBean bean;
@PostConstruct
public void init() {
client = ClientBuilder.newClient();
target = client.target(
"http://localhost:8989/movieplex7/webresources/movie/");
}
@PreDestroy
public void destroy() {
client.close();
}
public Movie[] getMovies() {
return target
.request()
.get(Movie[].class);
}
public Movie getMovie() {
Movie m = target
.path("{movie}")
.resolveTemplate("movie", bean.getMovieId())
.request()
.get(Movie.class);
return m;
}
public void deleteMovie() {
target
.path("{movieId}")
.resolveTemplate("movieId", bean.getMovieId())
.request()
.delete();
}
public void addMovie() {
Movie m = new Movie();
m.setId(bean.getMovieId());
m.setName(bean.getMovieName());
m.setActors(bean.getActors());
target
.register(MovieWriter.class)
.request()
.post(Entity.entity(m, MediaType.APPLICATION_JSON));
}
}
说明:
(1)ClientBuilder是Client API的主入口,它负责调用REST endpoints。
(2)JAX-RS provider负责创建Client实例, Client实例是“客户端”的“比较重”的对象,因此只在必要的时候创建,不用的时候要关闭。
(3)使用@Inject批注,依赖注入了另一个Managed Bean:MovieBackingBean,其定义见后面。
2. MovieFacadeREST.java
MovieFacadeREST.java是RESTful Web Service的Server端。
package org.glassfish.movieplex7.rest;
import java.util.List;
import javax.ejb.Stateless;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import org.glassfish.movieplex7.entities.Movie;
/**
* @author Arun Gupta
*/
@Named
@Stateless
@Path("movie")
public class MovieFacadeREST extends AbstractFacade {
@PersistenceContext
protected EntityManager em;
public MovieFacadeREST() {
super(Movie.class);
}
@POST
@Override
@Consumes({"application/xml", "application/json"})
public void create(Movie entity) {
super.create(entity);
}
@PUT
@Override
@Consumes({"application/xml", "application/json"})
public void edit(Movie entity) {
super.edit(entity);
}
@DELETE
@Path("{id}")
public void remove(@PathParam("id") Integer id) {
super.remove(super.find(id));
}
@GET
@Path("{id}")
@Produces({"application/xml", "application/json"})
public Movie find(@PathParam("id") Integer id) {
return super.find(id);
}
@GET
@Override
@Produces({"application/xml", "application/json"})
public List getAll() {
return super.getAll();
}
@GET
@Path("{from}/{to}")
@Produces({"application/xml", "application/json"})
public List findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
return super.findRange(new int[]{from, to});
}
@GET
@Path("count")
@Produces("text/plain")
public String countREST() {
return String.valueOf(super.count());
}
@Override
protected EntityManager getEntityManager() {
return em;
}
}
3. movies.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:c="http://xmlns.jcp.org/jsp/jstl/core"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<body>
<ui:composition template="./../WEB-INF/template.xhtml">
<ui:define name="content">
<h:form prependId="false">
<h:selectOneRadio value="#{movieBackingBean.movieId}"
layout="pageDirection">
<c:forEach items="#{movieClientBean.movies}" var="m">
<f:selectItem itemValue="#{m.id}" itemLabel="#{m.name}"/>
</c:forEach>
</h:selectOneRadio>
<h:commandButton value="Details" action="movie" />
<h:commandButton value="Delete" action="movies"
actionListener="#{movieClientBean.deleteMovie()}"/>
<h:commandButton value="New Movie" action="addmovie" />
</h:form>
</ui:define>
</ui:composition>
</body>
</html>
2. MovieBackingBean.java
为了能够把movieId从movies.xhtml传递到movie.xhtml,定义了MovieBackingBean.java。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.glassfish.movieplex7.client;
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
/**
*
* @author pmma
*/
@Named
@SessionScoped
public class MovieBackingBean implements
Serializable {
int movieId;
String movieName;
String actors;
public String getMovieName() {
return movieName;
}
public void setMovieName(String movieName) {
this.movieName = movieName;
}
public String getActors() {
return actors;
}
public void setActors(String actors) {
this.actors = actors;
}
public int getMovieId() {
return movieId;
}
public void setMovieId(int movieId) {
this.movieId = movieId;
}
}
Project 下载:6. movieplex7(Restful).7z
没有评论:
发表评论