简体   繁体   中英

How to map JSON request in REST webservice in MULE?

I have aa requirement to expose a REST web service in Mue .. My Mule flow is as follow :-

 <flow name="MainService" doc:name="MainService">
 <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8082" doc:name="HTTP"/>
 <jersey:resources doc:name="REST">
  <component class="com.test.services.schema.maindata.v1.Impl.MainData"/>
 </jersey:resources>
 </flow>

Now my question is if I post a JSON request to this REST service .. How can I map the request which is in a Sting format into a object mapped with input parameters of MainData class .. for example .. If I post the following JSON request from a REST client as a request body :-

{
    "insertDataRequest": [
        {
            "id": "288",
            "name": "Sidray",
            "age": "55",
            "designation": "SE"
        }
    ]
}

The request gets into the flow as a String .. Now in Java class MainData, I can use GSON parser to parse the String .. but here I don't want to use GSON in Java class MainData to parse the String request and get id, name, age values .. instead of that I want a Mule transformer or something like that to parse the String and map into an Object before the java class so that in MainData class I can map with the input as following :-

public class MainData
{
@Override
    @POST
    @Produces("application/json")
    @Path("/insert/")
    public DataResponse insertDataOperation(int id,String name,int age,String designation)
{
// Here all the values like are mapped with input parameter
}
}

Is there any way in mule to convert the input request String into an Object and map with input parameter ?? Is this the right approach to expose a REST webservice which intake a JSON request .. please suggest ??

Define your POST body entities with JSON schema and generate Jackson-annotated POJOs so Jersey can automatically bind the request data to an object.

Even better: go contract first, specify your API with RAML and generate both the service endpoints interfaces and binding objects .

EDIT: How to configure JSON support on Jersey: http://www.mulesoft.org/documentation/display/current/Jersey+Module+Reference#JerseyModuleReference-JSONSupport

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM