Restful接口实现实例

来源:互联网 发布:php教学 编辑:程序博客网 时间:2024/05/22 15:24

rest需要jar包:
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);    }