开发运行环境:NetBeans7.3.1。
通过定义一个JAX-RS Entity Providers,允许使用JSON格式对Movie POJO类进行读写操作。
1. MovieReader.java
MovieReader.java会根据传进来的输入流,生成Movie对象,并返回。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.glassfish.movieplex7.json;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import javax.json.Json;
import javax.json.stream.JsonParser;
import javax.ws.rs.Consumes;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.Provider;
import org.glassfish.movieplex7.entities.Movie;
/**
*
* @author pmma
*/
@Provider
@Consumes(MediaType.APPLICATION_JSON)
public class MovieReader implements MessageBodyReader {
@Override
public boolean isReadable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return Movie.class.isAssignableFrom(type);
}
@Override
public Movie readFrom(Class type, Type type1, Annotation[] antns, MediaType mt, MultivaluedMap mm, InputStream in)
throws IOException, WebApplicationException {
Movie movie = new Movie();
JsonParser parser = Json.createParser(in);
while (parser.hasNext()) {
switch (parser.next()) {
case KEY_NAME:
String key = parser.getString();
parser.next();
switch (key) {
case "id":
movie.setId(parser.getInt());
break;
case "name":
movie.setName(parser.getString());
break;
case "actors":
movie.setActors(parser.getString());
break;
default:
break;
}
break;
default:
break;
}
}
return movie;
}
}
说明:
(1)@Provider批注表明这个类是一个JAX-RS provider,运行时将会被扫描到。
(2)isReadable用于甄别对象类型,只有Movie类型才可以被处理。
通过定义一个JAX-RS Entity Providers,允许使用JSON格式对Movie POJO类进行读写操作。
1. MovieReader.java
MovieReader.java会根据传进来的输入流,生成Movie对象,并返回。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.glassfish.movieplex7.json;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import javax.json.Json;
import javax.json.stream.JsonParser;
import javax.ws.rs.Consumes;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyReader;
import javax.ws.rs.ext.Provider;
import org.glassfish.movieplex7.entities.Movie;
/**
*
* @author pmma
*/
@Provider
@Consumes(MediaType.APPLICATION_JSON)
public class MovieReader implements MessageBodyReader
@Override
public boolean isReadable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return Movie.class.isAssignableFrom(type);
}
@Override
public Movie readFrom(Class
throws IOException, WebApplicationException {
Movie movie = new Movie();
JsonParser parser = Json.createParser(in);
while (parser.hasNext()) {
switch (parser.next()) {
case KEY_NAME:
String key = parser.getString();
parser.next();
switch (key) {
case "id":
movie.setId(parser.getInt());
break;
case "name":
movie.setName(parser.getString());
break;
case "actors":
movie.setActors(parser.getString());
break;
default:
break;
}
break;
default:
break;
}
}
return movie;
}
}
说明:
(1)@Provider批注表明这个类是一个JAX-RS provider,运行时将会被扫描到。
(2)isReadable用于甄别对象类型,只有Movie类型才可以被处理。
2. MovieWriter.java
MovieWriter.java会根据传进来的Movie对象,生成输出流。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package org.glassfish.movieplex7.json;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import javax.json.Json;
import javax.json.stream.JsonGenerator;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;
import org.glassfish.movieplex7.entities.Movie;
/**
*
* @author pmma
*/
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class MovieWriter implements MessageBodyWriter {
@Override
public boolean isWriteable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return Movie.class.isAssignableFrom(type);
}
@Override
public long getSize(Movie t, Class type, Type genericType, Annotation[] annotations, MediaType mediaType) {
return -1;
}
@Override
public void writeTo(Movie t, Class type, Type type1, Annotation[] antns, MediaType mt, MultivaluedMap mm, OutputStream out) throws IOException, WebApplicationException {
JsonGenerator gen = Json.createGenerator(out);
gen.writeStartObject()
.write("id", t.getId())
.write("name", t.getName())
.write("actors", t.getActors())
.writeEnd();
gen.flush();
}
}
说明:
(1)@Provider批注表明这个类是一个JAX-RS provider,运行时将会被扫描到。
(2)isWriteable用于甄别对象类型,只有Movie类型才可以被处理。
3. MovieClientBean.java中的addMovie方法
注意,这里“注册了”MovieWriter.class,也就是说当调用addMovie方法时,会根据MovieWriter的定义把Movie对象转换成JSON格式,然后以post方式调用RESTful Web Service。
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));
}
Project 下载:7. movieplex7(Json).7z
(1)@Provider批注表明这个类是一个JAX-RS provider,运行时将会被扫描到。
(2)isWriteable用于甄别对象类型,只有Movie类型才可以被处理。
3. MovieClientBean.java中的addMovie方法
注意,这里“注册了”MovieWriter.class,也就是说当调用addMovie方法时,会根据MovieWriter的定义把Movie对象转换成JSON格式,然后以post方式调用RESTful Web Service。
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));
}
Project 下载:7. movieplex7(Json).7z
没有评论:
发表评论