Web Application中集成Mule3.4.0和Jersey RestWebService
import javax.xml.bind.annotation.XmlRootElement;/** * * <p>功能描述,该部分必须以中文句号结尾。<p> * * 创建日期 2013-8-16<br> * @author $Author$<br> * @version $Revision$ $Date$ * @since 3.0.0 */@XmlRootElementpublic class HelloBean{ private String message; private int number; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; }}
?
?
?
import javax.ws.rs.DELETE;import javax.ws.rs.GET;import javax.ws.rs.HeaderParam;import javax.ws.rs.POST;import javax.ws.rs.Path;import javax.ws.rs.PathParam;import javax.ws.rs.Produces;import javax.ws.rs.QueryParam;import javax.ws.rs.core.Response;/** * * <p>功能描述,该部分必须以中文句号结尾。<p> * * 创建日期 2013-8-16<br> * @author $Author$<br> * @version $Revision$ $Date$ * @since 3.0.0 */@Path("/helloworld")public class HelloWorldResource{ @POST @Produces("text/plain") public String sayHelloWorld() { return "Hello World"; } @GET @Produces("application/json") @Path("/sayHelloWithJson/{name}") public HelloBean sayHelloWithJson(@PathParam("name") String name) { HelloBean hello = new HelloBean(); hello.setMessage("Hello " + name); return hello; } @DELETE @Produces("text/plain") public String deleteHelloWorld() { return "Hello World Delete"; } @GET @Produces("text/plain") @Path("/sayHelloWithUri/{name}") public String sayHelloWithUri(@PathParam("name") String name) { return "Hello " + name; } @GET @Produces("text/plain") @Path("/sayHelloWithHeader") public Response sayHelloWithHeader(@HeaderParam("X-Name") String name) { return Response.status(201).header("X-ResponseName", name).entity("Hello " + name).build(); } @GET @Produces("text/plain") @Path("/sayHelloWithQuery") public String sayHelloWithQuery(@QueryParam("name") String name) { return "Hello " + name; } @GET @Produces("text/plain") @Path("/throwException") public String throwException() throws HelloWorldException { throw new HelloWorldException("This is an exception"); }}
?
?
配置文件mule-config.xml
<?xml version="1.0" encoding="UTF-8"?><mule xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:jersey="http://www.mulesoft.org/schema/mule/jersey" xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/jersey http://www.mulesoft.org/schema/mule/jersey/current/mule-jersey.xsd http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd"> <model name="BasicJerseyTest"> <service name="helloWorldResource"> <inbound> <inbound-endpoint address="http://localhost:63081/" exchange-pattern="request-response"/> </inbound> <jersey:resources> <component name="code"><?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>MuleJerseyRestWeb</display-name> <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>ajax</servlet-name> <servlet-class>org.mule.transport.ajax.container.MuleAjaxServlet</servlet-class> </servlet> <servlet> <servlet-name>muleResources</servlet-name> <servlet-class>org.mule.transport.servlet.JarResourceServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>muleResources</servlet-name> <url-pattern>/mule-resource/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>ajax</servlet-name> <url-pattern>/ajax/*</url-pattern> </servlet-mapping> <mime-mapping> <extension>js</extension> <mime-type>application/x-javascript</mime-type> </mime-mapping> <mime-mapping> <extension>json</extension> <mime-type>application/json</mime-type> </mime-mapping></web-app>
?