RESTEasy数据自动装配
环境:
JDK1.6
resteasy-jaxrs-2.3.4.Final
之前很喜欢用SpringMVC,就是因为它能帮助我们自动完成页面数据的自动装配,我们不需要再用request.getParameter的方式去一个个的取页面传递来的参数。------jerval
使用@FormParam:
#1,#2,#3所对应的页面代码:
<form action="resteasy/form/books/mapped2" target="_blank" method="post"> <input id="author" name="author" value="111" type="text"/> <input name="author" value="222" type="text"/> <input id="ISBN" name="ISBN" value="222" type="text"/> <input id="title" name="title" value="333" type="text"/> <input type="submit" value="submit"/> </form>
1,方法参数上直接加@FormParam
@POST @Path("books/mapped2") @Produces(MediaType.APPLICATION_JSON) @Mapped public Book2 getBooksMapped2(@FormParam("author") String author, @FormParam("ISBN") String ISBN, @FormParam("title") String title) { return new Book2(author, ISBN, title); }
这种适用于参数非常少的情况。
2,使用方法参数MultivaluedMap来获得
@POST @Path("books/mapped2") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Mapped public List<Book2> getBooksMapped3(MultivaluedMap<String,String> form) { List<Book2> list = new ArrayList<Book2>(); list.add(new Book2("Bill Burke2222", "5965292602222", "EJB 3.0222")); // list.add(new Book2(author, ISBN, title)); return list; }
这里面适用于多个参数,或者参数不能枚举的情况。这里通过form.get("key")得到的值将是一个String的数组,key对应页面元素的name属性。
同时也可以将1和2混合使用,如:
@POST @Path("books/mapped2") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Mapped public List<Book2> getBooksMapped3(@FormParam("author") String author, MultivaluedMap<String,String> form) { List<Book2> list = new ArrayList<Book2>(); list.add(new Book2("Bill Burke2222", "5965292602222", "EJB 3.0222")); // list.add(new Book2(author, ISBN, title)); return list; }
但官方文档好像说这个是非法的,不知道为什么?!但实际使用了一下,是没有问题的。
3,使用对象装配,这个是最常用和最效率的方法。
@POST @Path("books/mapped2") @Produces(MediaType.APPLICATION_JSON) @Mapped public Book2 getBooksMapped2(@Form BookForm bookForm) { List<Book2> list = new ArrayList<Book2>(); return new Book2("7777", "8888", "9999"); }
其中对应的BookForm代码为:
public class BookForm { @FormParam("author") private String author; @FormParam("ISBN") private String ISBN; @FormParam("title") private String title; public BookForm() { } public BookForm(String author, String ISBN, String title) { this.author = author; this.ISBN = ISBN; this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getISBN() { return ISBN; } public void setISBN(String ISBN) { this.ISBN = ISBN; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }
当然,如果对象比较复杂,如下:
1,页面代码:
<form action="resteasy/form/books/mapped3" target="_blank" method="post"> <input id="author" name="author" value="111" type="text"/> <input name="author" value="222" type="text"/> <input id="ISBN" name="ISBN" value="222" type="text"/> <input id="title" name="title" value="333" type="text"/> <input name="seller.name" value="seller[zhangsan]" type="text"/> <input name="seller.id" value="seller[5001]" type="text"/> <input type="submit" value="submit"/> </form>
2,Service里代码基本不需要变化:
@POST @Path("books/mapped3") @Produces(MediaType.APPLICATION_JSON) @Mapped public Book2 getBooksMapped3(@Form BookForm bookForm) { return new Book2("7777", "8888", "9999"); }
3,BookForm里代码,注意里面添加了Seller对象和相应的get和set操作:
public class BookForm { @FormParam("author") private String author; @FormParam("ISBN") private String ISBN; @FormParam("title") private String title; @Form(prefix = "seller") private Seller seller; public BookForm() { } public BookForm(String author, String ISBN, String title) { this.author = author; this.ISBN = ISBN; this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getISBN() { return ISBN; } public void setISBN(String ISBN) { this.ISBN = ISBN; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public Seller getSeller() { return seller; } public void setSeller(Seller seller) { this.seller = seller; } }
4,对应的Seller对象的代码:
public class Seller { @FormParam("id") private String id; @FormParam("name") private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
这样,当页面代码提交时,相应的字段值会对应的装配到对象属性上,与SpringMVC和Strust2的数据装配类似。
当然,这里你也可以使用@HeaderParam和@PathParam来提取Header或者URL参数值,如:
public class Seller { @HeaderParam("id") private String id; @PathParam("name") private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
更加复杂的使用如下:
页面端:
<form action="resteasy/library/books/person" target="_blank" method="post"> <input name="telephoneNumbers[0].countryCode" value="111" type="text"/> <input name="telephoneNumbers[0].number" value="222" type="text"/> <input name="telephoneNumbers[1].countryCode" value="2222" type="text"/> <input name="telephoneNumbers[1].number" value="333" type="text"/> <input name="address[INVOICE].street" value="seller[zhangsan]" type="text"/> <input name="address[INVOICE].houseNumber" value="seller[5001]" type="text"/> <input name="address[SHIPPING].street" value="seller[5001]" type="text"/> <input name="address[SHIPPING].houseNumber" value="seller[5001]" type="text"/> <input type="submit" value="submit-person"/> </form>
服务器上webservice对应的方法:
@POST @Path("books/person") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public void post (@Form Person p) { System.out.println("books/person------" + p); }
实体Person:
public class Person { @Form(prefix = "telephoneNumbers") List<TelephoneNumber> telephoneNumbers; @Form(prefix = "address") Map<String,Address> addresses; public List<TelephoneNumber> getTelephoneNumbers() { return telephoneNumbers; } public void setTelephoneNumbers(List<TelephoneNumber> telephoneNumbers) { this.telephoneNumbers = telephoneNumbers; } public Map<String,Address> getAddresses() { return addresses; } public void setAddresses(Map<String,Address> addresses) { this.addresses = addresses; } @Override public String toString() { return "Person [telephoneNumbers=" + telephoneNumbers + ", addresses=" + addresses + "]"; } }
实体TelephoneNumber:
public class TelephoneNumber { @FormParam("countryCode") private String countryCode; @FormParam("number") private String number; public String getCountryCode() { return countryCode; } public void setCountryCode(String countryCode) { this.countryCode = countryCode; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } @Override public String toString() { return "TelephoneNumber [countryCode=" + countryCode + ", number=" + number + "]"; } }
实体Address:
public class Address { @FormParam("street") private String street; @FormParam("houseNumber") private String houseNumber; public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getHouseNumber() { return houseNumber; } public void setHouseNumber(String houseNumber) { this.houseNumber = houseNumber; } @Override public String toString() { return "Address [street=" + street + ", houseNumber=" + houseNumber + "]"; } }
然后在浏览器访问页面端,结果如下:
books/person------Person [telephoneNumbers=[TelephoneNumber [countryCode=111, number=222], TelephoneNumber [countryCode=2222, number=333]], addresses={INVOICE=Address [street=seller[zhangsan], houseNumber=seller[5001]], SHIPPING=Address [street=seller[5001], houseNumber=seller[5001]]}]