找不到合适的HttpMessageConverter用于响应类型和内容类型[application / json; charset = UTF-8]异常发生

问题描述 投票:0回答:1

我试图在我的应用程序的其他模块中点击Spring REST端点。因此,我尝试使用REST模板获取用户列表,如下所示:

使用REST模板的API请求:

public List<LeadUser> getUsersBySignUpType(String type, String id) {

    String adminApiUrl = adminApiBaseUrl+"/crm/v1/users/?type="+type+"&id="+id;
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(org.springframework.http.MediaType.APPLICATION_JSON);
    HttpEntity entity = new HttpEntity(headers);
    ResponseEntity<LeadUserList> response = restTemplate.exchange(
            adminApiUrl, HttpMethod.GET, entity, LeadUserList.class);
    return response.getBody().getUsersList();
}

LeadUserList类:

public class LeadUserList {

    private List<LeadUser> usersList;

    public List<LeadUser> getUsersList() {
        return usersList;
    }
}

LeadUser模型类:

public class LeadUser {

    @JsonProperty("id")
    private String id;
    @JsonProperty("email")
    private String email;
    @JsonProperty("name")
    private String name;
    @JsonProperty("businessName")
    private String businessName;
    @JsonProperty("phone")
    private String phone;
    @JsonProperty("address")
    private String address;
    @JsonProperty("createdTime")
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    private Date createdTime;
    @JsonProperty("updatedTime")
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    private Date updatedTime;
    @JsonProperty("bookletSignups")
    private BookletSignUp bookletSignUp;
    @JsonProperty("eventSignups")
    private EventSignUp eventSignUp;
    @JsonProperty("infoSignups")
    private InfoSignUp infoSignUp;
    @JsonProperty("webinarSignups")
    private WebinarSignUp webinarSignUp;

    public LeadUser() {
    }
}

API端点控制器类:

@Controller
@Component
@RequestMapping(path = "/crm/v1")
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping(value = "/users", method = GET,produces = "application/json")
    @ResponseBody
    public ResponseEntity<List<User>> getPartnersByDate(@RequestParam("type") String type, 
    @RequestParam("id") String id) throws ParseException {

        List<User> usersList = userService.getUsersByType(type);
        return new ResponseEntity<List<User>>(usersList, HttpStatus.OK);
    }
}

虽然返回类型是来自API端点的JSON,但我得到了上述异常。我在这做错了什么?

例外:

Could not extract response: no suitable HttpMessageConverter found for response type [class admin.client.domain.LeadUserList] and content type [application/json]
java spring-mvc resttemplate spring-rest
1个回答
0
投票

尝试以下其他设置,

httpHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
httpHeaders.setContentType(MediaType.APPLICATION_JSON);

还要修复你的交换电话,

ResponseEntity<List<LeadUser>> response = restTemplate.exchange(
            adminApiUrl, HttpMethod.GET, entity, new ParameterizedTypeReference<List<LeadUser>>(){});
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.