一、接口測試工具
1、postman下載地址:https://download.csdn.net/download/qq_35792159/11898005
2、谷歌瀏覽器插件:https://www.cnblogs.com/guohu/p/11111911.html
二、第一次測試成功
1、模仿系統的登錄接口,在登陸成功頁面的F12查看接口地址
2、在postman里測試,發現返回的是jsp頁面,本來想偷懶不用寫接口的,看來還是需要自己寫。
3、於是模仿系統里的登錄接口,根據客戶要求的字段寫了一個簡單的登陸接口,結果在訪問的時候被攔截了
4、在JEECG的官方文檔的疑難解答里有排除攔截的設置:http://www.jeecg.org/forum.php?mod=viewthread&tid=1830&extra=page%3D1
在spring-mvc.xml中以下位置添加了自己的地址:
然后就可以使用http://localhost:8080/項目名/appController.do?appLogin&參數測試成功了
三,出現問題,修改再次測試
在我請求網址里,方法是以參數的形式請求的,給客戶測試是發現無法請求成功。再看看以前別人給自己的接口中好像都是http://localhost:8080//app/method這種的
1、研究@RequestMapping
開發者需要在控制器內部為每一個請求動作開發相應的處理方法。org.springframework.web.bind.annotation.RequestMapping 注解類型指示Spring用哪一個類或方法處理請求動作,該注解可用於類和方法。
@RequestMapping可以用來注釋一個控制器類,在這種情況下,所有方法都將映射為相對於類級別的請求,表示該控制器處理的所有請求都被映射到value屬性所指示的路徑下 參數參數
參數介紹參考網址:https://blog.csdn.net/qq_36285124/article/details/54289312
我只用到了value和params兩個參數,所以就記錄下這兩個吧。
- value的使用
@RequestMapping(value="list") public String getEmployeeData(ModelAndView mav,HttpServletRequest request,HttpServletResponse response){ System.out.println("============"); return "test"; }
訪問的地址可以是/xx項目名/xxcontroller名/list
- params的使用
@RequestMapping(value="list",params="one",method=RequestMethod.GET) public String getEmployeeData(ModelAndView mav,HttpServletRequest request,HttpServletResponse response){ System.out.println("============"); return "test"; }
這種方式如果兩個方法的value值相同,只需要加上參數one即可區別,訪問的地址為/xx項目名/xxcontroller名/list?one
或者
@RequestMapping(params="list",method=RequestMethod.GET) public String getEmployeeData(ModelAndView mav,HttpServletRequest request,HttpServletResponse response){ System.out.println("============"); return "test"; }
這種方式要訪問該方法的地址為:/xx項目名/xxcontroller名?list,沒錯,這里沒有方法名,list只是參數。
2、一開始以為換了參數使用value,還是像之前的配置呢樣配置過濾,結果卻發現不管怎么配置,都是無法找到網頁。
3、請教了一個使用這個框架很熟練的大神,才知道應該在web.xml中配置
<!-- API開放接口配置 --> <servlet> <servlet-name>APISpringMvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <description>spring mvc 配置文件</description> <param-name>contextConfigLocation</param-name> <param-value>classpath*:spring-mvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>APISpringMvc</servlet-name>
<url-pattern>/appController/*</url-pattern> </servlet-mapping>
備注:最好不要用<url-pattern>/</url-pattern>,可能會導致其他頁面被攔截
4、測試源碼:
@Controller @RequestMapping("/appController") public class AppController { // 設置方法下的子路經 @JAuth(auth=Permission.SKIP_AUTH)//放開權限 @RequestMapping("/method") @ResponseBody public String helloworld() { return "helloWorld"; } }
- 請求網址:http://localhost/項目名/appController/method
- @JAuth(auth=Permission.SKIP_AUTH)的作用是設置不讓系統攔截請求
- @ResponseBody 的使用和不使用的區別:不使用的話只能返回界面,無法返回數據,像例子中的請求是無法返回的。
四、拓展
1、@RequestMapping @ResponseBody 和 @RequestBody 注解的用法與區別
2、使用框架時,在web.xml中配置servlet時,攔截請求/和/*的區別。
3、Java測試接口,參數為對象的簡單測試方式 (這個我一開始有用過,但是后來發現好像不用也可以直接傳參數,留着下次看)