目录
微信小程序端的接口:
所以微信小程序 会传的参数是json格式
现在前端还没写好,先把api定义好,我负责写后端代码
使用如下的
测试接口的工具
Apifox - API 文档、调试、Mock、测试一体化协作平台 - 接口文档工具,接口自动化测试工具,接口Mock工具,API文档工具,API Mock工具,API自动化测试工具
新建项目、创建一个接口
要添加浏览器扩展,不然没法用
添加前置url
例如改为http://localhost:8888
GET请求:select方法
POST请求:insert方法
PUT请求:update方法
DELETE请求:删除方法
get请求填写Params
put请求填写body如下:
要选择json格式
测试的是如下Controller层
@PutMapping("/{id}/house")定义的路径
例如header必须携带token的 ,需要设置如下
怎么设置访问请求要包含自定义的token?
在springMVC中是通过配置xml文件实现的拦截
如下文章:
CRM-拦截器_crm拦截器_素心如月桠的博客-CSDN博客https://blog.csdn.net/m0_47010003/article/details/124887090下面这篇是拦截的源码解析【可不看】
implements HandlerInterceptor{ preHandle(); postHandle(); afterCompletion(); }然后去重写上面的3个方法
主要是写preHandle()方法,在进行增删改查等在handler之前执行
在springboot项目中添加拦截器的方式是添加注解就行,不用配置
WebMvcConfig
@Configuration注解的作用:声明一个类为配置类,用于取代bean.xml配置文件注册bean对象。
把拦截器AuthInitInterceptor 引入,要添加@Resource注解
如下:
- package com.huashang.config;
-
- import javax.annotation.Resource;
-
- import org.springframework.context.annotation.Configuration;
- import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
- import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
- import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
-
- import com.huashang.interceptor.AuthInitInterceptor;
- import com.huashang.interceptor.CrossInterceptor;
- import com.huashang.interceptor.SameUrlDataInterceptor;
-
- @Configuration
- public class WebMvcConfig extends WebMvcConfigurationSupport {
-
- @Resource
- private AuthInitInterceptor authInitInterceptor;
- @Resource
- private CrossInterceptor crossInterceptor;
-
- @Resource
- private SameUrlDataInterceptor sameUrlDataInterceptor;
-
- @Override
- public void addInterceptors(InterceptorRegistry registry) {
- registry.addInterceptor(crossInterceptor).addPathPatterns("/**");
- registry.addInterceptor(authInitInterceptor);
- registry.addInterceptor(sameUrlDataInterceptor);
- registry.addInterceptor(localeChangeInterceptor());
- }
-
- public LocaleChangeInterceptor localeChangeInterceptor() {
- LocaleChangeInterceptor lci = new LocaleChangeInterceptor();
- // 参数名
- lci.setParamName("lang");
- return lci;
- }
-
- }
preHandle方法添加代码如下:
看根据token能不能查到数据
所以我刚刚给我的测试接口,请求头中添加了一个token的参数为数据库中的1234321
可以看到200请求成功了
拦截器代码如下:
- package com.huashang.interceptor;
-
- import java.io.IOException;
-
- import javax.annotation.Resource;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import org.springframework.stereotype.Component;
- import org.springframework.web.servlet.HandlerInterceptor;
- import org.springframework.web.servlet.ModelAndView;
-
- import com.huashang.domain.user.entity.User;
- import com.huashang.service.IUserService;
- import com.huashang.util.StringUtil;
-
- @Component("authInitInterceptor")
- public class AuthInitInterceptor implements HandlerInterceptor{
-
- @Resource
- private IUserService userService;
-
- @Override
- public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
- throws Exception {
- if(ignoreAuth(request)){
- return true;
- }
-
- User currentUser;
- String token = request.getHeader("token");
- if(StringUtil.stringBlank(token)){
- authFail(response);
- return false;
- }
-
- currentUser = userService.loginByToken(token);
- if(currentUser == null){
- authFail(response);
- return false;
- }
-
- request.setAttribute("currentUser", currentUser);
- return true;
- }
-
- @Override
- public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
- ModelAndView modelAndView) throws Exception {
- // do nothing
-
- }
-
- @Override
- public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
- throws Exception {
- request.setAttribute("currentUser", null);
- }
-
- private void authFail(HttpServletResponse response) throws IOException{
- response.setHeader("content-type", "application/json");
- response.setStatus(401);
- response.getWriter().write("{\"error\": \"auth error\"}");
- }
-
- private boolean ignoreAuth(HttpServletRequest request) {
- return request.getMethod().equalsIgnoreCase("options")
- || request.getRequestURI().contains("/users/login")
- || request.getRequestURI().contains("/users/token")
- || request.getRequestURI().contains("/error");
- }
- }