Java restapi客户端的设计

Java restapi客户端的设计,java,rest,design-patterns,Java,Rest,Design Patterns,我想创建一个RESTAPI客户端,对于同一个RESTAPI服务器,它将调用不同的URL。这种不同的调用将向客户端返回JSON和XML。 在这种情况下,什么样的设计模式比较好? 到目前为止,我已经提出了战略和指挥的组合: public interface IRestCall { /** Setup the URL for the call. */ void setup(IRestSetup setup) throws Exception; /** Execute the c

我想创建一个RESTAPI客户端,对于同一个RESTAPI服务器,它将调用不同的URL。这种不同的调用将向客户端返回JSON和XML。 在这种情况下,什么样的设计模式比较好?

到目前为止,我已经提出了战略和指挥的组合:

public interface IRestCall {
    /** Setup the URL for the call. */
    void setup(IRestSetup setup) throws Exception;
    /** Execute the call, using the URL set up in the first step. 
     * @throws Exception */
    void call() throws Exception;
    /** Process the result and return to the user. */
    <T> T getResult(IRestResultProcessor<T> result) throws Exception;
} 
公共接口IRestCall{
/**设置呼叫的URL*/
无效设置(IRestSetup设置)引发异常;
/**使用第一步中设置的URL执行调用。
*@抛出异常*/
void call()抛出异常;
/**处理结果并返回给用户*/
T getResult(IRestResultProcessor结果)抛出异常;
} 
这是策略界面。该战略的背景将是: 在Facade类中的某些Get/Post/Put方法中

IRestSetup和IRestResultProcessor是命令对象的接口
它将为RESTAPI设置URL并处理结果

我认为您不需要一些特殊的设计模式来处理这种情况。我个人只想为GET和POST请求添加一些通用方法。下面的代码只处理GET请求,但它应该足以理解我的意思

处理GET请求的客户端方法可能类似于

/**
restUrl - url to the service 
resClz - the actual POJO object that the JSON or XML response will be mapped to
resType - type of MIME object returned by the server (typically MediaType.APPLICATION_JSON or MediaType.APPLICATION_XML)
*/  
  <T> T doRestGet(String restUrl, Class<T> resClz, String resType){

        T obj;
        Client client = ClientBuilder.newClient();            

        WebTarget target = client.target(UriBuilder.fromUri("http://localhost:8088/Your_WS_root").build()); 

        try{

         obj =(T) target.path(restUrl).request()
              .accept(resType).get().readEntity(resClz);


        }catch(Exception e){
         System.out.println(e);//log or propagate the exception

        return null;
        }
        return obj;
}
现在你可以呼叫你的服务了

UserMessage uMsg = (UserMessage)doRestGet("userMsg", UserMessage.class, MediaType.APPLICATION_JSON ); 

我不认为你需要一些特殊的设计模式来处理这种情况。我个人只想为GET和POST请求添加一些通用方法。下面的代码只处理GET请求,但它应该足以理解我的意思

处理GET请求的客户端方法可能类似于

/**
restUrl - url to the service 
resClz - the actual POJO object that the JSON or XML response will be mapped to
resType - type of MIME object returned by the server (typically MediaType.APPLICATION_JSON or MediaType.APPLICATION_XML)
*/  
  <T> T doRestGet(String restUrl, Class<T> resClz, String resType){

        T obj;
        Client client = ClientBuilder.newClient();            

        WebTarget target = client.target(UriBuilder.fromUri("http://localhost:8088/Your_WS_root").build()); 

        try{

         obj =(T) target.path(restUrl).request()
              .accept(resType).get().readEntity(resClz);


        }catch(Exception e){
         System.out.println(e);//log or propagate the exception

        return null;
        }
        return obj;
}
现在你可以呼叫你的服务了

UserMessage uMsg = (UserMessage)doRestGet("userMsg", UserMessage.class, MediaType.APPLICATION_JSON ); 

在服务器端解析JSON的好设计是使用GSON。您必须设计适当的JavaBeans来解析和验证JSON响应。对于XML,您可以使用JAXB绑定。谢谢您的回答,但这只是我问题的一小部分。另外,我对设计感兴趣,而不是特定的框架。在服务器端解析JSON的好设计是使用GSON。您必须设计适当的JavaBeans来解析和验证JSON响应。对于XML,您可以使用JAXB绑定。谢谢您的回答,但这只是我问题的一小部分。另外,我对设计感兴趣,而不是一个特定的框架。哪种框架最适合实现它?Jersey是JAX-RS规范的参考实现,它可能是最容易使用的REST框架(至少对我来说),哪种框架最适合实现它,Jersey是JAX-RS规范的参考实现,它可能是最容易使用的REST框架(至少对我来说)