Spring boot restful api demo
1、Controller
package com.jiankunking.example.controller;
import com.jiankunking.example.model.Domain;
import com.jiankunking.example.model.Response;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
/**
* Created by jiankunking on 2017/5/29 16:55.
* curd restful api demo
*/
@Controller
@RequestMapping(value = "/api/test")
public class CrudController {
/**
* 新增
* POST /api/test
* 引數:
* {
name: 'name',
visible: 1,
services: ['xx','yy'],
}
* @return
*/
@ResponseBody
@RequestMapping(method = RequestMethod.POST)
public Response insertTest(@RequestBody Domain domain,
HttpServletRequest request) {
return Response.ok("新增成功!");
}
/**
* 修改
* PUT /api/test/${id}
* @param
* @return
*/
@ResponseBody
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public Response updateTest(@PathVariable(value = "id") String id, @RequestBody Domain domain,
HttpServletRequest request) {
return Response.ok("修改成功!");
}
/**
* 刪除
* DELETE /api/test/${id}
* @param id
* @return
*/
@ResponseBody
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public Response deleteTestById(@PathVariable(value = "id") String id) {
return Response.ok("刪除成功!");
}
/**
* 查詢
* GET /api/test
* 引數:{ sysId: '1' }
* @return
*/
@ResponseBody
@RequestMapping(method = RequestMethod.GET)
public Response getTestBySysId(@RequestParam(name = "sysId", required = true) String sysId) {
HashMap map = new HashMap();
map.put(1, 1);
return Response.ok(map);
}
/**
* 查詢
* GET /api/test/${id}
* @return
*/
@ResponseBody
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public Response getTestInfoByModuleId(@PathVariable(value = "id") String id) {
HashMap map = new HashMap();
map.put(2, 2);
return Response.ok(map);
}
}
2、entity
package com.jiankunking.example.model;
import lombok.Data;
import java.util.Date;
/**
* Created by jiankunking on 2017/5/29 17:00.
*/
@Data
public class Domain {
private String name;
private int visible;
private String[] services;
private Date createTime;
}
package com.jiankunking.example.model;
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
/**
* Created by jiankunking on 2017/5/9 13:47.
* 該類已簡化
*/
public class Response<T> implements Serializable {
private static final long serialVersionUID = -750644833749014618L;
private T data;
@Getter
@Setter
private Boolean isSuccess;
public Response() {
this.setIsSuccess(true);
}
public T getData() {
return this.data;
}
public void setData(T data) {
this.data = data;
}
public static <T> Response<T> ok(T data) {
Response resp = new Response();
resp.setData(data);
resp.setIsSuccess(true);
return resp;
}
public static <T> Response<T> fail(Integer code, String error) {
Response resp = new Response();
//resp.setCode(code);
//resp.setMsg(error);
resp.setIsSuccess(false);
return resp;
}
}
3、postman 細節
4、原始碼下載
http://download.csdn.net/detail/xunzaosiyecao/9855449
作者:jiankunking 出處:http://blog.csdn.net/jiankunking
相關文章
- Spring Boot入門系列(二十)快速打造Restful API 介面Spring BootRESTAPI
- Spring Boot Demo with DockerSpring BootDocker
- spring boot構建restful服務Spring BootREST
- 使用Spring Boot RESTful Web流式資料 | TechshardSpring BootRESTWeb
- Spring Security專案Spring MVC開發RESTful API(二)SpringMVCRESTAPI
- Spring boot+Swagger配置無侵入式Restful介面(二)Spring BootSwaggerREST
- 基於 Spring Boot 2.0 構建一個 RESTful WebServiceSpring BootRESTWeb
- Spring Boot入門系列(二十一)如何優雅的設計 Restful API 介面版本號,實現 API 版本控制!Spring BootRESTAPI
- 如何在生產環境中通過Restful API的方式請求重啟Spring Boot應用?RESTAPISpring Boot
- 理解RESTful APIRESTAPI
- Spring Boot系列22 Spring Websocket實現websocket叢集方案的DemoSpring BootWeb
- Spring Boot WebFlux 增刪改查完整實戰 demoSpring BootWebUX
- Spring Boot 2 + Spring Security 5 + JWT 的單頁應用Restful解決方案Spring BootJWTREST
- 在Spring Boot快取API - Code FactorySpring Boot快取API
- Spring Boot整合Zuul API閘道器Spring BootZuulAPI
- 如何在spring boot 使用 gitlab的ApiSpring BootGitlabAPI
- 使用Docker實現Spring Boot Restful Web服務案例原始碼DockerSpring BootRESTWeb原始碼
- Spring Boot中使用Swagger2構建RESTful APIs介紹Spring BootSwaggerRESTAPI
- springboot gradle demo (使用 Gradle 構建的 Spring Boot專案)Spring BootGradle
- 只需一步,在Spring Boot中統一Restful API返回值格式與統一處理異常Spring BootRESTAPI
- RESTful風格APIRESTAPI
- Spring Boot 之 RESRful API 許可權控制Spring BootAPI
- Spring Boot + GraphQL建立API的開源案例Spring BootAPI
- 使用JBang構建Spring Boot Rest API教程Spring BootRESTAPI
- Spring Boot 中 10 行程式碼構建 RESTful 風格應用Spring Boot行程REST
- Spring Boot核心技術之Restful對映以及原始碼的分析Spring BootREST原始碼
- nodejs實現restful APINodeJSRESTAPI
- RESTful API 最佳實踐RESTAPI
- restful api最佳實踐RESTAPI
- restful api設計指南RESTAPI
- 理解RESTful Api設計RESTAPI
- Spring Boot中實現乾淨API響應Spring BootAPI
- Spring Boot:Spring Boot配置MybatisSpring BootMyBatis
- Spring Boot:Spring Boot配置SwaggerSpring BootSwagger
- Spring boot 之自動生成API文件swagger2Spring BootAPISwagger
- Spring Boot 編寫 API 的 10條最佳實踐Spring BootAPI
- RESTful api風格介紹RESTAPI
- RESTful API 設計規範RESTAPI
- Zalando RESTful API和事件指南RESTAPI事件