前言
小编最近在维护项目的过程中,遇到了swagger中不同环境下的同一方法运行效果不同。一、问题背景
自己在写SSM框架中的controller层写方法时,将两个注入@PathVariable与@RequestParam的概念没有理解清楚,导致Tomcat拦截了错误的语法。本地运行成功而在Nginx服务器部署后的环境被拦截。(自己听了公司技术老总的分析后,大体的理解是这样的,可能会有一些偏差。)二、两种注解在后端如何正确使用
1.第一种情况参数都使用@RequestParam注入,那么地址的拼写是不需要加参数的。@RequestMapping(value = {"/findDictionarybyTypeId"},
method = RequestMethod.GET)
@ResponseBody
public ItooResult findDictionarybyTypeId(HttpServletRequest request,
HttpServletResponse response, @RequestParam(required = false, defaultValue = "")
String typeId, @RequestParam(required = false, defaultValue = "") String dictionaryInfo)
在swagger中的展现形式:
2.另一种情况参数使用@PathVariable注入,那么地址的拼写是需要加参数值的。
@RequestMapping(value = {"/exportToExcel/{typeId}"},
method = RequestMethod.GET)
@ResponseBody
public ItooResult exportToExcel(HttpServletRequest request,
HttpServletResponse response, @PathVariable String typeId,
@RequestParam(required = false, defaultValue = "") String dictionaryInfo)
在swagger中的展现形式:
三、两种注解影响前端URL拼写
1.使用@RequestParam注入的地址拼写:参数需要写参数名:'?typeId=' + this.dictionnaryIdurl = 'http://192.168.22.52:8080/single-web/dictionary/findDictionarybyTypeId'
+ '?typeId=' + this.dictionnaryId + '&dictionaryInfo=' + this.info;
2.使用@PathVariable注入的地址拼写:参数直接传值:this.dictionnaryId
const url = 'http://192.168.22.52:8080/single-web/dictionary/exportToExcel/'
+ this.dictionnaryId + '?dictionaryInfo=' + this.info;