English 中文(简体)
复方服务层与控制器
原标题:response wrapper service layer vs controller

目前,我正试图利用春布特制作一个教育、科学和技术预报软件。

我正利用应急能力提供应对措施,并另外为综合对策设定一个目标。

现行法典正视这一点。

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class RestResponse<T> {
  private Integer status;
  private HttpStatus httpStatus;
  private String path;
  private String message;
  private T data;
}

如果是在服务层面

装置特性结果

最终结果

return RestResponse.builder()
              .status(200)
              .httpStatus(HttpStatus.OK)
              .message("Success")
              .data("")
              .build();


return RestResponse.builder()
              .status(ase.statusCode())
              .httpStatus(HttpStatus.valueOf(ase.statusCode()))
              .message(ase.awsErrorDetails().errorCode())
              .data("")
              .build();

当我们利用捕获物来恢复休战反应,控制者将其置于应急状态,并将它归还给我们时,我们就能够使用。

这是正确的结构吗?

在控制层进行总结吗?

如果我们需要加以修改,我们如何能够这样做?

我目前认为,我需要在控制层制造一个Response物体。

问题回答

www.un.org/spanish/ecosoc 全面答复您的询问,结合最佳做法和处理你们的关切:。

<>Key Point:

  • Wrap responses in the controller layer: Maintain a clear separation of concerns by creating RestResponse objects within controllers.
  • Service layer focuses on business logic: Services should return raw data or domain objects, leaving response formatting to the controller.
  • Consider exception handling: Customize exception responses effectively for better error reporting.

<>结构:<>

  1. http://www.ohchr.org。

    • Handle incoming requests.
    • Call appropriate service methods.
    • Create RestResponse objects based on service results and HTTP status codes.
    • Wrap RestResponse in ResponseEntity and return it.
    @RestController
    @RequestMapping("/api/devices")
    public class DeviceController {
        private final DeviceService deviceService;
    
        @GetMapping("/{index}")
        public ResponseEntity<RestResponse<DeviceEntity>> getDeviceByIndex(@PathVariable String index) {
            DeviceEntity device = deviceService.getDeviceByDeviceIndex(index);
            if (device == null) {
                return ResponseEntity.notFound().build();
            }
            return ResponseEntity.ok(
                RestResponse.<DeviceEntity>builder()
                    .status(200)
                    .httpStatus(HttpStatus.OK)
                    .message("Success")
                    .data(device)
                    .build()
            );
        }
    }
    
  2. <>服务年限:

    • Encapsulate business logic.
    • Return raw data or domain objects.
    • Throw custom exceptions for specific errors.
    @Service
    public class DeviceService {
        private final DeviceMapper deviceMapper;
    
        public DeviceEntity getDeviceByDeviceIndex(String deviceIndex) {
            return deviceMapper.getDeviceByDeviceIndex(deviceIndex);
        }
    }
    

<>Exception Handling:

  • Define custom exception classes for specific API errors.
  • Use @ExceptionHandler in controllers to handle exceptions and construct appropriate RestResponse objects.

<>补充审议:

  • Tailor RestResponse fields to your API s needs.
  • Explore libraries like Spring HATEOAS for rich hypermedia responses.

www.un.org/spanish/ecosoc 通过遵循这一结构,你创造了结构良好、可适应和方便用户的、温斯布特的、符合要求的、符合要求的、符合要求的、符合用户需要的、符合环境规范的、符合环境规范的、可适应性的、适合用户需要的、符合环境规范的、符合环境规范的、可适应性的、符合环境规范的、符合环境规范的、可适应性的、可使用性的、可使用性、可使用性、可使用性、可使用性、可使用性、可使用性、可使用性、可再生性、可使用性、可使用性、可使用性、可使用性、可使用性、可再生性、可使用性、可使用性、可使用性、可再生性、可使用性、可使用性、可使用性、可使用性、可再生性、可再生性、可使用性、可再生性、可再生性、可使用性、可使用性、可使用性、可使用性、可使用性、可使用性、可使用性、可使用性、可使用性、可再生性、可使用性、可使用性、可使用性、可使用性、可再生性、可再生





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签