Spring拒绝发送ResponseEntity但不抛出异常

时间:2018-01-15 14:29:26

标签: java rest spring-mvc spring-boot

我的POJO /数据模型:

public class CanResendResponse {
    @JsonProperty(value = "canResend")
    @NotEmpty
    public Boolean canResend;

    // getters, setters & ctors
}

我的Spring Boot控制器&方法:

@RestController
@RequestMapping("v1/data/fizzes")
class FizzResource {
    @GetMapping(value = "{fizzId}/canResend")
    public void canResendVerifications(@PathVariable(value = "fizzId") String fizzId) {
        Fizz fizz = fizzRepository.findById(fizzId);
        Boolean canResend;

        System.out.println("Fizz name:" + fizz.getName());

        if(fizz.canResend()) {
            canResend = Boolean.TRUE;
        } else {
            canResend = Boolean.FALSE;
        }

        return new ResponseEntity<CanResendResponse>(new CanResendResponse(canResend), HttpStatus.OK);
    }
}

我的卷曲命令:

curl -H "Content-Type: application/json" -X GET https://localhost:9200/v1/data/fizzes12345/canResend

当我运行curl命令时,我没有在服务器端看到任何异常/错误,并且curl没有错误地完成但我没有看到预期的HTTP响应实体,如:

{
  "canResend" : "true"
}

但是我在STDOUT中看到Fizz name: Joe消息。

我已经在浏览器中确认了相同的行为(我打https://localhost:9200/v1/data/fizzes12345/canResend)到浏览器中,并且响应/页面为空。 关于我可以采取哪些措施来解决这个问题?

2 个答案:

答案 0 :(得分:1)

您的方法具有VOID返回类型。试试这个:

@GetMapping(value = "{fizzId}/canResend")
public ResponseEntity canResendVerifications(@PathVariable(value = "fizzId") String fizzId) {
    Your method code goes here...
}

答案 1 :(得分:1)

更改

public void canResendVerifications(@PathVariable(value = "fizzId") String fizzId) {

public ResponseEntity<> canResendVerifications(@PathVariable(value = "fizzId") String fizzId) {

...并确保return ResponseEntity。{/ p>