yankee
yankee

Reputation: 40870

How do I start supporting a custom handler method response type in Spring MVC?

I oftentimes want to return an Object as response body which is not supported by spring. So Instead I do:

public HttpEntity<?> doStuff() {
  MyClass myObject = ... ;
  return SomeHelper.toHttpEntity(myObject);
}

While this works, it is uncool, because it makes my code less testable and adds the same call over and over to various handler methods. Thus I wonder if I could add support for an additional type in spring.

I couldn't find anything on Google, but reading through the source code I found the interface HandlerMethodReturnValueHandler whose implementors do the conversion. So I'd implement that interface for my custom type, but how do I register it (using XML) in spring?

Upvotes: 0

Views: 1118

Answers (1)

a better oliver
a better oliver

Reputation: 26858

I don't think that you need your own HandlerMethodReturnValueHandler, but just in case:

<mvc:annotation-driven>
  <mvc:return-value-handlers>
    <bean class="my.own.Handler" />
  </mvc:return-value-handlers>

If mvc is your default namepsace you omit the qualifier, of course.

Upvotes: 1

Related Questions