如何使用Spring MVC设计通用的响应构建器/ RESTful Web服务?

Eli*_*sky 9 java rest spring-mvc

尝试使用Spring MVC构建RESTful Web服务.

控制器应返回特定的Java类型,但响应主体必须是通用信封.如何才能做到这一点?

以下代码部分是我到目前为止所拥有的:

控制器方法:

    @Controller
    @RequestMapping(value = "/mycontroller")
    public class MyController {

        public ServiceDetails getServiceDetails() {
             return new ServiceDetails("MyService");
        }
    }
Run Code Online (Sandbox Code Playgroud)

回复信封:

    public class Response<T> {

        private String message;
        private T responseBody;

    }
Run Code Online (Sandbox Code Playgroud)

ServiceDetails 码:

    public class ServiceDetails {

        private String serviceName;

        public ServiceDetails(String serviceName) {
            this.serviceName = serviceName;
        }
    }
Run Code Online (Sandbox Code Playgroud)

对客户的最终回应应显示为:

   {

     "message" : "Operation OK"
     "responseBody" : {
                        "serviceName" : "MyService"
                      }

   }  
Run Code Online (Sandbox Code Playgroud)

ben*_*n75 0

你可以做的就是将MyRestController结果包装成这样Response

@Controller
@RequestMapping(value = "/mycontroller")
public class MyRestController {

    @Autowired
    private MyController myController;

    @RequestMapping(value = "/details")
    public @ResponseBody Response<ServiceDetails> getServiceDetails() {
         return new Response(myController.getServiceDetails(),"Operation OK");
    }
}
Run Code Online (Sandbox Code Playgroud)

此解决方案使您的原始代码MyController独立于 REST 代码。看来你需要将 Jackson 包含在你的类路径中,以便 Spring 会自动神奇地序列化为 JSON(有关详细信息,请参阅此)

编辑

看来你需要一些更通用的东西......所以这是一个建议。

@Controller
@RequestMapping(value = "/mycontroller")
public class MyGenericRestController {

    @Autowired
    private MyController myController;

    //this will match all "/myController/*"
    @RequestMapping(value = "/{operation}")
    public @ResponseBody Response getGenericOperation(String @PathVariable operation) {
          Method operationToInvoke = findMethodWithRequestMapping(operation);
          Object responseBody = null;
          try{
               responseBody = operationToInvoke.invoke(myController);
          }catch(Exception e){
               e.printStackTrace();
               return new Response(null,"operation failed");
          }
         return new Response(responseBody ,"Operation OK");
    }

    private Method findMethodWithRequestMapping(String operation){
         //TODO
         //This method will use reflection to find a method annotated
         //@RequestMapping(value=<operation>)
         //in myController
         return ...
    }
}
Run Code Online (Sandbox Code Playgroud)

并保持原来的“myController”几乎原样:

@Controller
public class MyController {

    //this method is not expected to be called directly by spring MVC
    @RequestMapping(value = "/details")
    public ServiceDetails getServiceDetails() {
         return new ServiceDetails("MyService");
    }
}
Run Code Online (Sandbox Code Playgroud)

主要问题是:MyController需要的 @RequestMapping 可能被一些自定义注释替换(并适应findMethodWithRequestMapping对此自定义注释执行内省)。