调用Rest Service可以基于两种方式:

一种是oj.Collection.extend

一种是$.ajax

  • CORS问题

但在调用之前,首先需要解决rest service的CORS问题.(跨域调用不允许),方法如下:

  • 首先下载jar包:

http://software.dzhuvinov.com/cors-filter-installation.html

<filter>
<filter-name>CORS</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
<init-param>
<param-name>cors.supportedMethods</param-name>
<param-value>GET, POST, HEAD, PUT, PATCH, DELETE</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

然后重新部署rest service.

  • AJAX调用

通过ajax调用的代码如下:

login.js

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
* login module
*/
define(['ojs/ojcore', 'knockout', 'jquery', 'ojs/ojknockout', 'ojs/ojinputtext','ojs/ojbutton'],

function (oj, ko, $)
{

function LoginModel()
{
var self = this;
self.username = ko.observable("eric");
self.password = ko.observable();

self.tracker = ko.observable();

self.login = function (data, event) {

alert(self.username() +" - " +self.password());

$.ajax({
url: "http://127.0.0.1:7001/RestfulApplication-RestfulProject-context-root/resources/restfulproject/login",
data: JSON.stringify({ "loginname": self.username(), "password": self.password() }),
type: "POST",
contentType: "application/json",
success: function (data, textStatus, jqXHR) {
alert("Success");

//var response = $.parseJSON(data);

self.username(null);
self.password(null);

self.router = oj.Router.rootInstance;
self.router.configure({
'dashboard': {label: 'Dashboard', isDefault: true},
'incidents': {label: 'Incidents'},
'customers': {label: 'Customers'},
'about': {label: 'About'}
});

var data = [
{name: 'Dashboard', id: 'dashboard',
iconClass: 'oj-navigationlist-item-icon demo-icon-font-24 demo-chart-icon-24'},
{name: 'Incidents', id: 'incidents',
iconClass: 'oj-navigationlist-item-icon demo-icon-font-24 demo-fire-icon-24'},
{name: 'Customers', id: 'customers',
iconClass: 'oj-navigationlist-item-icon demo-icon-font-24 demo-people-icon-24'},
{name: 'About', id: 'about',
iconClass: 'oj-navigationlist-item-icon demo-icon-font-24 demo-info-icon-24'}
];

// self.navDataSource = new oj.ArrayTableDataSource(data, {idAttribute: 'id'});

//
//
var rootViewModel = ko.dataFor(document.getElementById('mainContent'));
rootViewModel.navDataSource.reset(data, {idAttribute: 'id'});
//rootViewModel.userLogin('redsam');
//rootViewModel.isLoggedIn('true');
//rootViewModel.restSessionId(jqXHR.getResponseHeader('REST_SESSIONID'));

oj.Router.sync();
},
error: function (jqXHR, textStatus, errorThrown) {
alert("401 Unauthorized");
alert("jqXHR",jqXHR.toString());
alert('something went wrong', textStatus.toString());
alert('something ', errorThrown.toString());
}
});
return true;
};

}

return LoginModel();

});

  • oj.Collection: customer.js

/**
* Copyright (c) 2014, 2016, Oracle and/or its affiliates.
* The Universal Permissive License (UPL), Version 1.0
*/
/**
* Main content module
*/
define(['ojs/ojcore', 'knockout', 'jquery', 'ojs/ojknockout', 'ojs/ojmodel', 'ojs/ojcollectiontabledatasource',
'ojs/ojtable', 'ojs/ojpagingcontrol'],
function(oj, ko, $) {
/**
* The view model for the main content view template. Please note that since
* this example uses ojModule binding, you do not need to call ko.applyBindings
* like the JET Cookbook examples. ojModule handles applying bindings for its
* associated view.
*/
function peopleContentViewModel() {
var self = this;
self.serviceURL = 'http://127.0.0.1:7001/RestfulApplication-RestfulProject-context-root/resources/restfulproject/Persons';
self.collectionEmployees = ko.observable();
self.pagingDatasource = ko.observable();

parseEmpl = function (response) {

return {
FirstName: response['firstname'],
HireDate: response['hiredate'],
Id:response['id'],
LastName: response['lastname']
};
};

function getURL(operation, collection, options) {
return self.serviceURL;
};

var collEmpls = new oj.Collection.extend({
customURL: getURL,
fetchSize: 5,
model: new oj.Model.extend({
idAttribute: 'Id',
parse: parseEmpl
})
});

self.collectionEmployees(new collEmpls());
self.pagingDatasource = new oj.PagingTableDataSource(new oj.CollectionTableDataSource(self.collectionEmployees()));
}

/**
* This example returns a view model instance, but can instead return a constructor function
* which will be invoked to create a view model instance for each module reference.
* This instance example will be used as a singleton whenever this module is referenced.
* Please see the 'ViewModel's Lifecycle' section of the ojModule doc for more info.
*/
return new peopleContentViewModel();
});

  • 调试工具:

脚本可以通过alert进行信息的输出.

Rest服务的调试:

下载chrome Advanced Rest client,然后通过chrome://apps/,选择ARC后启动测试Rest Service。

应用调试

通过chrome带的developer tools,选择network,可以看到每个请求及响应

  • 结果:

JET 调用后端Rest Service的更多相关文章

  1. SpringCloud微服务之跨服务调用后端接口

    SpringCloud微服务系列博客: SpringCloud微服务之快速搭建EurekaServer:https://blog.csdn.net/egg1996911/article/details ...

  2. 翻译-使用Spring调用SOAP Web Service

    原文链接: http://spring.io/guides/gs/consuming-web-service/ 调用SOAP web service 本指南将指导你使用Spring调用一个基于SOAP ...

  3. express:webpack dev-server开发中如何调用后端服务器的接口?

    开发环境:     前端:webpack + vue + vue-resource,基于如下模板创建的开发环境: https://github.com/vuejs-templates/webpack  ...

  4. ASP.Net:Javascript 通过PageMethods 调用后端WebMethod方法 + 多线程数据处理 示例

    ASP.Net:Javascript 通过PageMethods 调用后端WebMethod方法 + 多线程数据处理 示例 2012年04月27日 16:59:16 奋斗的小壁虎 阅读数:4500   ...

  5. 关于controller中调用多个service方法的问题

    一般service方法是有事务的,把所有操作封装在一个service方法中是比较安全的. 如果在controller中调用多个service方法,只有查询的情况下是可以这样的.

  6. 在js内生成PDF文件并下载的功能实现(不调用后端),以及生成pdf时换行的格式不被渲染,word-break:break-all

    在js内生成PDF文件并下载的功能实现(不调用后端),以及生成pdf时换行的格式不被渲染,word-break:break-all 前天来了个新需求, 有一个授权书的文件要点击下载, 需要在前端生成, ...

  7. Spring Boot发布和调用RESTful web service

    Spring Boot可以非常简单的发布和调用RESTful web service,下面参考官方指导体验一下 1.首先访问 http://start.spring.io/ 生成Spring Boot ...

  8. 前端https调用后端http

    昨晚发生了一个,很........的事 我前端的域名  和后端的域名 都没有做认证,前端的访问的80 调用的后端80 然后我给前端的域名做了认证ssl,但是调用后端的时候报错 原因是  https 调 ...

  9. 关于jpa的Specification自定义函数,实现oracle的decode;以及如何在静态方法中调用注入的service

    如何在静态方法中调用注入的service Public class ClassA{ public static ClassA classA; @Resource private Service ser ...

随机推荐

  1. 【BZOJ】1708: [Usaco2007 Oct]Money奶牛的硬币

    [算法]DP [题解] 如果每个排列算一种,则令f[i]表示凑成面值为i的方案数,容易推出f[i]+=f[i-a[j]]. 现在是每个组合才算一种,令f[i][j]第二维表示只使用前j种面值,f[i] ...

  2. 【IDEA】设置类头注释和方法注释

    idea和eclipse的注释还是有一些差别的. 类头注释: 打开file->setting->Editor->Filr and Code Templates->Include ...

  3. shell整数加法

    http://blog.csdn.net/ll_0520/article/details/5959577 #plus #!/bin/sh let a=$1+$2 b=$[$1+$2] ((c=$1+$ ...

  4. Linux上Core Dump文件的形成和分析

    原文: http://baidutech.blog.51cto.com/4114344/904419 Core,又称之为Core Dump文件,是Unix/Linux操作系统的一种机制,对于线上服务而 ...

  5. postgresql数据库备份和恢复(超快)

    PostgreSQL自带一个客户端pgAdmin,里面有个备份,恢复选项,也能对数据库进行备份 恢复(还原),但最近发现数据库慢慢庞大的时候,经常出错,备份的文件过程中出错的几率那是相当大,手动调节灰 ...

  6. Multi-Paxos协议日志同步应用

    使用Multi-Paxos协议的日志同步与恢复 基于Basic-Paxos协议的日志同步方案, 所有成员的身份都是平等的, 任何成员都可以提出日志持久化的提案, 并且尝试在成员组中进行持久化. 而在实 ...

  7. mysql常用函数示例

    CREATE TABLE `orders` ( `OrderId` INT(10) NOT NULL AUTO_INCREMENT COMMENT '编号', `ProductName` VARCHA ...

  8. MongoDB多集合排序的一种实现

    需求 假设有三个类型有所不同的表,saleorders.careorders.repairorders,表中有storeId信息,用于关联其所属的门店stores表,现在有个需求是要将这三个表集中展示 ...

  9. 如何在qt中使用中文输入法

    参考: http://blog.csdn.net/u013812682/article/details/52101088 dpkg -L fcitx-frontend-qt5 到qt安装目录里find ...

  10. centos7.2使用yum安装MariaDB10.1

    ①添加MariaDB的yum源 1.创建MariaDB.repo sudo vi /etc/yum.repos.d/Mariadb.repo 2.将以下文件中的字段添加到MariaDB.repo文件中 ...