Rest Custom HTTP Message Converter Spring Boot 1.2.3 -
i want create custom of httpmessageconverter using rest, json, spring boot 1.2.3 , spring 4, custom httpmessageconverter its' never called.
i have preformed following steps :
1: created class extends abstracthttpmessageconverter
@component public class productconverter extends abstracthttpmessageconverter<employee> { public productconverter() { super(new mediatype("application", "json", charset.forname("utf-8"))); system.out.println("created "); } @override protected boolean supports(class<?> clazz) { return false; } @override protected employee readinternal(class<? extends employee> clazz, httpinputmessage inputmessage) throws ioexception, httpmessagenotreadableexception { inputstream inputstream = inputmessage.getbody(); system.out.println("test******"); return null; } @override protected void writeinternal(employee t, httpoutputmessage outputmessage) throws ioexception, httpmessagenotwritableexception { // todo auto-generated method stu }
}
2: create configuration class register httpmessageconverters
@configuration public class webmvcconfig extends webmvcconfigureradapter{ @override public void configuremessageconverters(list<httpmessageconverter<?>> converters) { system.out.println("configure message converters"); converters.add(new productconverter()); super.configuremessageconverters(converters); //super.extendmessageconverters(converters); } }
3: rest class method
@requestmapping(value="/{categoryid}" ,method=requestmethod.post, consumes="application/json") @preauthorize("permitall") public responseentity<productentity> saveproduct(@requestbody employee employee , @pathvariable long categoryid) { logger.log(level.info, "category id: {0}" , categoryid); responseentity<productentity> responseentity = new responseentity<productentity>(httpstatus.ok); return responseentity; }
my custom httpmessagecoverter it's created never called ? there configuration or step i'm missing ? input or advice appreciated.
after overriding (abstracthttpmessageconverter) class methods, found out there's 2 annotations achieving polymorphism @jsontypeinfo , @jsonsubtypes. wants achieve polymorphism can use these 2 annotations.
i believe want configure these message converters using configuremessageconverters method in configuration class extends webmvcconfigureradapter. i've done myself converter csv content. i've included code below. this link shows example well. this link may helpful. seems spring configuration not clear on best place configure things. :) let me know if helps.
@configuration public class applicationwebconfiguration extends webmvcconfigureradapter { @override public void configuremessageconverters(list<httpmessageconverter<?>> converters) { super.configuremessageconverters(converters); converters.add(new csvmessageconverter()); } }
you need top modify supports() method return true classes supported converter. see the spring doc abstracthttpmessageconverter supports method.
Comments
Post a Comment