| Collection<Movie> getMovies() | HTTP GET /movies |
| Movie getMovie(movieId) | HTTP GET /movies/{movieId} |
| void addMovie(Movie movie) *createXXX도 동일함 |
HTTP POST /movies |
| void updateMovie(String movieId, Movie movie) | HTTP PUT /movies/{movieId} |
| void deleteMovie(String movieId) *removeXXX도 동일함 |
HTTP DELETE /movies/{movieId} |
@WebService(
targetNamespace = "http://anyframe.sample.movie.restful.httpbinding.namingconvention")
public interface MovieService {
public Collection<Movie> getMovies() throws Exception;
public Movie getMovie(String movieId) throws Exception;
public void addMovie(@WebParam(name = "Movie")
Movie movie) throws Exception;
public void updateMovie(String movieId, @WebParam(name = "Movie")
Movie movie) throws Exception;
public void deleteMovie(String movieId) throws Exception;
중략...
public class HttpBindingNamingConventionTest extends RemotingTestCase {
// ==============================================================
// ====== TestCase 수행에 필요한 사전 작업 정의 ====================
// ==============================================================
public void setUp() throws Exception {
this.setServer(new JaxWsServer());
ServerInfo serverInfo =
new ServerInfo(MovieService.class, new MovieServiceImpl(),
"http://localhost:9002/movieservice/");
// Use the HTTP Binding which understands the
// Java Rest Annotations
serverInfo.setBindingId(HttpBindingFactory.HTTP_BINDING_ID);
this.getServer().setServerInfo(serverInfo);
super.setUp();
}
중략...
<jaxws:endpoint id="movieService"
implementor="#anyframe.sample.movie.jaxws.MovieService"
bindingUri="http://apache.org/cxf/binding/http"
address="/movieservice">
</jaxws:endpoint>
중략...
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "Movie")
public class Movie implements Serializable {
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
중략...
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"movies" })
@XmlRootElement(name = "getMoviesResponse")
public class GetMoviesResponse {
@XmlElement(name = "return", required = true)
private Collection<Movie> movies;
public Collection<Movie> getMovie() {
return movies;
}
public void setMovie(Collection<Movie> movies) {
this.movies = movies;
}
중략...
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"movie" })
@XmlRootElement(name = "getMovieResponse")
public class GetMovieResponse {
@XmlElement(name = "return", required = true)
private Movie movie = null;
public Movie getMovie() {
return movie;
}
public void setMovie(Movie movie) {
this.movie = movie;
}
중략...
Movie GetMoviesResponse GetMovieResponse
@javax.xml.bind.annotation.XmlSchema(
namespace = "http://anyframe.sample.movie.restful.httpbinding.namingconvention",
elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package anyframe.sample.movie.restful.httpbinding.namingconvention;
public class HttpBindingNamingConventionTest extends RemotingTestCase {
// ==============================================================
// ====== TestCase methods ======================================
// ==============================================================
/**
* [Flow #-1] Positive Case : Get method로 Movie Service의 전체 목록 조회 기능을 호출하여
* XML data를 리턴받고 JAXB를 사용하여 GetMoviesResponse 객체로 전환하여 사용한다.
* Naming Convention 규칙:
* Collection<"resource class name"> get+"The plural of resource class name"()
*
* getMovies method를 RESTful한 Web Service로 노출하여 Client가 호출가능하도록 한다.
* (ex. public Collection<Movie> getMovies() throws Exception; )
* @throws Exception
* throws exception which is from service
*/
public void testFindMovieListAll() throws Exception {
// 1. find movie
GetMethod get = new GetMethod("http://localhost:9002/movieservice/movies");
HttpClient httpclient = new HttpClient();
String response = "";
try {
assertEquals(200, httpclient.executeMethod(get));
response = get.getResponseBodyAsString();
System.out.println(response);
} catch (Exception e) {
fail();
} finally {
get.releaseConnection();
}
JAXBContext jaxbContext =
JAXBContext.newInstance("anyframe.sample.movie.restful.httpbinding.namingconvention");
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
GetMoviesResponse movies = (GetMoviesResponse) unmarshaller.unmarshal
(new InputSource(new StringReader(response)));
// 2. check the movie information
assertEquals(2, movies.getMovie().size());
}
/**
* [Flow #-3] Positive Case : Get method로 Movie Id가 "001"인 Movie를 조회하는 기능을 호출하여
* XML data를 리턴받고 JAXB를 사용하여 GetMovieResponse 객체로 전환하여 사용한다.
* Naming Convention 규칙:
* "resource class name" get+"resource class name"(Object id)
*
* getMovie method를 RESTful한 Web Service로 노출하여 Client가 호출가능하도록 한다.
* (ex. public Movie getMovie(String movieId) throws Exception;)
* @throws Exception
* throws exception which is from service
*/
public void testFindMovie() throws Exception {
// 1. find movie
GetMethod get=new GetMethod("http://localhost:9002/movieservice/movies/001");
HttpClient httpclient = new HttpClient();
String response = "";
try {
assertEquals(200, httpclient.executeMethod(get));
response = get.getResponseBodyAsString();
System.out.println("find: " + response);
} catch (Exception e) {fail();
} finally { get.releaseConnection();}
JAXBContext jaxbContext = JAXBContext
.newInstance("anyframe.sample.movie.restful.httpbinding.namingconvention");
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
GetMovieResponse movie = (GetMovieResponse) unmarshaller.unmarshal(
new InputSource(new StringReader(response)));
// 2. check the movie information
assertEquals("The Sound Of Music", movie.getMovie().getTitle());
assertEquals("Robert Wise", movie.getMovie().getDirector());
}
/**
* [Flow #-4] Positive Case : Post method로 Movie Id가 "003"인 신규 Movie를 생성하는 .
* 기능을 호출한다
* Naming Convention 규칙:
* void add or create + "resource class name"("resource class" obj)
*
* addMovie method를 RESTful한 Web Service로 노출하여 Client가 호출가능하도록 한다.
* (ex. public void addMovie(@WebParam(name = "Movie") Movie movie) ...
* @throws Exception
* throws exception which is from service
*/
public void testCreateMovie() throws Exception {
// 1. create movie
String inputFile = this.getClass().getClassLoader().getResource(
"webservices/restful/httpbinding/namingconvention/create_movie.txt").getFile();
File input = new File(inputFile);
PostMethod post =
new PostMethod("http://localhost:9002/movieservice/movies");
RequestEntity entity =
new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
post.setRequestEntity(entity);
HttpClient httpclient = new HttpClient();
String response = "";
try {
assertEquals(200, httpclient.executeMethod(post));
response = post.getResponseBodyAsString();
System.out.println("create: " + response);
} catch (Exception e) {
fail();
} finally {
post.releaseConnection();
}
중략...
| anyframe-remotingtest-src.zip |