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

Reply via email to