Java 类未从Spring Boot中的Get API请求映射,如何映射到对象>;
我无法将任何值映射到Profile.class。它们不是0就是null 当我在localhost:8080中查找值时,Profile对象中的所有值都是0或null。我不知道该怎么办,请帮忙 我尝试过很多不同的方法,但似乎都不管用。我尝试过一次交换,我尝试过用getForObject将它映射到Profile类,但没有任何效果 这是我的休息控制器Java 类未从Spring Boot中的Get API请求映射,如何映射到对象>;,java,spring,spring-boot,rest,spring-mvc,Java,Spring,Spring Boot,Rest,Spring Mvc,我无法将任何值映射到Profile.class。它们不是0就是null 当我在localhost:8080中查找值时,Profile对象中的所有值都是0或null。我不知道该怎么办,请帮忙 我尝试过很多不同的方法,但似乎都不管用。我尝试过一次交换,我尝试过用getForObject将它映射到Profile类,但没有任何效果 这是我的休息控制器 package com.finance.Services; import com.finance.Model.Profile; import org.
package com.finance.Services;
import com.finance.Model.Profile;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.Collections;
@RestController
@RequestMapping(value="/profile", headers = "Accept=application/json")
public class ProfileService {
@RequestMapping(path="/{stockSymbol}", method=RequestMethod.GET)
public Profile getProfile(@PathVariable("stockSymbol") String stockSymbol) {
String url = ("https://financialmodelingprep.com/api/v3/company/profile/"+ stockSymbol +"?apikey=bc03aa8fb9a9d1a4944287b3cec3dfe1");
RestTemplate restTemplate = new RestTemplate();
HttpHeaders head = new HttpHeaders();
head.setContentType(MediaType.APPLICATION_JSON);
head.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
HttpEntity<Profile> request = new HttpEntity(head);
ResponseEntity<Profile> response = restTemplate.exchange(
url,
HttpMethod.GET,
request,
Profile.class
);
if (response.getStatusCode() == HttpStatus.OK) {
return response.getBody();
} else {
return null;
}
}
}
下面是发生的事情。当
restemplate.exchange
返回响应时,信息采用此格式
{
"symbol" : "TT",
"profile" : {
"price" : 83.36,
"beta" : "0",
"volAvg" : "2185585",
"mktCap" : "1.99403786E10",
//more keys below
}
因此,您不能直接将响应映射到Profile类
这是你应该做的
APIResponseDTO
ProfileService
类中,使用新的POJO类从restTemplate.exchange
获取结果下面是发生的事情。当
restemplate.exchange
返回响应时,信息采用此格式
{
"symbol" : "TT",
"profile" : {
"price" : 83.36,
"beta" : "0",
"volAvg" : "2185585",
"mktCap" : "1.99403786E10",
//more keys below
}
因此,您不能直接将响应映射到Profile类
这是你应该做的
APIResponseDTO
ProfileService
类中,使用新的POJO类从restTemplate.exchange
获取结果从您访问的url返回的响应结构是什么?从您访问的url返回的响应结构是什么?
public class APIResponseDTO {
private String symbol;
private Profile profile; // Your existing Profile class
//getter and setters below
}
ResponseEntity<APIResponseDTO> response = restTemplate.exchange(
url,
HttpMethod.GET,
request,
APIResponseDTO.class
);
if (response.getStatusCode() == HttpStatus.OK) {
return response.getBody().getProfile();
}