How to auto generate response fields that do not have POJO

70 views Asked by At

We have a service that simply returns the json document on a GET request. Since we do not have the POJO for the response "model", it appears we won't be able to use the auto response fields generation "goodness".

One option for us is to create the Pojos (quite large, about 50 attributes) and a corresponding controller that uses the pojos. This is awkward as we now have to maintain the model and corresponding controller just so we can auto generate the model.

Any ideas on how we can still leverage some auto generation of the response fields would be greatly appreciated.

Here's the controller I'm referring to:

@RestController
@RequestMapping("/api")
public class ProductController {

   @Autowired
   ProductService productService;

   @RequestMapping(value = { "/products/{ids}" }, method = { RequestMethod.GET },
         produces = "application/json", headers={"accept=application/json"})
   @Timed
   @ExceptionMetered
   @LogExecutionTime
   public String getProductDetails(@PathVariable("id") String id)  {
      return productService.getProductDetails(id);
   }
1

There are 1 answers

0
Florian Benz On

At the moment I see no way of leveraging the auto generation without putting additional effort into it. Spring Auto REST Docs works by inspecting POJOs with a Jackson visitor (static introspection without runtime information) and there is currently no way of deriving the JSON fields from a string (would be dynamic at runtime). Thus, I only see two options:

  1. The approach that you already described: Creating the corresponding POJO and using it.
  2. Using Spring REST Docs for the corresponding test and manually document each field in the test. Might be the better option here if you do not want to alter the production code.