1
votes

I am writing a simple spring boot REST API for fun. Spring boot is new for me. By call the "/search" i get following exception:

Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class java.util.LinkedHashMap

My Code:

@PostMapping("/search") public ResponseEntity getSearchResultViaAjax(@Valid @RequestBody SearchCriteria search, Errors errors) {

   ResponseBody result = new ResponseBody();

    //If error, just return a 400 bad request, along with the error message
    if (errors.hasErrors()) {

        result.setMsg(errors.getAllErrors().stream().map(x -> x.getDefaultMessage()).collect(Collectors.joining(",")));
        return ResponseEntity.badRequest().body(result);

    }

    List<User> users = userService.findByUserNameOrEmail(search.getUsername());
    if (users.isEmpty()) {
        result.setMsg("no user found!");
    } else {
        result.setMsg("success");
    }
    result.setResult(users);

    return ResponseEntity.ok(result);

}

Any help would be greatly appreciated.

Thanks in advance!

2
I use spring boot v: 2.0.0.RELEASE - smile95
Can you give us the stacktrace please? - Nyamiou The Galeanthrope

2 Answers

0
votes

Make sure you have getters and setters generated in the concerned entity/pojo classes.

Also make sure you have all the dependency required set coreectly in your build file. If you are using maven,check for below jackson libs dependency.

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.4.3</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.4.3</version>
</dependency>
0
votes

User contains a LinkedHashMap and Spring doesn't know how to convert that to JSON. You should try using a regular HashMap.