关于RestFul编程可以参考:https://www.cnblogs.com/wang-yaz/p/9237981.html
关于jaxrs的实现需要有restful的理解。
话不多说直接上代码!!
1、服务端的开发
1、新建web项目、工程目录如下图
2、pom文件添加依赖
4.0.0 com.cr server 1.0-SNAPSHOT server http://www.example.com UTF-8 1.7 1.7 org.apache.cxf cxf-rt-frontend-jaxrs 3.0.1 org.apache.cxf cxf-rt-transports-http-jetty 3.0.1 org.slf4j slf4j-log4j12 1.7.12 test org.apache.cxf cxf-rt-rs-client 3.0.1 org.apache.cxf cxf-rt-rs-extension-providers 3.0.1 org.codehaus.jettison jettison 1.3.7 junit junit 4.11 test org.apache.maven.plugins maven-compiler-plugin 3.2
3、类:
User.java
注意需要和client端在同一个包里
package com.cr.entity;import javax.xml.bind.annotation.XmlRootElement;//基于restful风格的webservice//客户端与服务的之间通讯可以传到xml、json//该注解指定对象序列化为xml或者json数据时根节点的名称//xml://json:效率更高 {"User":"",pwd:""}@XmlRootElement(name = "User")public class User { private String name; private String pwd; //.....}
接口
UserService.java
package com.cr.service;import com.cr.entity.User;import javax.ws.rs.*;import java.util.List;//访问当前服务接口对应的路径@Path("/userService")@Produces("*/*")//支持任意类型public interface UserService { @POST @Path("/user")//访问当前端方法对应的路径 @Consumes({ "application/xml","application/json"})//服务器支持的请求数据格式类型 public void save(User user); @GET @Path("/user") @Consumes({ "application/xml","application/json"}) @Produces("application/xml") public ListfindUser();}
接口的实现
UserServiceImpl.java
package com.cr.service.impl;import com.cr.entity.User;import com.cr.service.UserService;import java.util.ArrayList;import java.util.List;public class UserServiceImpl implements UserService { public void save(User user){ System.out.println("save:"+user); } public ListfindUser(){ List uList = new ArrayList<>(); User u1 = new User("aa","qwe"); User u2 = new User("bb","asd"); uList.add(u1); uList.add(u2); System.out.println(uList); return uList; }}
发布服务
package com.cr;import com.cr.service.impl.UserServiceImpl;import org.apache.cxf.interceptor.LoggingInInterceptor;import org.apache.cxf.interceptor.LoggingOutInterceptor;import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;public class server { public static void main(String[] args) { //发布服务的工程 JAXRSServerFactoryBean factoryBean = new JAXRSServerFactoryBean(); //设置服务的 地址 factoryBean.setAddress("http://localhost:8001/cr/"); //设置服务类 factoryBean.setServiceBean(new UserServiceImpl()); //添加日志输入 factoryBean.getInInterceptors().add(new LoggingInInterceptor()); factoryBean.getOutInterceptors().add(new LoggingOutInterceptor()); //发布服务 factoryBean.create(); System.out.println("发布成功..."); }}
2、客户端的代码
1、工程结构
2、pom文件
4.0.0 com.cr client 1.0-SNAPSHOT client http://www.example.com UTF-8 1.7 1.7 org.apache.cxf cxf-rt-frontend-jaxrs 3.0.1 org.apache.cxf cxf-rt-transports-http-jetty 3.0.1 org.slf4j slf4j-log4j12 1.7.12 test org.apache.cxf cxf-rt-rs-client 3.0.1 org.apache.cxf cxf-rt-rs-extension-providers 3.0.1 org.codehaus.jettison jettison 1.3.7 junit junit 4.11 test org.apache.maven.plugins maven-compiler-plugin 3.2
3、类
User.java
package com.cr.entity;import javax.xml.bind.annotation.XmlRootElement;//基于restful风格的webservice//客户端与服务的之间通讯可以传到xml、json//该注解指定对象序列化为xml或者json数据时根节点的名称//xml://json:效率更高 {"User":"",pwd:""}@XmlRootElement(name = "User")public class User { private String name; private String pwd; //....}
测试:
@Testpublic void save(){ //远程调用服务端 WebClient. create("http://localhost:8001/cr/userService/user"). type("application/json"). post(new User("qq","22"));}
server端的打印
----------------------------ID: 4Address: http://localhost:8001/cr/userService/userEncoding: ISO-8859-1Http-Method: POSTContent-Type: application/jsonHeaders: {Accept=[*/*], Cache-Control=[no-cache], connection=[keep-alive], Content-Length=[31], content-type=[application/json], Host=[localhost:8001], Pragma=[no-cache], User-Agent=[Apache CXF 3.0.1]}Payload: {"User":{"name":"qq","pwd":22}}--------------------------------------save:com.cr.entity.User@4986f0c7INFO - LoggingOutInterceptor - Outbound Message---------------------------ID: 4Response-Code: 204Content-Type:Headers: {Date=[Mon, 18 Mar 2019 11:56:29 GMT], Content-Length=[0]}--------------------------------------
测试:
//查询@Testpublic void find(){ WebClient.create("http://localhost:8001/cr/userService/user").get();}
ID: 5Address: http://localhost:8001/cr/userService/userHttp-Method: GETContent-Type: */*Headers: {Accept=[*/*], Cache-Control=[no-cache], connection=[keep-alive], content-type=[*/*], Host=[localhost:8001], Pragma=[no-cache], User-Agent=[Apache CXF 3.0.1]}--------------------------------------[com.cr.entity.User@7f4f702, com.cr.entity.User@3cb7324]INFO - LoggingOutInterceptor - Outbound Message---------------------------ID: 5Response-Code: 200Content-Type: application/xmlHeaders: {Content-Type=[application/xml], Date=[Mon, 18 Mar 2019 11:57:38 GMT]}Payload:aa qwe bb asd
3、额外的补充
额外的补充都可以尝试去实现!!!