Mule ESB 学习笔记(8)mule和jersey的整合使用
程序员文章站
2022-07-12 18:59:50
...
在项目常用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>