我在本地系统的不同端口上创建了两个名为 MS1 和 MS2 的 spring boot 项目。 MS1.controller 有 /GetAPI 调用外部 API 端点,即 MS2.controller /GetAPI。当我运行 MS2 时,它以 JSON 格式获取数据。但是,当我使用 MS1 触发 API 时,它会抛出 415 Unsupported Media Type。在 spring boot 中默认数据格式是 JSON。我什至创建了标题并将其添加到 restTemplate.getForObject() 方法中。请参考我的代码,让我知道哪里出了问题。
主课:
@SpringBootApplication
@ComponentScan("ondc_account_validation")
public class OndcAccountValidation {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SpringApplication.run(OndcAccountValidation.class, args);
}
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
@Bean
public SubscriberAccount getSubscriberAccount() {
return new SubscriberAccount();
}
}
域名:
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"subscriber_id",
"unique_key_id",
"participant_legal_name",
"participant_type",
"pan",
"bank_details",
"created_at",
"updated_at"
})
public class SubscriberAccount {
@JsonProperty("subscriber_id")
private String subscriber_id;
@JsonProperty("unique_key_id")
private String unique_key_id;
@JsonProperty("participant_legal_name")
private String participant_legal_name;
@JsonProperty("participant_type")
private String participant_type;
@JsonProperty("pan")
private PanDetails panDetails;
@JsonProperty("bank_details")
private BankAccountDetails accountDetails;
@JsonProperty("created_at")
private String created_at;
@JsonProperty("updated_at")
private String updated_at;
private List<SubscriberAccount> subsAccount;
/**/
public String getSubscriber_id() {
return subscriber_id;
}
public void setSubscriber_id(String subscriber_id) {
this.subscriber_id = subscriber_id;
}
public String getUnique_key_id() {
return unique_key_id;
}
public void setUnique_key_id(String unique_key_id) {
this.unique_key_id = unique_key_id;
}
public String getParticipant_legal_name() {
return participant_legal_name;
}
public void setParticipant_legal_name(String participant_legal_name) {
this.participant_legal_name = participant_legal_name;
}
public String getParticipant_type() {
return participant_type;
}
public void setParticipant_type(String participant_type) {
this.participant_type = participant_type;
}
public PanDetails getPanDetails() {
return panDetails;
}
public void setPanDetails(PanDetails panDetails) {
this.panDetails = panDetails;
}
public BankAccountDetails getAccountDetails() {
return accountDetails;
}
public void setAccountDetails(BankAccountDetails accountDetails) {
this.accountDetails = accountDetails;
}
public String getCreated_at() {
return created_at;
}
public void setCreated_at(String created_at) {
this.created_at = created_at;
}
public String getUpdated_at() {
return updated_at;
}
public void setUpdated_at(String updated_at) {
this.updated_at = updated_at;
}
public SubscriberAccount(String subscriber_id, String unique_key_id, String participant_legal_name,
String participant_type, PanDetails panDetails, BankAccountDetails accountDetails, String created_at,
String updated_at) {
super();
this.subscriber_id = subscriber_id;
this.unique_key_id = unique_key_id;
this.participant_legal_name = participant_legal_name;
this.participant_type = participant_type;
this.panDetails = panDetails;
this.accountDetails = accountDetails;
this.created_at = created_at;
this.updated_at = updated_at;
}
public SubscriberAccount() {
}
public List<SubscriberAccount> getSubsAccount() {
return subsAccount;
}
public void setSubsAccount(List<SubscriberAccount> subsAccount) {
this.subsAccount = subsAccount;
}
}
控制器:
@RestController
public class AccountValidationController {
@Autowired
FetchRegistrationDetails fetchRegistrationDetailsSerivce;
@Autowired
private RestTemplate restTemplate;
@RequestMapping(value ="/registration", consumes ="application/json" , produces="application/json")
public ResponseEntity<List<SubscriberAccount>> getRegistrationDetails() throws URISyntaxException {
List<SubscriberAccount> subAccountList = fetchRegistrationDetailsSerivce.fetchRegistrationDetails();
return ResponseEntity.ok(subAccountList);
}
}
界面:
public interface FetchRegistrationDetails {
public List<SubscriberAccount> fetchRegistrationDetails() throws URISyntaxException;
}
服务实现:
@Service("fetchRegistrationDetails")
public class FetchRegistrationDetailsImpl implements FetchRegistrationDetails {
@Autowired
RestTemplate restTemplate;
@Autowired(required = true)
SendToExcel sendToExcel;
public List<SubscriberAccount> fetchRegistrationDetails() throws URISyntaxException {
// TODO Auto-generated method stub
String url = "http://localhost:8091/api/registrationAPI";
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type", "application/json");
List<SubscriberAccount> subAccList = (List<SubscriberAccount>) restTemplate.getForObject(url,
List.class, headers);
// List<SubscriberAccount> subcrLi st = subAccList.getBody();
for (SubscriberAccount subscriberAccount : subAccList) {
System.out.println(subscriberAccount);
}
sendToExcel.saveDataToExcel(subAccList);
return subAccList;
}
}
域名: 与上面相同的域
控制器: @RestController
public class SimulatorController {
static final String url = "http:localhost:8091/api/registrationAPI";
@Autowired
private SimulatorService simulatorService;
@GetMapping(value ="/registrationAPI", consumes ="application/json" , produces="application/json")
public ResponseEntity<List<SimulatorDTO>> sendRegistrationData() throws Exception {
List<SimulatorDTO> simulatorEntity = simulatorService.sendRegistrationData();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return ResponseEntity.status(HttpStatus.OK).headers(headers).body(simulatorEntity);
}
}
服务接口:
@Component
public interface SimulatorService {
public List<SimulatorDTO> sendRegistrationData() throws FileNotFoundException, IOException;
List<SimulatorDTO> fetchDetailsFromExcel(File file) throws FileNotFoundException, IOException;
}
服务实现:
@Service
public class SimulatorServiceImpl implements SimulatorService {
@Override
public List<SimulatorDTO> sendRegistrationData() throws FileNotFoundException, IOException {
File file = new File("D:\\Workspace\\RegistrationData.xlsx");
List<SimulatorDTO> list = new ArrayList<>();
PanDTO panDTO = new PanDTO("AVLPT1847Q", "DummyName", "12-12-2011");
BankDTO bankDTO = new BankDTO("202120211077", "SBIN000836", "DummyName",
"Nodal");
list.add(new SimulatorDTO("ABC.com", "123456789ABC", "DUmmyName",
panDTO, bankDTO, "2023-01-23", "2023-01-23"));
list.add(new SimulatorDTO("AcC.com", "1233356789ABC", "DuummyName", panDTO, bankDTO,
"2023-01-24", "2023-01-24"));
System.out.print(list);
return list;
}
请告诉我你的想法。 注意:我在这里没有使用任何数据库,我只是保存在 excel 中。
我尝试手动设置标题,Content-Type 是 Application/json。但仍然没有用,它说 415 错误代码。
如果能避免这些瓶颈,我期待一种新方法和最常用的方法。
我认为问题在于您正在使用
restTemplate.getForObject
,它不接受标题作为第三个参数但是uriVariables
url - the URL
responseType - the type of the return value
uriVariables - the variables to expand the template
请尝试使用
restTemplate.exchange
并将 requestEntity 作为第三个参数传递。你可以从这样的标题中创建它们
HttpEntity<Void> requestEntity = new HttpEntity<>(headers);
所以你应该有这样的东西
restTemplate.exchange(url, HttpMethod.GET, requestEntity, List.class);