[英]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: 我有一个带有Servlet入站终结点的简单Mule流设置,它调用了REST服务:
<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. 顺便说一句,我使用servlet端点的原因是因为它在JBoss Webapp中运行。
Here is a simple html/jquery/ajax client i made to call the REST service: 这是我制作的用于调用REST服务的简单html / jquery / ajax客户端:
$(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. 问题在于参数未传递到REST服务。 The service is getting called, but I have a NullPayload. 该服务正在被调用,但是我有一个NullPayload。
If I change my endpoint to HTTP, and run this same app in the Mule container instead of JBoss, then it works. 如果我将端点更改为HTTP,并在Mule容器而不是JBoss中运行相同的应用程序,那么它将起作用。 The parameters do pass. 参数确实通过。 But with a servlet endpoint in JBoss, the parameters are lost. 但是在JBoss中使用servlet端点时,参数会丢失。 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 我需要能够将数据发布到REST服务
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以显示Mule配置和PersonService的结构
Web.xml: 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 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. 并让负载按原样撞击Jersey组件。
Then you need to replace your custom parameter parsing and instead rely on the standard feature JAX-RS provides: 然后,您需要替换您的自定义参数解析,而是依靠JAX-RS提供的标准功能:
@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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.