2

I have integrated swagger to my Spring Boot project using SpringFox build and it work for my GET API. However on my POST API which requires the request in application/json format, the swagger-ui doesn't set the header.

My Code:

@PostMapping(value="/login", consumes="application/json", produces="application/json")
    public WsResponse login(UserLoginRequest requestBody) throws Exception {

        validateAuthToken(requestBody.getId(), requestBody.getToken());

        return serviceWs.login(requestBody);
    }

The command generated from swagger-ui:

curl -X POST "http://localhost:8080/api/login?token=2342343324&username=23434&password=123" -H "accept: application/json"

Note that the header "Content-Type" is not set and thus backend is giving an error when trying to call.

The issue is similar to this post but there's no resolution, any help is appreciated.

ipohfly
  • 1,959
  • 6
  • 30
  • 57

2 Answers2

0

Try putting application/json within the braces:

Swagger definition

Swagger UI

Note: Works for me for springfox-swagger2 and springfox-swagger-ui v2.8.0

Valijon
  • 12,667
  • 4
  • 34
  • 67
  • The content type is omitted in springdoc's swagger UI (v1.4.3) requests, regardless of whether the braces are present. – johnnieb Sep 24 '20 at 00:33
0

Add @RequestBody annotation in your post controller

e.g

 @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<Student> add(@Valid @RequestBody Student student) {
        
        return new ResponseEntity<>(service.add(student), HttpStatus.CREATED);
    }
Ishan Liyanage
  • 2,237
  • 1
  • 26
  • 25