Mule 3.0 Restful Webservice
Mule 3.0 was recently released. Restful web services are now fully supported. You can hot deploy the application now. It is much easier to deploy an application. Unfortunately, as I write this, the documentation is not yet up to date. The configuration for a restful web service has changed.
The configuration file looks like the following:
mule-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/3.0/mule.xsd">
<model name="smsSample">
<simple-service name="MyService" address="http://localhost:63081/" component-class="com.jasonrowland.example.MyServiceClass" type="jax-rs" />
</model>
</mule>
The class to implement this service looks like this:
package com.jasonrowland.example;
import javax.ws.rs.*;
@Path("/myroot")
public class MyServiceClass {
@GET
@Produces("application/json")
@Path("/myfunc/{myvar}")
public MyResponse myfunc(
@PathParam("myvar") String myvar,
@FormParam("keyword") String keyword) {
MyResponse r = new MyResponse();
if ("fail".equalsIgnoreCase(keyword)) {
r.setCode(1);
r.setText("fail is a reserved keyword");
} else {
r.setCode(0);
r.setText("SUCCESS");
}
return r;
}
}
The MyResponse object needs to have an attribute set on it for this to work.
package com.jasonrowland.example;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class SmsResponse {
// code
public int getCode() {
return this.code;
}
public void setCode(int value) {
this.code = value;
}
// text
public String getText() {
return this.text;
}
public void setText(String value) {
this.text = value;
}
private int code;
private String text;
}
You simply put all your files in a folder under the mule/apps directory. The directory structure looks like this:
MULEHOME/apps/YOURAPP/
/mule-config.xml
/lib/
/yourapp.jar
/log4j.jar
/anyotherdependency.jar
To run your application in mule, simply type:
mule -app YOURAPP
To hot deploy your application, update your files and type:
touch YOURAPP/mule-config.xml
