Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
436 views
in Technique[技术] by (71.8m points)

java - Specify message converters for single Spring Controller

I'm writing a postback handler library for a service that has some odd JSON mapping practices. I can very easily configure a custom Jackson ObjectMapper to handle the mapping, but if I register a Spring MVC message converter with that mapper, it applies the mapping (which is incorrect for the rest of the JSON I'm dealing with) to all incoming requests.

There are a handful of manual approaches I can apply to hand-decode these messages, but it would be much cleaner to have the Spring type-conversion service handle it in the request pipeline.

Is there a feasible way to attach a custom message converter to a specific controller, handler method, or mapping string/template/prefix?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had a similar situation - I wanted to create custom @ResponseBody equivalent that would allow me to configure certain aspects of JSON serialization (in my example, I wanted JSON values optionally html encoded). I ended up creating custom @JsonResponseBodyannotation which would cause spring mvc to use differently configured Jackson message converter for methods annotated with it.

I had to extend Spring's AbstractMessageConverterMethodProcessorto register custom handling for my custom annotation and then I needed to register my CustomAbstractMessageConverterMethodProcessor with spring framework by adding it to RequestMappingHandlerAdapter's list of handlers. This was also tricky, because if you use xml config you have to do it via BeanPostProcessor.

At the time I was attempting this (year ago, Spring 3.1), I didn't find any easier way.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...