简体   繁体   中英

How to fix converter in RestController in SpringBoot?

I try to do POST request:

POST http://localhost:8080/api/add
Content-Type: application/json

{
  "amount": "6",
  "timestamp": "2021-01-31T14:35:01.00Z"
}

The response is:

HTTP/1.1 500 
Content-Length: 0
Date: Sun, 31 Jan 2021 14:33:43 GMT
Connection: close

<Response body is empty>
Response code: 500; Time: 15ms; Content length: 0 bytes

there is my Controller:

@RestController
@RequestMapping(value = "/api")
public class StatsController {
    private final StatisticService statisticsService;

    @Autowired
    public StatsController(StatisticService statisticsService) {
        this.statisticsService = statisticsService;
    }

    @PostMapping(value = "/add")
    public Transaction create(@RequestBody Transaction transaction) {
        return statisticsService.createStats(transaction);
    }

    @GetMapping("/stats")
    public Statistic getStats() {
        return statisticsService.getStats();
    }
}

But also I am receiving WARN message in Spring Boot:

2021-01-31 16:31:13.394  WARN 33984 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class java.util.LinkedHashMap]
2021-01-31 16:33:43.651  WARN 33984 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=UTF-8' not supported]

I tried different suggestions from similar problems:

  • checked if my pojos have getters/setters
  • tried to add some jackson dependencies
  • add consumes = MediaType.APPLICATION_JSON_VALUE to RestController annotation.
  • reloaded maven project

Nothing helped to me.

I have changed Spring Boot version from 2.4.2 to 2.4.0 and problem was fixed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM