在Spring Boot中返回响应消息

时间:2019-07-04 04:39:54

标签: spring-boot http-status-codes

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

    @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);
    }

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

2 个答案:

答案 0 :(得分:0)

向客户端发送响应的理想方法是在Controller中使用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);
    }

使用邮递员进行测试

状态201已创建 Created

已使用状态226 IM Status IMUSED

答案 1 :(得分:0)

好的,我真的不满意发送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);
}