Spring Boot和JWT-找不到发布请求返回页面

时间:2019-03-15 09:46:50

标签: spring-boot jwt

我正在尝试在Spring Boot应用程序中实现JWT(在this教程之后),并且我有一个UserController页面,如下所示:

@RestController
@RequestMapping("/users")
public class UserController {



    @Autowired
    private ApplicationUserRepository applicationUserRepository;



    private BCryptPasswordEncoder bCryptPasswordEncoder;



    public UserController(ApplicationUserRepository applicationUserRepository,
            BCryptPasswordEncoder bCryptPasswordEncoder) {
        this.applicationUserRepository = applicationUserRepository;
        this.bCryptPasswordEncoder = bCryptPasswordEncoder;
    }



    @PostMapping("/users/sign-up")
    public void signUp(@RequestBody ApplicationUser user) {
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        applicationUserRepository.save(user); //Salvataggio sul DB
    } 

}

使用邮递员,如果我对第http://localhost:8080/users/sign-up页进行POST请求,则会返回404页面未找到错误。
我不知道在哪里寻找错误或缺少的部分。我与本教程所做的唯一不同的事情是为我的(exclude = { SecurityAutoConfiguration.class })添加了@SpringBootApplication以禁用默认登录页面。

1 个答案:

答案 0 :(得分:0)

错误在这里:

@PostMapping("/users/sign-up")

这是因为您的rest控制器已经映射到/users

替换为:

@PostMapping("sign-up")

这样做:

@RestController
@RequestMapping("/users")
public class UserController {


    @Autowired
    private ApplicationUserRepository applicationUserRepository;



    private BCryptPasswordEncoder bCryptPasswordEncoder;



    public UserController(ApplicationUserRepository applicationUserRepository,
        BCryptPasswordEncoder bCryptPasswordEncoder) {
        this.applicationUserRepository = applicationUserRepository;
        this.bCryptPasswordEncoder = bCryptPasswordEncoder;
    }



    @PostMapping("sign-up")
    public void signUp(@RequestBody ApplicationUser user) {
        user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
        applicationUserRepository.save(user); //Salvataggio sul DB
    } 

}

您是否正确发出邮递员请求? 试试:

curl -H "Content-Type: application/json" -X POST -d '{
    "username": "admin",
    "password": "password"
}' http://localhost:8080/users/sign-up

,您将得到一个404,然后检查项目的其他文件。