jersey框架中的web service接口传递对象参数
一、参考资源:
1.http://blog.csdn.net/cb2474600377/article/details/44240191
2.http://wenku.baidu.com/link?url=RyoJ3qtAkzk0yRsMhDcDecYIEVlLVm4RRrazWj1dFX1Wr
hEJ7LxzouqLkFJHXj0-Mco4xG8BXCUkGTqNgKJ1-uV7shCFLQIomb5_n_-63M_
二、webservice开发实例
关于jersey开发web service简单接口和客户端调用,可以参考:
http://blog.csdn.net/cb2474600377/article/details/44240191
以下开发环境及工具:
1.window764为Pc机
2.Tomcat7.0 window 64位版本
3.Eclipse 3.7 window 64位版本
4.火狐浏览器36.0.1版本
5.jaxrs-ri-2.16版本
1.新建一个web 项目,将下载的jaxrs-ri-2.16解压(下载资源可以参考‘资源地址1’),将里面的全部jar导入项目中,传递的参数为对象,需要转换成json格式,这里就要导入json支持的jar包(这里提供json支持jar的下载地址:http://pan.baidu.com/s/1bnfIWZP )
2.导包完成后,在src下的相应包中创建一个实体类UserBean和web service类RestService以及资源加载类MyServiceRegister,代码如下:
UserBean:注意:@JsonInclude是将该实体对象转换成json格式的注解配置,不能掉,以及导包不能导错,类路径为: com.fasterxml.jackson.annotation.JsonInclude
package com.jersey.webservice; import com.fasterxml.jackson.annotation.JsonInclude; @JsonInclude(JsonInclude.Include.NON_DEFAULT) public class UserBean { private String name; private String sex; private int age; public UserBean() { super(); // TODO Auto-generated constructor stub } public UserBean(String name, String sex, int age) { super(); this.name = name; this.sex = sex; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "UserBean [name=" + name + ", sex=" + sex + ", age=" + age + "]"; } }
RestService:
package com.jersey.webservice; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("HelloWorld") public class RestService { @GET @Path("/sayHello/{name}") @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON}) @Produces(MediaType.APPLICATION_JSON) public String sayHello(@PathParam("name")String name){ return "Hello "+name; } //实体对象参数web接口 @POST @Path("/sayName") @Consumes( MediaType.APPLICATION_JSON+";charset=UTF-8") @Produces(MediaType.APPLICATION_JSON) public UserBean sayName(UserBean ub){ ub.setName(ub.getName()+"123"); return ub; } }
MyServiceRegister:在加载webservice接口类前也要加载JacksonJsonProvider类,JacksonJsonProvider的类路径com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider
package com.jersey.webservice; import org.glassfish.jersey.server.ResourceConfig; import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider; public class MyServiceRegister extends ResourceConfig { public MyServiceRegister() { //转换格式资源类 register(JacksonJsonProvider.class); //web service接口类 register(RestService.class); } }
3.在web.xml中为web service接口添加配置
<servlet> <servlet-name>jersey-servlet</servlet-name> <servlet-class> org.glassfish.jersey.servlet.ServletContainer </servlet-class> <!-- 配置自己的资源加载类去加载资源 --> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value> com.jersey.webservice.MyServiceRegister </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jersey-servlet</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping>
4.运行项目,浏览器访问:http://localhost:8080/JerseyObjPar/rest/HelloWorld/sayHello/admin
出现如下结果,则接口发布成功。
三、客户端调用,实现对象传递(需要服务端提供实体类)
1.新建项目,导入jersey客户端支持jar和json支持jar(可以将写服务端的jar全部导入项目),然后在src下新建实体类UserBean,与服务端实体类一致,然后新建一个测试类Test,类中的各个类导包不要导错
package com.jersey.webservice; import javax.ws.rs.client.Client; import javax.ws.rs.client.ClientBuilder; import javax.ws.rs.client.Entity; import javax.ws.rs.client.Invocation.Builder; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.MediaType; import org.glassfish.jersey.client.ClientConfig; import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider; public class Test { public static void main(String[] args) { String url = "http://localhost:8080/JerseyObjPar/rest"; ClientConfig clientConfig = new ClientConfig(); clientConfig.register(JacksonJsonProvider.class); Client client = ClientBuilder.newClient(clientConfig); WebTarget webTarget = client.target(url).path("HelloWorld").path("sayName"); Builder builder = webTarget.request(MediaType.APPLICATION_JSON); UserBean ub = new UserBean("admin","男",121); UserBean res = builder.post(Entity.entity(ub, MediaType.APPLICATION_JSON), UserBean.class); System.out.println(res); } }
2.运行程序,结果:
最后附项目资源下载:http://pan.baidu.com/s/1qWv75Je