微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

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

控制器方法

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

        public ServiceDetails getServiceDetails() {
             return new ServiceDetails("MyService");
        }
    }

回复信封:

public class Response<T> {

        private String message;
        private T responseBody;

    }

ServiceDetails代码

public class ServiceDetails {

        private String serviceName;

        public ServiceDetails(String serviceName) {
            this.serviceName = serviceName;
        }
    }

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

{

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

   }

解决方法

你可以做的是让一个MyRestController只是将结果包装在一个响应中,如下所示:
@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");
    }
}

解决方案使您的原始MyController与REST代码保持独立.看来你需要在类路径中包含Jackson,以便Spring自动神奇地序列化为JSON(详见this)

编辑

看来你需要更通用的东西……所以这里有一个建议.

@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 ...
    }
}

并保持原来的“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");
    }
}

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

原文地址:https://www.jb51.cc/java/129049.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐