java - 在 Web 方法、服务层或 DAO 层中创建服务响应?

标签 java spring web-services dao service-layer

我是 Web 服务和 Spring 的新手。我有一个像这样的网络服务方法:

@WebMethod(operationName = "docSearch")
    public ServiceResponse docSearch(@WebParam(name = "docNum") String docNum) {
        //TODO lots of TODOs here:
        docService = getDocService();
        ServiceResponse serviceResponse = docService.docSearchService(docNum);
        return serviceResponse;
    }

使用此服务:

@Transactional
    public ServiceResponse docSearchService(ServiceRequest serviceRequest, String docNum) {
        //TODO lots of TODOs here:
        ServiceResponse serviceResponse = new ServiceResponse();
        serviceResponse = docDao.docSearchDAO(serviceRequest, docNum);
        return serviceResponse;
    }

对应的DAO是:

public ServiceResponse docSearchDAO(ServiceRequest serviceRequest, String docNum) {             

        Session session = sessionFactory.getCurrentSession();
        List<doc> temp = session.createCriteria(Doc.class)
                .add(Restrictions.eq("id", docNum))
                .list();

        if (temp != null && temp.size() > 0) {

            serviceResponse.setDescription("Service response successfully implemented);           
            return serviceResponse;
        } else {
            serviceResponse.setDescription("Service response was not successfully implemented);  
            return serviceResponse;
        }

        }

我想知道在 Web 方法、服务层或 DAO 层中填充此 ServiceResponse 对象的最佳方式是什么?

最佳答案

我会在你的网络方法中说。 Web服务应该是调用服务层的接口(interface)。它应该将传入请求转换为服务层可以理解的内容,并将结果转换为 Web 服务可以发送的内容。

该服务通常是应用程序的可重用部分,可以在 Web 服务和 Web 应用程序(带有 Controller )之间重用,或者可以通过批量插入来重用。基本上,调用服务(包含业务逻辑)的所有内容都是服务的接口(interface)层。

Controllers provide access to the application behavior that you typically define through a service interface. Controllers interpret user input and transform it into a model that is represented to the user by the view. Spring implements a controller in a very abstract way, which enables you to create a wide variety of controllers. Reference Guide

对我来说,这适用于与服务层交互的应用程序的每个部分。它基本上从/到服务层理解的东西转换。这是a blog链接到一些有趣的信息(在更广泛的架构层面上)。

链接:

  1. Spring Reference Guide
  2. Clean Architecture

关于java - 在 Web 方法、服务层或 DAO 层中创建服务响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18638783/

相关文章:

java - android使用zk客户端与curator连接zk获取数据失败并出现connectionLoss

java - XMLEncoder 拒绝写入文件

java - 将表连接添加到现有项目会导致无限递归

c++ - 生成没有 stub 的 WSDL 请求/响应

java - 接收 HTTP 传输错误 : javax.net.ssl.SSLHandshakeException : sun. security.validator.ValidatorException:PKIX 路径构建失败

java - 解析包含未知长度字段的字节数组

java - 需要使用看起来很奇怪的 Schema 来解析 XML

java - 尝试应用 AttributeConverter 时出错

java - 如何使用 spring-boot 创建 REST 服务?

c# - LiveCycle PDF 以 XML 形式提交到 .NET Web 服务