Restful接口实现实例
来源:互联网 发布:php教学 编辑:程序博客网 时间:2024/05/22 15:24
rest需要jar包:
web.xml加如下配置:
<context-param> <param-name>org.restlet.application</param-name> <param-value>vs.dascsl.restservice.app.RestJaxRsApplication</param-value> </context-param> <servlet> <servlet-name>RestletServlet</servlet-name> <servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>RestletServlet</servlet-name> <url-pattern>/restServices/*</url-pattern> </servlet-mapping>
服务接口Rest资源加载App类:
public class ServiceInterfaceApplication extends Application { @Override public Set<Class<?>> getClasses() { Set<Class<?>> rrcs = new HashSet<Class<?>>(); // rrcs.add(ServDataResource.class);// 这个application中绑定这三个reource。 // rrcs.add(ServMetaResource.class);// 用一个application管理多个resource // rrcs.add(UserLoginResource.class); rrcs.add(ModelChangeResource.class); return rrcs; }}
Rest接口JAX风格App类:
加入了运行环境中,如果有多个Application可以在此绑定。
/*** * Rest接口JAX风格App类. * @author gengjanlu * *///创建运行环境。RESTlet 架构为了更好的支持 JAX-RS 规范,定了 JaxRsApplication 类来初始化基于 JAX-RS 的 Web Service 运行环境。public class RestJaxRsApplication extends JaxRsApplication { /** * 初始化. * @param context 上下文 */ public RestJaxRsApplication(Context context) { super(context); this.add(new ServiceInterfaceApplication()); // this.add(new DomainAndDatasetMetaApp()); }}
测试类TestRestClient:
public class TestModelRestClient { public static String url="http://127.0.0.1:8080/DASBASE/restServices/"; public static String testModel(){ ClientResource client = new ClientResource(url+"modelChangeService/compCollectModelVersion"); String body = ""; // 查询模型变更 try { Form form = new Form(); form.add("jsonData","{\"localVersion\":\"V2.002\"}"); body = client.post(form.getWebRepresentation()).getText(); } catch (Exception e) { e.printStackTrace(); } System.out.println(body); // return body; } public static void main(String[] args) { testModel(); }}
Resource类:
@Path("modelChangeService")public class ModelChangeResource { @POST @Path("compCollectModelVersion") public String compCollectModelVersion(Representation entity) { String result = ""; try { Form form = new Form(entity); String json = form.getFirstValue("jsonData"); JSONObject to_json = JSONObject.fromObject(json); String to_string = (String) to_json.get("localVersion"); result = ModelChangeServiceImpl.compCollectModelVersion(to_string); } catch (Exception e) { e.printStackTrace(); } return result; }}
ServiceImpl类:
public static String compCollectModelVersion(String localVersion) throws Exception { localVersion = localVersion.substring(1); String ver[] = localVersion.split("\\."); String bigVer = ver[0]; String smallVer = ver[1]; String versionList = ""; Map<String, String> dataMap = new LinkedHashMap<String,String>(); Map<String, String>versionMap = versionData(); if(versionMap.get("VERSION_CODE").equals(bigVer) && versionMap.get("NEW_CHANGE_CODE").substring(1).equals(smallVer)){ dataMap.put("isEqual", "true"); }else { String verSql = "SELECT c.change_code FROM mv_oobpm_version_change c where c.change_code between 'C"+smallVer+"' and '"+versionMap.get("NEW_CHANGE_CODE")+"'"; List<Map<String, String>> dataList = DBService.getModelMgrInstance().queryForList(verSql); for(Map<String,String> map:dataList){ versionList+="V"+versionMap.get("VERSION_CODE")+"."+map.get("CHANGE_CODE").substring(1)+","; } dataMap.put("isEqual", "false"); dataMap.put("lastVersion", "V"+versionMap.get("VERSION_CODE")+"."+versionMap.get("NEW_CHANGE_CODE").substring(1)); dataMap.put("versionList", versionList.substring(0, versionList.length()-1)); } return new Gson().toJson(dataMap); }
阅读全文
1 0
- Restful接口实现实例
- springmvc 实现restful接口
- Restful接口实现方法
- restful接口ADF实现代码
- springMVC的Restful接口实现
- 使用SpringMVC实现RESTFul接口
- Yii2 使用 RESTful 写API接口 实例
- Yii2 使用 RESTful 写API接口 实例
- 高性能tornado框架简单实现restful接口及运维开发实例
- 高性能tornado框架简单实现restful接口及运维开发实例
- RESTful接口签名认证实现机制
- 图片上传--Jersey实现RESTful接口
- 图片上传--Jersey实现RESTful接口
- 图片上传--SpringMVC实现RESTful接口
- thinkphp3.2 restful接口的实现
- 《利用Java框架实现Restful接口》
- Apache HttpClient实现访问RESTFUL接口
- 三种方法实现调用Restful接口
- IP地址正则匹配解析
- Linux 进程与线程一(创建-关闭线程)
- knight moves经典广搜(bfs)
- https://zhidao.baidu.com/question/1543082147564945667.html
- 如何在 Kaggle 首战中进入前 10%
- Restful接口实现实例
- macbook pro 用 DP 接口外接显示器系统音量不可用
- SpringMVC总结
- 点击图片控制音乐播放及暂停
- Session & Token
- 简单的使用ehcache
- python实现对解析之后的DOM进行层次化处理升序输出
- J2EE系列之MyBatis学习笔记(九)-- 杂项
- 一文理清散乱的物联网里开发者必须关注的技术!