代码之家  ›  专栏  ›  技术社区  ›  smile95

弹簧引导:失败。。编写HTTP消息:springframework。http。转换器。HttpMessageNotWritableException

  •  1
  • smile95  · 技术社区  · 7 年前

    为了好玩,我正在编写一个简单的spring boot REST API。弹簧靴对我来说是新的。通过调用“/搜索”,我得到以下异常:

    无法写入HTTP消息:org。springframework。http。转换器。HttpMessageNotWritableException:找不到类型为java类的返回值的转换器。util。LinkedHashMap

    我的代码:

    @后期映射(“/搜索”) 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);
    
    }
    

    任何帮助都将不胜感激。

    提前感谢!

    2 回复  |  直到 7 年前
        1
  •  0
  •   Mahesh_Loya    7 年前

    确保在相关实体/pojo类中生成了getter和setter。

    还要确保在构建文件中正确设置了所需的所有依赖项。 如果您使用的是maven,请检查以下jackson libs依赖项。

    <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>
    
        2
  •  0
  •   Nyamiou The Galeanthrope    7 年前

    User 包含 LinkedHashMap Spring不知道如何将其转换为JSON。你应该试着用普通的 HashMap