Spring Boot - 不支持请求方法'POST'

Bar*_*tek 4 java spring spring-boot

PageNotFound: Request method 'POST' not supported我的Spring Boot应用程序中出现异常.

这是我的控制器:

@RestController
public class LoginController {

UserWrapper userWrapper = new UserWrapper();

@RequestMapping(value = "/api/login", method = RequestMethod.POST, headers = "Content-type: application/*")
public @ResponseBody ResponseEntity getCredentials(@RequestBody UserDTO userDTO) {

    User user = userWrapper.wrapUser(userDTO);
    if (userDTO.getPassword().equals(user.getPassword())) {
        return new ResponseEntity(HttpStatus.OK);
    } else {
        return new ResponseEntity(HttpStatus.BAD_REQUEST);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我发送邮件请求,localhost:8080/api/login但它不起作用.你有什么想法吗?

编辑:

UserDTO:

public class UserDTO implements Serializable {

private String email;
private String password;
//getters and setters
Run Code Online (Sandbox Code Playgroud)

和json我发送:

{
   "email":"email@email.com",
   "password":"password"
}
Run Code Online (Sandbox Code Playgroud)

Bal*_*aji 10

我通过禁用CSRF解决了这个问题.

@Configuration
class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
    }
 }
Run Code Online (Sandbox Code Playgroud)


Bar*_*tek 3

我解决了我的问题。我从 RequestMapping 中删除了标头并添加了@Autowired注释UserWrapper,现在一切正常。