简体   繁体   English

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

[英]No suitable HttpMessageConverter found for response type and content type [application/json;charset=UTF-8] exception occurs

Im trying to hit Spring REST endpoint in my other module of the application.我试图在我的应用程序的其他模块中点击 Spring REST 端点。 So im trying to use the REST Template to get a list of users as below :所以我尝试使用 REST 模板来获取用户列表,如下所示:

The API request using REST Template :使用 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 class : LeadUserList 类:

public class LeadUserList {

    private List<LeadUser> usersList;

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

LeadUser model class : 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() {
    }
}

The API endpoint controller class : 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);
    }
}

Although the return type is JSON from the API endpoint im getting the above exception.虽然返回类型是来自 API 端点的 JSON,但我得到了上述异常。 What have I done wrong here?我在这里做错了什么?

The exception :例外:

Could not extract response: no suitable HttpMessageConverter found for response type [class admin.client.domain.LeadUserList] and content type [application/json]

Try additional settings as follows,尝试以下附加设置,

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

Also fix your exchange call,还修复你的交换电话,

ResponseEntity<List<LeadUser>> response = restTemplate.exchange(
            adminApiUrl, HttpMethod.GET, entity, new ParameterizedTypeReference<List<LeadUser>>(){});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找不到适用于响应类型[..]和内容类型[application / json]的HttpMessageConverter - No suitable HttpMessageConverter found for response type [..] and content type [application/json] 找不到适合响应类型和内容类型的HttpMessageConverter - no suitable HttpMessageConverter found for response type and content type 内容类型 &#39;application/json;charset=UTF-8&#39; - Content type 'application/json;charset=UTF-8' 没有找到适合响应类型的 HttpMessageConverter - no suitable HttpMessageConverter found for response type 严重:找不到媒体类型 = 应用程序/json;字符集 = utf-8 的 MessageBodyWriter - SEVERE: MessageBodyWriter not found for media type=application/json;charset=utf-8 不支持内容类型 'application/json;charset=UTF-8' - Content type 'application/json;charset=UTF-8' not supported org.springframework.web.client.RestClientException:提取类型和内容类型的响应时出错 [application/json;charset=utf-8] - org.springframework.web.client.RestClientException: Error while extracting response for type and content type [application/json;charset=utf-8] 没有为响应类型找到合适的HttpMessageConverter - Custom RestTemplate - No suitable HttpMessageConverter found for response type - Custom RestTemplate 找不到适合响应类型 [class org.springframework.http.ResponseEntity] 和内容类型 [application/octet-stream] 的 HttpMessageConverter - No suitable HttpMessageConverter found for response type [class org.springframework.http.ResponseEntity] and content type [application/octet-stream] 找不到用于Java类型和MIME媒体类型application / json; charset = utf-8的消息正文阅读器 - A message body reader for Java type and MIME media type, application/json;charset=utf-8, was not found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM