I'm creating a REST service using annotated controllers and the content negotiation (@ResponceBody). I have two different controller methods returning instance of {{Foo}} that serves different use cases and I want the JSON representation of the {{Foo}} to be different for those methods.
For example:
@ResponseBody
public Foo method1() {... return new Foo(123); } // should produce '123'
@ResponseBody
public Foo method2() {... return new Foo(123); } // should produce '{name:"Foo", number:123}'
Of course I could use DTO pattern and return different DTOs in different methods (e.g. {{FooDTO1}} and {{FooDTO2}} respectively) and simply register different JSON serializers for those DTOs. But I wonder if there is a better way, as to me it just feels wrong to define two additional DTO classes and create disposable instances of those classes only in order to apply proper JSON serializers. Can't I just somehow hint to Spring or Jackson which Serializer should be used for which case?