java - Multiple @GET for different MIME - how to consume this with plain HttpURLConnection() -


i realized possible define in restful resource .java file:

@get @produces("text/plain") public string getplaintexthello() { ... }  @get @produces("application/json") public string getjsonhello() { ... } 

isn't fantastic? wait moment....

problem

i consuming api simple client. code of httpurlconnection:

url obj = new url("http://some.url/res/hello"); httpurlconnection conn = (httpurlconnection) obj.openconnection(); conn.setrequestmethod("get"); ... /* response ... conn.getinputstream() */ 

how server 'know' 1 method call serve client?

regards.

first of should consider using same method different types of "produces":

@get  @produces({ "application/xml", "text/plain"})  public string gethello() { ... } 

the different types of "produces" handled jaxb (in case response object...).

you can define client side "accept" mime type using:

string uri =      "http://localhost:8080/hello/"; url url = new url(uri); httpurlconnection connection =      (httpurlconnection) url.openconnection(); connection.setrequestmethod("get"); connection.setrequestproperty("accept", "application/json"); 

this question provides more insights (and other client side frameworks) related problem: rest. jersey. how programmatically choose type return: json or xml?


Comments

Popular posts from this blog

php - get table cell data from and place a copy in another table -

javascript - Mootools wait with Fx.Morph start -

php - Navigate throught databse rows -