복붙노트

[SPRING] 스프링 부트 - 요청 방법 'POST'가 지원되지 않습니다.

SPRING

스프링 부트 - 요청 방법 'POST'가 지원되지 않습니다.

예외가 발생했습니다. PageNotFound : 요청 메소드 'POST'가 Spring Boot App에서 지원되지 않습니다.

이것은 내 컨트롤러 다.

@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에서 게시물 요청을 보내고 있지만 작동하지 않습니다. 어떤 생각있어?

편집하다:

UserDTO :

public class UserDTO implements Serializable {

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

그리고 json 나는 보낸다 :

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

해결법

  1. ==============================

    1.CSRF를 비활성화하여이 문제를 해결했습니다.

    CSRF를 비활성화하여이 문제를 해결했습니다.

    @Configuration
    class SecurityConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().disable();
        }
     }
    
  2. ==============================

    2.나는 내 문제를 해결했다. RequestMapping에서 헤더를 제거하고 UserWrapper에 @Autowired 주석을 추가했습니다. 이제 모든 것이 작동합니다.

    나는 내 문제를 해결했다. RequestMapping에서 헤더를 제거하고 UserWrapper에 @Autowired 주석을 추가했습니다. 이제 모든 것이 작동합니다.

  3. ==============================

    3.이것은 JPA를 사용하는 경우 발생하는 문제입니다. 스프링 부트 저장소에는 모든 요청이 내장되어 있습니다. 따라서 요청은 리포지토리 요청과 일치해야합니다. 이는 스프링 부트 POST가 작동하는 유일한 예입니다 https://dzone.com/articles/crud-using-spring-data-rest 열쇠는 리포지토리 나머지 호출을 일치시켜야한다는 것입니다.

    이것은 JPA를 사용하는 경우 발생하는 문제입니다. 스프링 부트 저장소에는 모든 요청이 내장되어 있습니다. 따라서 요청은 리포지토리 요청과 일치해야합니다. 이는 스프링 부트 POST가 작동하는 유일한 예입니다 https://dzone.com/articles/crud-using-spring-data-rest 열쇠는 리포지토리 나머지 호출을 일치시켜야한다는 것입니다.

    리파지토리 Iterface는 이와 같이 보일 것이다.

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

    컨트롤러가 이렇게 보일 것입니다. 요청 값 / 사용자를 확인하십시오. 끝에 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 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 요청을 컨트롤러 나 서비스 클래스에서 덮어 쓰고 싶다면 충분히 설명했으면 좋겠지 만 그 예제가 작동하고 더 이상 원유 코드를 많이 작성할 필요가 없습니다.

  4. from https://stackoverflow.com/questions/28716632/spring-boot-request-method-post-not-supported by cc-by-sa and MIT license