Spring: How to choose response type on run time? -


i want choose response media type on run time in method.

for example, following code:

@requestmapping(value = "/getrecord",     produces = {"application/octet-stream", "application/json;charset=utf-8" }) public byte[] getdata(     @requestparam(value="id", required=true) integer id)     throws ioexception {     if (id == 1)         return createbytearray();     throw new mydataexception(); } 

in code, kind of possible response types 2.

  1. byte[] (by normal execution path)
  2. mydataexception (by exception execution path)

mydataexception later handled exception handler, , converted simple class. can converted json response.

first, thought if provide 2 response types produces option of @requestmapping annotation, message converter convert 2 types according actual return object. not case.

in spring class org.springframework.web.servlet.mvc.method.annotation.abstractmessageconvertermethodprocessor, writewithmessageconverters() method ignores actual return object type when selecting response type if produces option present.

how can let spring choose response type on run time based on actual return object?

another possibility mixed approach between 2 described solutions:

@controlleradvice public class myexceptionhandler extends responseentityexceptionhandler {  @exceptionhandler({ mydataexception.class }) protected responseentity<object> handleinvalidrequest(runtimeexception e, webrequest request) {     mydataexceptionire = (mydataexception) e;     list<fielderrorresource> fielderrorresources = new arraylist<>();      list<fielderror> fielderrors = ire.geterrors().getfielderrors();     (fielderror fielderror : fielderrors) {         fielderrorresource fielderrorresource = new fielderrorresource();         fielderrorresource.setresource(fielderror.getobjectname());         fielderrorresource.setfield(fielderror.getfield());         fielderrorresource.setcode(fielderror.getcode());         fielderrorresource.setmessage(fielderror.getdefaultmessage());         fielderrorresources.add(fielderrorresource);     }      errorresource error = new errorresource("mydataexception", ire.getmessage());     error.setfielderrors(fielderrorresources);      httpheaders headers = new httpheaders();     headers.setcontenttype(mediatype.application_json);      return handleexceptioninternal(e, error, headers, httpstatus.unprocessable_entity, request); }} 

solution proposed on this blog

edit:

i've added fielderror , errorresource classes blog since might deleted in future:

errorresource:

@jsonignoreproperties(ignoreunknown = true) public class errorresource { private string code; private string message; private list<fielderrorresource> fielderrors;  public errorresource() { }  public errorresource(string code, string message) {     this.code = code;     this.message = message; }  public string getcode() { return code; }  public void setcode(string code) { this.code = code; }  public string getmessage() { return message; }  public void setmessage(string message) { this.message = message; }  public list<fielderrorresource> getfielderrors() { return fielderrors; }  public void setfielderrors(list<fielderrorresource> fielderrors) {     this.fielderrors = fielderrors; } } 

fielderrorresource:

@jsonignoreproperties(ignoreunknown = true) public class fielderrorresource { private string resource; private string field; private string code; private string message;  public string getresource() { return resource; }  public void setresource(string resource) { this.resource = resource; }  public string getfield() { return field; }  public void setfield(string field) { this.field = field; }  public string getcode() { return code; }  public void setcode(string code) { this.code = code; }  public string getmessage() { return message; }  public void setmessage(string message) { this.message = message; }} 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -