【问题标题】:Return response messages in spring boot在 Spring Boot 中返回响应消息
【发布时间】:2019-07-04 04:39:54
【问题描述】:

我正在使用带有 h2 数据库的 spring boot。我想在成功插入寄存器时返回 201 消息,在复制时返回 400 消息。我正在使用 ResponseEntity 来实现这一点,例如,接下来是我从服务中创建的方法:

    @Override
    public ResponseEntity<Object> createEvent(EventDTO eventDTO) {
        if (eventRepository.findOne(eventDTO.getId()) != null) {
            //THis is a test, I am looking for the correct message
            return new ResponseEntity(HttpStatus.IM_USED);
        }
        Actor actor = actorService.createActor(eventDTO.getActor());
        Repo repo = repoService.createRepo(eventDTO.getRepo());
        Event event = new Event(eventDTO.getId(), eventDTO.getType(), actor, repo, createdAt(eventDTO));
        eventRepository.save(event);
        return new ResponseEntity(HttpStatus.CREATED);
    }

这是我的控制器:

    @PostMapping(value = "/events")
    public ResponseEntity addEvent(@RequestBody EventDTO body) {
        return eventService.createEvent(body);
    }

但是我在浏览器中没有收到任何消息,我正在与邮递员进行不同的测试,当我咨询所有事件时,结果是正确的,但是每次我发帖时我都没有收到任何消息浏览器,我不太确定这个问题的原因是什么。有什么想法吗?

【问题讨论】:

  • 你是如何从浏览器发送帖子请求的?
  • 您需要400 来获取现有的ryt?但是你给了IM_USED,这是226

标签: spring-boot http-status-codes


【解决方案1】:

向客户端发送响应的理想方式是在控制器中使用 ResponseEntity 创建 DTO/DAO

Controller.java

@PostMapping("/test")
        public ResponseEntity<Object> testApi(@RequestBody User user)
        {
            System.out.println("User: "+user.toString());
            return assetService.testApi(user);
        }

Service.java

public ResponseEntity testApi(User user) {  
        if(user.getId()==1)
            return new ResponseEntity("Created",HttpStatus.CREATED);
        else
            return new ResponseEntity("Used",HttpStatus.IM_USED);   
           // for BAD_REQUEST(400) return new ResponseEntity("Bad Request",HttpStatus.BAD_REQUEST);
    }

使用 Postman 测试

已创建状态 201

状态 226 IM 已使用

【讨论】:

    【解决方案2】:

    好的,发送ResponseEntity 而不是Controller 的服务让我感觉不太好。对于这些情况,您可以使用@ResponseStatusExceptionHandler 类,如下所示。

    exception包中创建一个类

    GlobalExceptionHandler.java

    @ControllerAdvice
    public class GlobalExceptionHandler {
        @ResponseStatus(HttpStatus.BAD_REQUEST)
        @ExceptionHandler(DataIntegrityViolationException.class) // NOTE : You could create a custom exception class to handle duplications
        public void handleConflict() {
        }
    }
    

    Controller.java

    @PostMapping(value = "/events")
    @ResponseStatus(HttpStatus.CREATED) // You don't have to return any object this will take care of the status
    public void addEvent(@RequestBody EventDTO body) {
       eventService.createEvent(body);
    }
    

    现在改变服务看起来像,

    Service.java

    @Override
    public void createEvent(EventDTO eventDTO) { // No need to return
       if (eventRepository.findOne(eventDTO.getId()) != null) {
            throw new DataIntegrityViolationException("Already exists"); // you have to throw the same exception which you have marked in Handler class
       }
       Actor actor = actorService.createActor(eventDTO.getActor());
       Repo repo = repoService.createRepo(eventDTO.getRepo());
       Event event = new Event(eventDTO.getId(), eventDTO.getType(), actor, repo, createdAt(eventDTO));
       eventRepository.save(event);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-20
      • 2016-12-14
      • 2019-07-27
      • 2018-06-19
      • 1970-01-01
      • 2017-12-04
      • 2018-11-07
      相关资源
      最近更新 更多