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

6

我在我的Spring Boot应用程序中遇到了异常PageNotFound: Request method 'POST' not supported

这是我的控制器:

@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);
    }
  }
}

我正在向localhost:8080/api/login发送POST请求,但它没有起作用。你有什么想法吗? 编辑: 用户DTO:
public class UserDTO implements Serializable {

private String email;
private String password;
//getters and setters

我发送的JSON数据如下:

{
   "email":"email@email.com",
   "password":"password"
}

你发送的内容类型是什么?你在头部中指定了什么? - EpicPandaForce
我正在发送JSON,在头部中我有Content-type: application/json - Bartek
你能展示一下 UserDTO 类以及你正在发布的 JSON 吗?示例数据就可以,只是为了结构。 - EpicPandaForce
我将其添加到帖子中。 - Bartek
你使用哪个Web容器?你是如何部署你的应用程序的? - Marcelo Keiti
尝试使用@RequestParam捕获您的信息,这样您就可以确定这不是Spring Boot问题。 - LynAs
3个回答

14
我通过禁用CSRF来解决了这个问题。
@Configuration
class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
    }
 }

感谢。已解决 org.restlet 在 Spring Boot 2 中的问题。 - LMC

3

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


0

如果您正在使用JPA,则会出现此问题。Spring Boot Repository已经内置了所有请求,因此您的请求必须匹配Repository请求。这是我找到的唯一一个具有工作Spring Boot POST的示例https://dzone.com/articles/crud-using-spring-data-rest。关键是您必须匹配Repository Rest调用。

您的存储库接口将如下所示,加上任何覆盖,没有实现类。

public interface UseerRepository extends  CrudRepository<User, Integer>{
}

你的控制器将会是这样的。注意请求值/users,它是实体名称加上一个S。

@RestController
public class LoginController {

@RequestMapping("/api/login")//this will return the login page
public String home() {
    return "login";
}
UserWrapper userWrapper = new UserWrapper();
//this will do the post
@RequestMapping(value = "/users", 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);
}
  }
}

你的应用程序配置文件应该看起来像这样 注意 @ComponentScan 没有 basepackage={"com"} 如果你这样做,那么你的 JPA 将无法工作

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration;


@Configuration
@ComponentScan
@EnableJpaRepositories
@Import(RepositoryRestMvcConfiguration.class)
@EnableAutoConfiguration
@PropertySource("application.properties")
public class Application {

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);         
}
}

如果您想覆盖存储库的POST请求,请在控制器或服务类中执行。我希望我已经解释得足够清楚了,但是那个示例确实有效,您不再需要编写大量的粗糙代码。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接