Java Spring引导-请求方法';邮政';不支持
我在Spring Boot应用程序中遇到异常Java Spring引导-请求方法';邮政';不支持,java,spring,spring-boot,Java,Spring,Spring Boot,我在Spring Boot应用程序中遇到异常PageNotFound:请求方法“POST”不受支持 这是我的控制器: @RestController public class LoginController { UserWrapper userWrapper = new UserWrapper(); @RequestMapping(value = "/api/login", method = RequestMethod.POST, headers = "Content-type: appli
PageNotFound:请求方法“POST”不受支持
这是我的控制器:
@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请求,但它不起作用。你知道吗
编辑:
用户地址:
public class UserDTO implements Serializable {
private String email;
private String password;
//getters and setters
我将发送:
{
"email":"email@email.com",
"password":"password"
}
我解决了我的问题。我从RequestMapping中删除了头,并为UserWrapper
添加了@Autowired
注释,现在一切都正常了。如果使用JPA,就会出现这个问题。spring引导存储库内置了所有请求。因此,您的请求必须与存储库请求相匹配,这是我发现的唯一一个Spring开机自检有效的示例
关键是您必须匹配存储库Rest调用
您的存储库Iterface将如下所示,再加上没有impl类的任何覆盖
public interface UseerRepository extends CrudRepository<User, Integer>{
}
您的应用程序配置文件应该如下所示:Notice@ComponentScan no 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请求,请在控制器或服务类中执行,我希望我已经解释得足够好,但该示例确实有效,您不必再编写大量粗糙的代码了我通过禁用CSRF解决了此问题
@Configuration
class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
}
}
您发送的内容类型是什么?你在标题中指定了什么?我正在发送json,在标题中我有内容类型:application/json
你能显示UserDTO
类和你正在发布的json
吗?示例数据很好,仅用于结构。我将其添加到Post中,您使用的是哪个web容器?您是如何部署应用程序的?谢谢。修复了org.restlet behing spring boot 2的问题。
@Configuration
class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
}
}