简体   繁体   中英

Spring boot REST API Method Conflict when same url having GET and POST methods

I am working on a REST API and having the below issue when it is deployed to the Azure cloud. There are two REST APIs, one is for GET and one is for POST only difference is the method parameters as below.

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v2")
public class PatientController {

  @GetMapping("/patients/{patientId}/settings")
  Page<PatientDto> getPatient(@PathVariable String patientId, Pageable pageable) {
    return patientService.getPatient(patientId, pageable);
  }


  @PostMapping("/patients/{patientId}/settings")
  PatientDto createPatient(
      @PathVariable String patientId, @Valid @RequestBody PatientRequestDto PatientRequestDto) {
    return patientService.createPatient(patientId, patientRequestDto);
  }
}

Now the problem is when I call for the GET method, it returns 405 Method Not Allowed with below as a response header.

Response Headers
Content-Type: application/json
Date: Wed, 21 Dec 2022 12:31:16 GMT
Allow: POST
Cache-Control: no-store, must-revalidate, no-cache, max-age=0
Expires: 0
Pragma: no-cache

It seems like GET and POST methods have conflicts and only the POST method is recognized. But the strange thing is when I run this on my localhost, it works as expected with http://localhost:8080 but after deploying to the Azure cloud environment, this issue occurs. I am using Springboot 4.7.x.

Appreciate your kind support on this.Thank you!

Most probably you have enabled Cross-site request forgery (CSRF) . You need either provide _csrf parameter in the request or disable CSRF using Spring Security Configuration:

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf().disable();
    }
}

See Spring Security documentation for CSRF for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM