出现404原因肯定是找不到该页面
原因1、Application一定要在写代码的class文件之上,也就是说Application要包含你写的代码文件

在写POST之前先写一个简单的可访问页面HelloWorld
访问:http://localhost:8081/hello 尝试打开当前页面
@RestController
public class HelloWorldController {
@RequestMapping("/hello")
public String index() {
return "Hello World";
}
}以上方式的编写原因只为避免踩到无用的坑,首先确保我们的配置环境等没有问题。然后开始真正编写我们需要的内容
在此之后,开始POST接口编写之旅
package com.controller;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
@RestController
public class HelloWorldController {
@RequestMapping(value = "/hello",method = RequestMethod.POST)
@ApiOperation(value = "登录接口,成功登录后获取cookies", httpMethod = "POST")
public String index(HttpServletResponse response, @RequestParam (value = "username", required = true) String userName,
@RequestParam(value = "password", required = true) String password) {
if (userName.equals("zhangshan") && password.equals("111111")) {
// cookie = new Cookie("login", "true");
// response.addCookie(cookie);
return "恭喜你登录成功";
} else
return "用户名或密码错误";
}
}只需要一个class文件即可测试POST是否成功
测试时,填写的内容为body格式,不要写错了。
版权声明:本文为weixin_51019823原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。