Spring for Android. MediaType throws IllegalArgumentException: Invalid token character ';' in token "json;charset=utf-8" -
i'm trying use spring android jackson 2 create pojo rest call.
// set accept header httpheaders requestheaders = new httpheaders(); requestheaders.setaccept(collections.singletonlist(new mediatype("application","vnd.livescore_app.api.v1+json"))); requestheaders.setcontenttype(new mediatype("application","json;charset=utf-8")); httpentity<?> requestentity = new httpentity<object>(requestheaders); // create new resttemplate instance resttemplate resttemplate = new resttemplate(); // add jackson message converter resttemplate.getmessageconverters().add( new mappingjackson2httpmessageconverter()); // make http request, marshaling response json array of events responseentity<appversionobject> responseentity = resttemplate.exchange("https://example.com", httpmethod.get, requestentity, appversionobject.class); appversionobject object = responseentity.getbody();
crashes following log trace
05-13 10:55:42.656: e/androidruntime(19401): fatal exception: thread-1470 05-13 10:55:42.656: e/androidruntime(19401): java.lang.illegalargumentexception:invalid token character ';' in token "json;charset=utf-8" 05-13 10:55:42.656: e/androidruntime(19401): @ org.springframework.http.mediatype.checktoken(mediatype.java:377) 05-13 10:55:42.656: e/androidruntime(19401): @ org.springframework.http.mediatype.<init>(mediatype.java:351) 05-13 10:55:42.656: e/androidruntime(19401): @ org.springframework.http.mediatype.<init>(mediatype.java:303) 05-13 10:55:42.656: e/androidruntime(19401): @ com.madinsweden.livescoretennis.service.checkappversiontask.run(checkappversiontask.java:41) 05-13 10:55:42.656: e/androidruntime(19401): @ java.lang.thread.run(thread.java:856)
i may of course change content type more standardized application/json
following error.
05-13 11:03:07.054: e/androidruntime(20010): org.springframework.web.client.restclientexception: not extract response: no suitable httpmessageconverter found response type [com.madinsweden.livescoretennis.backend.appversionobject] , content type [application/vnd.livescore_app.api.v1+json;charset=utf-8] 05-13 11:03:07.054: e/androidruntime(20010): @ org.springframework.web.client.httpmessageconverterextractor.extractdata(httpmessageconverterextractor.java:79) 05-13 11:03:07.054: e/androidruntime(20010): @ org.springframework.web.client.resttemplate$responseentityresponseextractor.extractdata(resttemplate.java:655) 05-13 11:03:07.054: e/androidruntime(20010): @ org.springframework.web.client.resttemplate$responseentityresponseextractor.extractdata(rest template.java:641) 05-13 11:03:07.054: e/androidruntime(20010): @ org.springframework.web.client.resttemplate.doexecute(resttemplate.java:484) 05-13 11:03:07.054: e/androidruntime(20010): @ org.springframework.web.client.resttemplate.execute(resttemplate.java:439) 05-13 11:03:07.054: e/androidruntime(20010): @ org.springframework.web.client.resttemplate.exchange(resttemplate.java:415) 05-13 11:03:07.054: e/androidruntime(20010): @ com.madinsweden.livescoretennis.service.checkappversiontask.run(checkappversiontask.java:51) 05-13 11:03:07.054: e/androidruntime(20010): @ java.lang.thread.run(thread.java:856)
this line of code:
requestheaders.setcontenttype( new mediatype("application","json;charset=utf-8"));
is incorrect, because constructor designed take type , subtype, , json;charset=utf-8
not valid subtype (rather, concatenation between subtype , type parameter). want using overloaded constructor accepts type, subtype, , parameter map:
final map<string, string> parametermap = new hashmap<string, string>(4); parametermap.put("charset", "utf-8"); requestheaders.setcontenttype( new mediatype("application","json", parametermap));
this yields correct 'content-type':
application/json;charset="utf-8"
second problem specifying custom response type (application/vnd.livescore_app.api.v1+json
), spring not have http message converter. either have write own (as highlighted here), or change response type 'application/json'.
Comments
Post a Comment