1
votes

I have a simple Mule flow setup with a servlet inbound endpoint, which calls a REST service:

<flow name="sandboxFlow" doc:name="sandboxFlow">
    <servlet:inbound-endpoint path="rest" responseTimeout="10000" doc:name="Servlet"/>
    <set-payload value="#[message.inboundProperties['http.query.params']]" doc:name="Set Payload"/>
    <logger message="Payload is #[message.payload]" level="INFO" doc:name="Logger"/>
    <jersey:resources doc:name="REST">
        <component class="com.csc.rest.service.PersonService">
    </jersey:resources>
</flow>

By the way, the reason I am using a servlet endpoint is because this is running in a JBoss webapp.

Here is a simple html/jquery/ajax client i made to call the REST service:

$(document).ready(function(){
    $("#buttonEnroll").click(function(){
        var name = $("#textName").val();
        var ssn = $("#textSsn").val();
        var params = "name="+name+"&ssn="+ssn;

        $.ajax({
            type: "POST"
            url: "esb/rest/PersonService",
            data: params,
            cache: false,
            success: function(data){
                alert("POST success: " + data);
            }
        });
    });
});

The problem is that the parameters are not being passed to the REST service. The service is getting called, but I have a NullPayload.

If I change my endpoint to HTTP, and run this same app in the Mule container instead of JBoss, then it works. The parameters do pass. But with a servlet endpoint in JBoss, the parameters are lost. Here is the output from the logger component:

INFO [org.mule.api.processor.LoggerMessageProcessor] (default task-2) Payload is null

Any idea how to fix this? I need to be able to POST data to the REST service

This is just a starting point, I am hoping to get working before adding more components to the project.

EDIT: Adding web.xml to show mule config and structure of PersonService

Web.xml:

<context-param>
    <param-name>org.mule.config</param-name>
    <param-value>mule-config.xml</param-value>
</context-param>

<listener>
    <listener-class>org.mule.config.builders.MuleXmlBuilderContextListener</listener-class>
</listener>

<servlet>
    <servlet-name>muleServlet</servlet-name>
    <servlet-class>org.mule.transport.servlet.MuleReceiverServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>muleServlet</servlet-name>
    <url-pattern>/esb/*</url-pattern>
</servlet-mapping>

PersonService

@Path("/PersonService")
@RequestScoped
public class PersonService {

    PersonManager manager = PersonManager.newPersonManager();

    @POST
    public String addPerson(String input) {
        System.out.println("addPerson: input=" + input.toString());
        HashMap<String,String> params = Toolbox.parseParameters(input)
        String name = params.get("name");
        String ssn = params.get("ssn");
        if(name != null && ssn != null) {
            Person person = new Person(name, ssn);
            manager.persist(person);
        }
        return "person added: " + name + ";" + ssn;
    }
}
1
Please provide: Mule version ; web.xml fragment that shows the config of the Mule bits ; General structure of com.csc.rest.service.PersonService (no method content, just class/methods so we can see the annotations.David Dossot
@DavidDossot I added the requested infodave823
Thanks! So what you're getting is an input String that contains "NullPayload"?David Dossot
yes. The logger shows payload as null, but the println in the addPerson method actually displays "addPerson: input=?? sr org.mule.transport.NullPayload1 L5U??? xp"dave823

1 Answers

1
votes

I think you need to remove:

<set-payload value="#[message.inboundProperties['http.query.params']]" doc:name="Set Payload"/>

and let the payload hit the Jersey component as-is.

Then you need to replace your custom parameter parsing and instead rely on the standard feature JAX-RS provides:

@POST
public String addPerson(@FormParam("name") String name,
                        @FormParam("ssn") String ssn) {

    if(name != null && ssn != null) {
        Person person = new Person(name, ssn);
        manager.persist(person);
    }
    return "person added: " + name + ";" + ssn;
}