java Spring Boot - 不支持请求方法“POST”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28716632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot - Request method 'POST' not supported
提问by Bartek
I got exception PageNotFound: Request method 'POST' not supported
in my Spring Boot app.
PageNotFound: Request method 'POST' not supported
我的 Spring Boot 应用程序出现异常。
This is my controller:
这是我的控制器:
@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);
}
}
}
I am sending post request at localhost:8080/api/login
but it doesn't work. Have you got any idea?
我正在发送 post 请求,localhost:8080/api/login
但它不起作用。你有什么想法吗?
EDIT:
编辑:
UserDTO:
用户DTO:
public class UserDTO implements Serializable {
private String email;
private String password;
//getters and setters
And json i send:
我发送的json:
{
"email":"[email protected]",
"password":"password"
}
回答by Balaji
I solved this issue by disabling the CSRF.
我通过禁用 CSRF 解决了这个问题。
@Configuration
class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
}
}
回答by Bartek
I solved my problem. I removed headers from RequestMapping and added @Autowired
annotation for UserWrapper
and everything works now.
我解决了我的问题。我从 RequestMapping 中删除了标头并添加了@Autowired
注释,UserWrapper
现在一切正常。
回答by Joe ONeil
This is a problem that occurs if you are using JPA. The spring boot Repository has all the request built in. so your request have to Matchthe Repository requests This is the only example I found that has a working Spring Boot POST https://dzone.com/articles/crud-using-spring-data-restThe key is you have to Matchthe Repository Rest call
如果您使用 JPA,则会出现此问题。Spring Boot Repository 内置了所有请求。因此您的请求必须与 Repository 请求匹配这是我发现的唯一一个具有工作 Spring Boot POST https://dzone.com/articles/crud-using-spring- data-rest关键是你必须匹配Repository Rest 调用
your repository Iterface will look like this plus any override there is no impl class
您的存储库 Iterface 将如下所示加上任何覆盖没有 impl 类
public interface UseerRepository extends CrudRepository<User, Integer>{
}
your controller will look like this. Notice the request value /usersIt is the entity name with an S on the end
你的控制器看起来像这样。注意请求值/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);
}
}
}
Your Application config file should look like this Notice @ComponentScan no basepackage={"com"} If you do that then your JPA does not work
您的应用程序配置文件应如下所示 注意 @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);
}
}
If you want to overwrite a repository POST request do it in the controller or a service class I hope I explained it well enough but that example does work and you do not have to write a lot of crude code anymore
如果您想覆盖存储库 POST 请求,请在控制器或服务类中执行此操作,我希望我解释得足够好,但该示例确实有效,您不必再编写大量粗略的代码