一、代码结构:

二、使用Springmvc开发restful API

传统url和rest区别:

三、写代码

1,编写RestfulAPI的测试用例:使用MockMvc伪造mvc

package com.imooc.web.controller;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date; import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext; @RunWith(SpringRunner.class)
@SpringBootTest
public class UserControllerTest { @Autowired
private WebApplicationContext webCtx; //伪造mvc
private MockMvc mockMvc; @Before
public void setup(){
mockMvc = MockMvcBuilders.webAppContextSetup(webCtx).build();
} /**
* 查询
*/
@Test
public void whenQuerySuccess() throws Exception{
String result = mockMvc.perform(MockMvcRequestBuilders.get("/user") //路径
.param("page", "10") //参数
.param("size", "12")
.param("sort", "age,desc")
.param("username", "xiaoming")
.param("age", "18")
.param("ageTo", "40")
.param("other", "otherProperty")
.contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(MockMvcResultMatchers.status().isOk()) //状态码200
.andExpect(MockMvcResultMatchers.jsonPath("$.length()").value(3))//长度为3,具体查看github的jsonPath项目
.andReturn().getResponse().getContentAsString();
System.err.println(result);
} /**
* 详情
*/
@Test
public void whenGetInfoSuccess() throws Exception{
String result = mockMvc.perform(MockMvcRequestBuilders.get("/user/1")
.contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.username").value("tom"))
.andReturn().getResponse().getContentAsString();
System.err.println(result);
} /**
* 详情失败
*/
@Test
public void whenGetInfoFail() throws Exception{
mockMvc.perform(MockMvcRequestBuilders.get("/user/a") //匹配正则
.contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(MockMvcResultMatchers.status().is4xxClientError());
} @Test
public void whenCreateSuccess() throws Exception{
long date = new Date().getTime();
System.err.println(">>>>>>>>"+date);
String content = "{\"username\":\"tom\",\"password\":null,\"birthday\":"+date+"}";
String result = mockMvc.perform(MockMvcRequestBuilders.post("/user")
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(content))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value("1"))
.andReturn().getResponse().getContentAsString();
System.err.println(result);
} @Test
public void whenUpdateSuccess() throws Exception{
//jdk8新增,当前日期加一年,测试生日@past注解
Date date = new Date(LocalDateTime.now().plusYears(1).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
System.err.println(">>>>>>>>"+date);
String content = "{\"id\":\"1\",\"username\":\"tom\",\"password\":null,\"birthday\":"+date.getTime()+"}";
String result = mockMvc.perform(MockMvcRequestBuilders.put("/user/1") //put
.contentType(MediaType.APPLICATION_JSON_UTF8)
.content(content))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.id").value("1"))
.andReturn().getResponse().getContentAsString();
System.err.println("update result>>>>> "+result);
} @Test
public void whenDeleteSuccess() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.delete("/user/1")
.contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(MockMvcResultMatchers.status().isOk());
} }

Controller:使用注解声明RestfulAPI

package com.imooc.web.controller;

import java.util.ArrayList;
import java.util.List; import javax.validation.Valid; import org.springframework.data.domain.Pageable;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.fasterxml.jackson.annotation.JsonView;
import com.imooc.dto.User;
import com.imooc.dto.UserQueryCondition; @RestController
@RequestMapping("/user")
public class UserController { /**
* @Description: 条件查询
* @param @param condition
* @param @param pageable
* @param @return
* @return List<User>
* @throws
* @author lihaoyang
* @date 2018年2月24日
*/
@GetMapping()
@JsonView(User.UserSimpleView.class)
public List<User> query(
//@RequestParam(value="username",required=false,defaultValue="lhy") String username
UserQueryCondition condition , Pageable pageable){
// System.err.println(username);
System.err.println(condition.toString());
System.err.println(pageable.toString()); List<User> users = new ArrayList<User>();
users.add(new User());
users.add(new User());
users.add(new User());
return users;
} /**
* 详情
* @Description: TODO
* @param @param id
* @param @return
* @return User
* @throws
* @author lihaoyang
* @date 2018年2月24日
*/
@GetMapping("{id:\\d+}") //{}里可以是正则,匹配数字
// @GetMapping("detail/{id}")
@JsonView(User.UserDetailView.class)
public User getInfo(@PathVariable(value="id",required=true) String id){
System.err.println(id);
User user = new User();
user.setUsername("tom");
user.setPassword("123456");
user.setId("1");
return user;
} /**
* 创建
* @Description:
* //@RequestBody:json映射到java
* @Valid 和User类上的@NotBlank注解一起做校验
* BindingResult存储的是校验错误信息
* @param @param user
* @param @return
* @return User
* @throws
* @author lihaoyang
* @date 2018年2月24日
*/
@PostMapping
public User create(@Valid @RequestBody User user,BindingResult errors){ if(errors.hasErrors()){
errors.getAllErrors().stream()
.forEach(error -> System.err.println(error.getDefaultMessage()));
} user.setId("1");
System.err.println(user);
return user;
} @PutMapping("/{id:\\d+}")
public User update(@Valid @RequestBody User user,BindingResult errors){ if(errors.hasErrors()){
errors.getAllErrors().stream()
.forEach(error -> System.err.println(error.getDefaultMessage()));
}
System.err.println(user);
return user;
} @DeleteMapping("/{id:\\d+}")
public void delete(@PathVariable String id){
System.err.println("delete method id is >>>>>>>"+id);
} }

到这里只是说了RestfulAPI增删改查的做法,使用查询使用@GetMapping()、新增使用@PostMapping、修改使用@PutMapping、删除使用@DeleteMapping。restful是根据响应的状态码确定接口调用是否成功的。

完整代码放在了github:https://github.com/lhy1234/spring-security

Spring Security构建Rest服务-0200-搭建项目的更多相关文章

  1. Spring Security构建Rest服务-1300-Spring Security OAuth开发APP认证框架之JWT实现单点登录

    基于JWT实现SSO 在淘宝( https://www.taobao.com )上点击登录,已经跳到了 https://login.taobao.com,这是又一个服务器.只要在淘宝登录了,就能直接访 ...

  2. Spring Security构建Rest服务-1202-Spring Security OAuth开发APP认证框架之重构3种登录方式

    SpringSecurityOAuth核心源码解析 蓝色表示接口,绿色表示类 1,TokenEndpoint 整个入口点,相当于一个controller,不同的授权模式获取token的地址都是 /oa ...

  3. Spring Security构建Rest服务-1201-Spring Security OAuth开发APP认证框架之实现服务提供商

    实现服务提供商,就是要实现认证服务器.资源服务器. 现在做的都是app的东西,所以在app项目写代码  认证服务器: 新建 ImoocAuthenticationServerConfig 类,@Ena ...

  4. Spring Security构建Rest服务-1200-SpringSecurity OAuth开发APP认证框架

    基于服务器Session的认证方式: 前边说的用户名密码登录.短信登录.第三方登录,都是普通的登录,是基于服务器Session保存用户信息的登录方式.登录信息都是存在服务器的session(服务器的一 ...

  5. Spring Security构建Rest服务-1001-spring social开发第三方登录之spring social基本原理

    OAuth协议是一个授权协议,目的是让用户在不将服务提供商的用户名密码交给第三方应用的条件下,让第三方应用可以有权限访问用户存在服务提供商上的资源. 接着上一篇说的,在第三方应用获取到用户资源后,如果 ...

  6. Spring Security构建Rest服务-1205-Spring Security OAuth开发APP认证框架之Token处理

    token处理之二使用JWT替换默认的token JWT(Json Web Token) 特点: 1,自包含:jwt token包含有意义的信息 spring security oauth默认生成的t ...

  7. Spring Security构建Rest服务-1204-Spring Security OAuth开发APP认证框架之Token处理

    token处理之一基本参数配置 处理token时间.存储策略,客户端配置等 以前的都是spring security oauth默认的token生成策略,token默认在org.springframe ...

  8. Spring Security构建Rest服务-0900-rememberMe记住我

    Spring security记住我基本原理: 登录的时候,请求发送给过滤器UsernamePasswordAuthenticationFilter,当该过滤器认证成功后,会调用RememberMeS ...

  9. Spring Security构建Rest服务-0800-Spring Security图片验证码

    验证码逻辑 以前在项目中也做过验证码,生成验证码的代码网上有很多,也有一些第三方的jar包也可以生成漂亮的验证码.验证码逻辑很简单,就是在登录页放一个image标签,src指向一个controller ...

随机推荐

  1. stark 增删改

    优雅装饰器 import functools def wrapper(func): @functools.wraps(func) # 保留原函数的信息 def inner(*args, **kwarg ...

  2. 对MVC模型的自悟,详尽解释,为了更多非计算机人员可以理解

    今天小编在复习之前刚刚学会的一个小项目,然后突然对MVC有了新的理解,决定迅速将其写成文档,否则可能会忘记,就算是一个顿悟,学了java语言好久了,刚刚才对其有了比较深入的理解,希望对于同样的人能够有 ...

  3. 日期 时间选择器(DatePicker和TimePicker)实现用户选择

    日期和时间 作者的设计TimePicker时,大小分布不合理,我调整宽度为match-parent高度为wrap-parent就可以了. public class MainActivity exten ...

  4. 如何将word中的图片和文字导入自己的博客中

    目前大部分的博客作者在用Word写博客这件事情上都会遇到以下3个痛点: 1.所有博客平台关闭了文档发布接口,用户无法使用Word,Windows Live Writer等工具来发布博客.使用Word写 ...

  5. (欧拉公式 很水) Coprimes -- sgu -- 1002

    链接: http://vj.acmclub.cn/contest/view.action?cid=168#problem/B Coprimes 时限:250MS     内存:4096KB     6 ...

  6. Java中JNI的使用详解第一篇:HelloWorld

    转自: http://blog.csdn.net/jiangwei0910410003/article/details/17465085 今天开始研究JNI技术,首先还是老套路,输出一个HelloWo ...

  7. java中如何通过Class获取类的属性、方法、注释

    public static String getPKName(Class clazz) { Field[] fields = clazz.getDeclaredFields(); for (Field ...

  8. 团队项目第六周——Alpha阶段项目复审(盐酸队)

    Alpha阶段项目复审 小组 优点 缺点,bug报告 名次 天冷记得穿秋裤队 功能比较新颖,可以离线下载,做的比较完整 在下载电影时容易中断 1 只会嘤嘤嘤队 游戏和记单词的融合,也比较新颖 部分浏览 ...

  9. [mysql] mysql如何实现更新一条记录中某个字段值的一部分呢?

    场景:在平常我们使用word文档等等office办公软件时,我们常会使用搜索->替换的功能. mysql: 在mysql 中有时候,我们也需要这样子的实现: 实现 SQL 语句: update ...

  10. 检测Linux服务器端口是否开通

    现如今云服务器已经是大势所趋,国内比较著名的云服务器厂商有阿里.腾讯,国外有aws,尽管有的公司目前为止还是使用的物理机,但是无论你是使用的云服务器还是物理机,在运行服务时都必不可少的需要监听到指定的 ...