java - SpringBoot RestController generic POST type -
i'm experimenting building microservices using spring boot.
i have back-end api receives responseentity post requests , processes (saving database etc). data object of self-created class.
now have top-level api (that handles authentication,..). end-users communicate back-end services through top-level api. api has forward requests right back-end api's.
in top api don't want need include classes (e.g. data class in case) , rather send string json data or something. tried this:
@requestmapping(method = requestmethod.post, value="/data") responseentity<string> createunit(@requestbody string data) { uri uri = util.getserviceurl("dataservice"); string url = uri.tostring() + "/data"; responseentity<string> result = resttemplate.postforentity(url, data, string.class); return new responseentity<string>(result.getbody(), httpstatus.ok); }
but results in org.springframework.web.client.httpclienterrorexception: 415 unsupported media type
.
so question is, there way forward these requests back-end without need include object classes in api? figured should able since same when web-browser sends requests in json format without knowing kind of object data is.
the back-end handling looks this:
@requestmapping(method = requestmethod.post, value="/data") responseentity<data> savedata(@requestbody data data) { //some code processes data return new responseentity<data>(dataprocessed, httpstatus.ok); }
when posting string
backend service have specify content-type
header spring knows httpmessageconverter
use deserialize data
object.
with resttemplate
can specify header this:
httpheaders headers = new httpheaders(); headers.setcontenttype(mediatype.application_json); httpentity<string> entity = new httpentity<string>(data, headers); resttemplate.postforentity(url, entity, responsetype);
Comments
Post a Comment