iot前台开发环境:请求示例

参考链接:http://www.cnblogs.com/keatkeat/category/872790.html

编辑-》update保存

一、typescipt

import { Injectable } from '@angular/core';
import { Headers, Http } from '@angular/http';
import { RequestOptions } from '@angular/http';

import 'rxjs/add/operator/toPromise';

@Injectable()
export class SubnetService {
constructor(private http: Http) {}

getDatas(): Promise<any> {
return this.http.get("/devices")
.toPromise()
.then(response => response.json())
.catch(this.handleError);
}

getData(): Promise<any> {
return this.http.get("/devices/")
.toPromise()
.then(response => response.json())
.catch(this.handleError);
}

update(id, data): Promise<any>{
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this.http.post("/devices/"+ id +"/update", JSON.stringify(data),options)
.toPromise()
.then(response => response.json())
.catch(this.handleError);
}

delete(id): Promise<any> {
return this.http.delete("/devices/"+ id)
.toPromise()
.then(response => response)
.catch(this.handleError);
}

private handleError(error: any): Promise<any> {
console.error('An error occurred', error);
return Promise.reject(error.message || error);
}
}

二、java代码

package com.inspur.iot.iot_hub.controller;

import com.inspur.iot.iot_hub.entity.DmDevicetype;
import com.inspur.iot.iot_hub.service.DeviceService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping(value="/devices") // 通过这里配置使下面的映射都在/devices下,可去除
public class DeviceTypeController {
@Autowired
DeviceService deviceServer;
@ApiOperation(value="获取设备类型列表", notes="")
@RequestMapping(value={""}, method= RequestMethod.GET)
public List<DmDevicetype> getDeviceList() {
List<DmDevicetype> devices = deviceServer.getDevice();
return devices;
}
@ApiOperation(value="查询设备类型", notes="")
@ApiImplicitParam(name = "device", value = "设备查询", required = true, dataType = "Device")
@RequestMapping(value="/code", method=RequestMethod.GET)
public List<DmDevicetype> getDevicesByCode(@PathVariable String code) {
List<DmDevicetype> devices = deviceServer.getDeviceByCode(code);
return devices;
}

@ApiOperation(value="创建设备类型", notes="")
@ApiImplicitParam(name = "device", value = "设备详细实体device", required = true, dataType = "Device")
@RequestMapping(value="", method=RequestMethod.POST)
public DmDevicetype createDevice(@RequestBody DmDevicetype device) {
deviceServer.create(device);
return device;
}

@ApiOperation(value="获取设备详细信息", notes="根据url的id来获取设备详细信息")
@ApiImplicitParam(name = "id", value = "设备ID", required = true, dataType = "String")
@RequestMapping(value="/{id}/detail", method=RequestMethod.GET)
public DmDevicetype getDevice(@PathVariable String id) {
return deviceServer.queryDevice(id);
}

@ApiOperation(value="更新设备详细信息", notes="根据url的id来指定更新对象,并根据传过来的device信息来更新用户详细信息")

@RequestMapping(value="/{id}/update", method=RequestMethod.POST)
public DmDevicetype updateDevice(@PathVariable String id, @RequestBody DmDevicetype device){
deviceServer.updateDevice(id,device);
return device;
}

@ApiOperation(value="删除设备", notes="根据url的id来指定删除对象")
@ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String")
@RequestMapping(value="/{id}", method=RequestMethod.DELETE)
public String deleteDevice(@PathVariable String id) {
deviceServer.deleteDevice(id);
return "success";
}

}

原文地址:https://www.cnblogs.com/lexiaofei/p/8365247.html