Mule ESB 学习笔记(8)mule和jersey的整合使用

阅读更多

            在项目常用jesery作为rest开发的框架,jesery简单快捷,易于扩展方便使用,在这里我们学习一下mule和jesery的整合使用.

1.简单的绑定一个资源请求路径:

        
            
                
            
            
                
            
        

 

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的整合使用
 * 

功能描述,该部分必须以中文句号结尾。

* * 创建日期 2013-8-23
* @author $Author$
* @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.多个资源的请求的并当使用:

    

        

        
            
            
        
    

 

 3.针对复杂数据类型的的解析处理

    
        
        
            
            
        
    

 

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
{
    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异常的处理绑定使用

 
        
            
                
            
            
                
                
            
        
    

 

你可能感兴趣的:(mule,ESB,SOA,REST,Jesery)