I have inherited a REST API and I'm looking at the best way to refactor it so that I can separate out the different architectural parts to aid:

  • making the code base leaner, cleaner and tidier
  • further refactoring
  • unit testing
  • reducing lock-in to 3rd party validation, JSON and REST libraries

Currently there are 3 layers, and the outermost layer is a set of services with methods inputting and outputting JSON with Gson. I manipulate the JSON output significantly using Gson, e.g. excluding database IDs (by choosing which pre-built Gson engine to use), formatting dates (by annotating the pojos), processing or ignoring nested object types (via engine again).

The service method signatures are annotated with Spring REST to create the REST endpoints and a lot of validation is done here automatically, but not all of course. The remaining validation is done in code but I'm looking at validation libraries to take over all the validation and allow better unit test coverage.

There is also a certain amount of messiness due to the initialisation of the pojo object graph on incoming data in the places where it's needed. I don't want to employ an ORM framework, due to performance concerns, but it does need structuring better.

In my ideal world, every endpoint would allow both JSON string-based interaction, and also strongly-typed interaction for unit tests, although I can't see how I can do that without having 2 signatures for every endpoint, a second layer on top of the services or an XML file-based declaration for the JSON serialization/deserialization. None of those 3 appeal to me.

With the codebase in the state it is in, all of these concerns are entwined far too much.

  • 1
    This could be a good conversation. I know this stuff, use it a lot, and spent 10 minutes thinking about how to write a helpful answer, but I decided the question is too broad to write a sufficiently concise answer. If you split it to some narrower questions like, how to unit test rest end points, or how to abstract away json or rest libraries there could be some good answers. – joshp Mar 2 at 7:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.