在项目常用jesery作为rest开发的框架,jesery简单快捷,易于扩展方便使用,在这里我们学习一下mule和jesery的整合使用.
1.简单的绑定一个资源请求路径:
<service name="helloWorldResource"> <inbound> <inbound-endpoint address="http://localhost:63081/" exchange-pattern="request-response"/> </inbound> <jersey:resources> <component class="org.mule.module.jersey.HelloWorldResource"/> </jersey:resources> </service>
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; import com.easyway.esb.mule.jersey.exception.HelloWorldException; /** * mule和jersey的整合使用 * <p>功能描述,该部分必须以中文句号结尾。<p> * * 创建日期 2013-8-23<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"); } }
2.多个资源的请求的并当使用:
<flow name="helloWorldResource"> <inbound-endpoint address="http://localhost:63081/" /> <jersey:resources> <component class="org.mule.module.jersey.HelloWorldResource" /> <component class="org.mule.module.jersey.AnotherWorldResource" /> </jersey:resources> </flow>
3.针对复杂数据类型的的解析处理
<flow name="helloWorldResource"> <inbound-endpoint address="http://localhost:63081/" exchange-pattern="request-response"/> <jersey:resources> <component class="com.easyway.esb.mule.jersey.HelloWorldResource"/> <jersey:context-resolver class="com.easyway.esb.mule.jersey.contextresolver.JaxbCustomContextResolver" /> </jersey:resources> </flow>
import javax.ws.rs.ext.ContextResolver; import javax.ws.rs.ext.Provider; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import com.easyway.esb.mule.jersey.HelloBean; import com.sun.jersey.api.json.JSONConfiguration; import com.sun.jersey.api.json.JSONJAXBContext; @Provider public class JaxbCustomContextResolver implements ContextResolver<JAXBContext> { protected JAXBContext context; @Override public JAXBContext getContext(Class<?> type) { if (type.equals(HelloBean.class)) { if (context == null) { try { context = new JSONJAXBContext(JSONConfiguration.natural().build(), type); } catch (JAXBException e) { // do nothing } } return context; } return null; } }
4.针对REST异常的处理绑定使用
<model name="BasicJerseyTest"> <service name="helloWorldResource"> <inbound> <inbound-endpoint address="http://localhost:63081/" exchange-pattern="request-response"/> </inbound> <jersey:resources> <component class="com.easyway.esb.mule.jersey.HelloWorldResource"/> <jersey:exception-mapper class="com.easyway.esb.mule.jersey.exception.HelloWorldExceptionMapper" /> </jersey:resources> </service> </model>
相关推荐
Mule ESB 是一个轻量级的基于java的企业服务总线和集成平台, 使得开发人员可以快速,简单的连接多个... Mule ESB 包含如下强大的能力: 服务创建和托管— 暴露和托管可重用服务, 使用Mule ESB作为一个轻量级服务容器.
文档主要介绍了Mule ESB的使用方法,并结合具体实例加深对ESB的理解,对新手很有帮助哦!
Mule ESB 项目在Linux中的部署与开发与应用案例
MuleESB是一个基于Java的轻量级企业服务总线和集成平台,允许开发人员快速便利地连接多个应用,并支持应用间的数据交换。MuleESB支持集成现有系统而无论其底层采用何种技术,如JMS、WebServices、JDBC、HTTP以及其他...
Mule ESB应用部署 Mule ESB应用的目录结构,配置文件说明
ESB原理及Mule ESB实践
MuleESB3.0 属于轻量级的消息框架和整合平台,mule云
mule,mule esb,Mule,ESB
mule esb开发手册
mule esb 项目 例子 入门
MuleESB集成webservice+restful(sprintboot+mybatis+mysql)+activeMQ+javamail,五天的研究成果,集成了我所关注的点,希望有更多的朋友一起学习进步。
mule esb mule esb 开发工具
开源企业服务总线资料,介绍MULE ESB 官方例子的使用和原理。
MuleEsb开源框架简介.pdf
mule esb mule esb打包手册文档
mule esb 的 简单介绍, 以及一些主要特性的介绍
MuleESB_3.0_中文教程
mule esb 3 user guider: 社区成熟,文档丰富的开源esb mule用户手册