Spring/Eureka/Feign - FeignClient setting Content-Type header to application/x-www-form-urlencoded -


when use feignclient setting content-type application/x-www-form-urlencoded instead of application/json;charset=utf-8.

if use resttemplate send same message message header content-type correctly set application/json;charset=utf-8.

both feignclient , resttemplate using eureka service discovery, , discovered problem debugging http message received server.

the controller on server side looks this:

@restcontroller @requestmapping("/site/alarm") public class sitealarmcontroller {     @requestmapping(method = requestmethod.post)     @responsebody     public responseentity<raisealarmresponsedto> raisealarm(@requestbody raisesitealarmrequestdto requestdto) {         ...     } 

my feignclient interface in service calls alarm looks this:

@feignclient("alarm-service") public interface alarmfeignservice {     @requestmapping(method = requestmethod.post, value = "/site/alarm")     raisealarmresponsedto raisealarm(@requestbody raisesitealarmrequestdto requestdto); } 

the http message headers feignclient are:

accept: */* cache-control: no-cache pragma: no-cache user-agent: java/1.7.0_60 host: smit005s-macbook-pro.local:9120 connection: keep-alive content-type: application/x-www-form-urlencoded content-length: 323 

the alarm service doesn't content-type , throws following exception:

2015-04-22 12:12:28.580 thread="qtp1774842986-25" class="org.eclipse.jetty.servlet.servlethandler" level="warn"  org.springframework.web.util.nestedservletexception: request processing failed; nested exception feign.feignexception: status 415 reading alarmfeignservice#raisealarm(raisesitealarmrequestdto); content: {"timestamp":1429701148576,"status":415,"error":"unsupported media type","exception":"org.springframework.web.httpmediatypenotsupportedexception","message":"unsupported media type","path":"/site/alarm"}     @ org.springframework.web.servlet.frameworkservlet.processrequest(frameworkservlet.java:978) ~[spring-webmvc-4.1.5.release.jar:4.1.5.release]     @ org.springframework.web.servlet.frameworkservlet.doget(frameworkservlet.java:857) ~[spring-webmvc-4.1.5.release.jar:4.1.5.release]     @ javax.servlet.http.httpservlet.service(httpservlet.java:618) ~[tomcat-embed-core-8.0.20.jar:8.0.20]     ...     ... /* commented rest of stack out */     ... 

if change client side code use resttemplate follows:

@service public class alarmservice {     @autowired     private resttemplate resttemplate; ...     public void send(raisesitealarmrequestdto alarm) {         raisealarmresponsedto result = resttemplate.postforobject("http://alarm-service/site/alarm",              raisesitealarmrequestdto, raisealarmresponsedto.class);     } } 

it works resttemplate, alarm-service receives message , processes successfully. message headers sent resttemplate are:

accept: application/json, application/*+json content-type: application/json;charset=utf-8 cache-control: no-cache pragma: no-cache user-agent: java/1.7.0_60 host: smit005s-macbook-pro.local:9120 connection: keep-alive content-length: 323 

the answer @spencergibb suggests; use consumes directive in @requestmapping annotation on feignclient interface. spring/netflix documentaition has example.

so example @feignclient interface declaration in client now:

@feignclient("alarm-service") public interface alarmfeignservice {     @requestmapping(method = requestmethod.post, value = "/site/alarm", consumes = "application/json"))     raisealarmresponsedto raisealarm(raisesitealarmrequestdto requestdto); } 

note necessary on client side , server side controller not need have change.

would nice if done default on @feignclient , consistent resttemplate , server side controller @requestmapping annotation. maybe can done in future release of spring-cloud.


Comments

Popular posts from this blog

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - UML - How would you draw a try catch in a sequence diagram? -

c++ - No viable overloaded operator for references a map -