That means I should send JSON as a String if I need to send a JSON request to my service? If I'm not using curl or a rest client, do I need to convert the json object in to a string and send it to the service?
On Wed, May 4, 2016 at 11:03 AM, Afkham Azeez <[email protected]> wrote: > Remove the media type. You are specifying that you are consuming JSON but > sending text. Remove the JSON media type and send your request from curl > without a media type header. > > On Wed, May 4, 2016 at 10:15 AM, Yasith Lokuge <[email protected]> wrote: > >> Hi, >> >> I'm still getting the following error, >> >> Problem accessing: /service/2. Reason: Unsupported Media Type >> >> Please not that I've used the following annotation, >> >> @Consumes(MediaType.APPLICATION_JSON) >> >> >> On Tue, May 3, 2016 at 6:32 PM, Afkham Azeez <[email protected]> wrote: >> >>> Change your curl to: >>> curl -X POST -d '{"symbol": "GOOG","name": "Google Inc.","high": >>> 190.23,"low": 187.45}' "http://localhost:8080/hello2/2" >>> >>> InputStream doesn't work like that in MSF4J. Check the streaming sample >>> for details https://github.com/wso2/msf4j/tree/master/samples/fileserver >>> >>> >>> On Tue, May 3, 2016 at 4:53 PM, Yasith Lokuge <[email protected]> wrote: >>> >>>> Hi, >>>> >>>> I tried to send a json post request to the sample microservice I built >>>> using wso2 ms4j framework. >>>> >>>> It works with the pojo class as in the below example (POST service/1) >>>> but doesn't work with the String type and InputStream type. >>>> >>>> Please refer the following java code / curl request and the error I >>>> received. >>>> >>>> >>>> package org.example.service; >>>> import java.io.BufferedReader;import java.io.InputStream;import >>>> java.io.InputStreamReader; >>>> import javax.ws.rs.Consumes;import javax.ws.rs.GET;import >>>> javax.ws.rs.POST;import javax.ws.rs.Path;import javax.ws.rs.core.MediaType; >>>> @Path("/service")public class HelloService { >>>> >>>> @GET >>>> @Path("/") >>>> public String get() { >>>> System.out.println("GET invoked"); >>>> return "Hello from WSO2 MSF4J"; >>>> } >>>> >>>> @POST >>>> @Path("/1") >>>> @Consumes(MediaType.APPLICATION_JSON) >>>> public void postOne(Stock stock) { >>>> >>>> System.out.println("POST_1 invoked"); >>>> System.out.println(stock.getName()); >>>> } >>>> >>>> @POST >>>> @Path("/2") >>>> @Consumes(MediaType.APPLICATION_JSON) >>>> public void postTwo(String jsonbody) { >>>> >>>> System.out.println("POST_2 invoked"); >>>> System.out.println(jsonbody); >>>> } >>>> >>>> @POST >>>> @Path("/3") >>>> @Consumes(MediaType.APPLICATION_JSON) >>>> public void postThree(InputStream jsonData) { >>>> StringBuilder stringData = new StringBuilder(); >>>> try { >>>> BufferedReader in = new BufferedReader(new >>>> InputStreamReader(jsonData)); >>>> String line = null; >>>> while ((line = in.readLine()) != null) { >>>> stringData.append(line); >>>> } >>>> } catch (Exception e) { >>>> e.printStackTrace(); >>>> } >>>> System.out.println("POST_3 invoked"); >>>> System.out.println(stringData.toString()); >>>> } >>>> } >>>> >>>> >>>> >>>> curl -X POST -H "Content-Type: application/json" -H "Cache-Control: >>>> no-cache" -d '{"symbol": "GOOG","name": "Google Inc.","high": >>>> 190.23,"low": 187.45}' "http://localhost:8080/service/2" >>>> >>>> Error in executing request: POST /service/2 >>>> >>>> curl -X POST -H "Content-Type: application/json" -H "Cache-Control: >>>> no-cache" -d '{"symbol": "GOOG","name": "Google Inc.","high": >>>> 190.23,"low": 187.45}' "http://localhost:8080/service/3" >>>> >>>> Error in executing request: POST /service/3 >>>> >>>> *Plain Text* >>>> >>>> >>>> >>>> curl -X POST -H "Content-Type: text/plain" -H "Cache-Control: no-cache" -d >>>> '{"symbol": "GOOG","name": "Google Inc.","high": 190.23,"low": 187.45}' >>>> "http://localhost:8080/service/2" >>>> >>>> Problem accessing: /service/2. Reason: Unsupported Media Type >>>> >>>> curl -X POST -H "Content-Type: text/plain" -H "Cache-Control: no-cache" -d >>>> '{"symbol": "GOOG","name": "Google Inc.","high": 190.23,"low": 187.45}' >>>> "http://localhost:8080/service/3" >>>> >>>> Problem accessing: /service/3. Reason: Unsupported Media Type >>>> >>>> -- >>>> *Regards,* >>>> *-Yasith Kanchana Lokuge-* >>>> >>>> website : http://www.techsurgeinnovations.com/ >>>> facebook : https://www.facebook.com/yasithlokuge >>>> linkedin : http://lk.linkedin.com/pub/yasith-lokuge/1b/713/229/ >>>> Mobile : +94771353682 >>>> Skype : yasith11 >>>> Twitter : @YasithLokuge >>>> >>>> _______________________________________________ >>>> Dev mailing list >>>> [email protected] >>>> http://wso2.org/cgi-bin/mailman/listinfo/dev >>>> >>>> >>> >>> >>> -- >>> *Afkham Azeez* >>> Director of Architecture; WSO2, Inc.; http://wso2.com >>> Member; Apache Software Foundation; http://www.apache.org/ >>> * <http://www.apache.org/>* >>> *email: **[email protected]* <[email protected]> >>> * cell: +94 77 3320919 <%2B94%2077%203320919>blog: * >>> *http://blog.afkham.org* <http://blog.afkham.org> >>> *twitter: **http://twitter.com/afkham_azeez* >>> <http://twitter.com/afkham_azeez> >>> *linked-in: **http://lk.linkedin.com/in/afkhamazeez >>> <http://lk.linkedin.com/in/afkhamazeez>* >>> >>> *Lean . Enterprise . Middleware* >>> >> >> >> >> -- >> *Regards,* >> *-Yasith Kanchana Lokuge-* >> >> website : http://www.techsurgeinnovations.com/ >> facebook : https://www.facebook.com/yasithlokuge >> linkedin : http://lk.linkedin.com/pub/yasith-lokuge/1b/713/229/ >> Mobile : +94771353682 >> Skype : yasith11 >> Twitter : @YasithLokuge >> > > > > -- > *Afkham Azeez* > Director of Architecture; WSO2, Inc.; http://wso2.com > Member; Apache Software Foundation; http://www.apache.org/ > * <http://www.apache.org/>* > *email: **[email protected]* <[email protected]> > * cell: +94 77 3320919 <%2B94%2077%203320919>blog: * > *http://blog.afkham.org* <http://blog.afkham.org> > *twitter: **http://twitter.com/afkham_azeez* > <http://twitter.com/afkham_azeez> > *linked-in: **http://lk.linkedin.com/in/afkhamazeez > <http://lk.linkedin.com/in/afkhamazeez>* > > *Lean . Enterprise . Middleware* > -- *Regards,* *-Yasith Kanchana Lokuge-* website : http://www.techsurgeinnovations.com/ facebook : https://www.facebook.com/yasithlokuge linkedin : http://lk.linkedin.com/pub/yasith-lokuge/1b/713/229/ Mobile : +94771353682 Skype : yasith11 Twitter : @YasithLokuge
_______________________________________________ Dev mailing list [email protected] http://wso2.org/cgi-bin/mailman/listinfo/dev
