springswagger-uispringfox

Add a header parameter in Swagger UI documentation with Springfox


I want to add a header parameter field in the auto-generated swagger ui documentation of my rest service. I use Spring and Springfox.

public ResponseEntity<User> saveNewUser(
        @ApiParam(value = "the user to create", required = true) @RequestBody User user) throws RestServiceException {

    userService.save(user);
    return new ResponseEntity<User>(user, HttpStatus.OK);
}

As you see I have already a body type parameter. I just want to add a header type one.


Solution

  • I just added @RequestHeader(value="myHeader") String headerStr :

    public ResponseEntity<User> saveNewUser(
            @RequestHeader(value="myHeader") String headerStr,
            @ApiParam(value = "the user to create", required = true) @RequestBody User user) throws RestServiceException {
    
        userService.save(user);
        return new ResponseEntity<User>(user, HttpStatus.OK);
    }
    

    (import org.springframework.web.bind.annotation.RequestHeader;)

    You can also add a global header on every service in your documentation with the solution described here : Spring + Springfox + Header Parameters