简体   繁体   中英

How to call Mule ESB REST service from AJAX HTML client

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;
    }
}

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;
}

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