Spring Boot将特定参数传递给另一个API。

huangapple go评论69阅读模式
英文:

Spring boot hand over specific parameter to another api

问题

// 代码部分不要翻译,以下为翻译内容

我想将来自API的Json响应的参数保存下来以便交给另一个API处理另一个API应该接收帐户余额并判断客户是否有正数或负数余额

JSON:
```json
id:          1
name:       "Lars"
lastname:  "Günther"
birth:      "1995-10-10"
sex:          "m"
road:        "Bahnhofsstrasse 17"
town:        "Borken"
account:

 0: 
    bankaccNr:      3
    customerstatus: "private client"
    accountbalance:   -3127
    openingdate:     "2001-01-23"
    iban:             "DE37500105174133576347"
  
 1: 
    bankaccNr:      16
    customerstatus: "corporate client"
    accountbalance: 71764
    openingdate:     "2014-07-09"
    iban:             "DE09500105177725527397"

postalcode: 46325

这是我调用此API时获得的JSON。0和1不是模型的一部分。它们在将这两个帐户映射到客户时自动生成。

Customer:

@ManyToMany(targetEntity = Account.class, mappedBy = "customer", cascade = CascadeType.ALL)
private Set<Account> account = new HashSet<>();

public Customer() {}

public Customer(String name, String lastname, Date birth, char sex, String road, int postalcode, String town) {
    this.name = name;
    this.lastname = lastname;
    this.birth = birth;
    this.sex = sex;
    this.road = road;
    this.postalcode = postalcode;
    this.town = town;
}
// 获取器/设置器...

我应该将其保存为对象还是哈希映射?我如何处理此对象?如果客户有多个帐户怎么办?

@RequestMapping(value = { "/{id}/liquid" }, method = RequestMethod.GET)
public Konto getLiquid(@PathVariable(value = "id") Long customer_id) {

    String url = "http://localhost:8081/mybank/customer/data/{id}";
    Customer customer = restTemplate.getForObject(url, Customer.class, customer_id);
    // 'object or hashmap?' 
    HashMap<String, Map> answer = restTemplate.getForObject(url, new HashMap<>().getClass(), customer_id);
        
    String urlForServiceB = "http://localhost:8082/liquicontroller/liquiditat/{value}";

    int firstvalue = ??? // 您需要指定一个值
    // 获取http响应 --> true或false
    return restTemplate.getForObject(urlForServiceB, String.class, firstvalue);
}

我如何向另一个API发送多个请求?

// 您可以通过在循环中迭代客户的帐户集合,并为每个帐户执行API请求。

for (Account account : customer.getAccount()) {
    // 构建每个帐户的请求URL
    String accountUrl = "http://api-url/account/" + account.getBankaccNr();
    
    // 执行API请求并处理响应
    AccountResponse accountResponse = restTemplate.getForObject(accountUrl, AccountResponse.class);
    
    // 在这里执行您的逻辑,例如判断帐户余额正负等等
}

请注意,上述代码中的一些类和变量名可能需要根据您的项目实际情况进行调整。

英文:

I want to save the parameters of a Json Response from an API to hand over to another API. The other API should receive account balance and would say if the customer has a positive or negative balance.

JSON:

id:	       1
name:	   &quot;Lars&quot;
lastname:  &quot;G&#252;nther&quot;
birth:     &quot;1995-10-10&quot;
sex:       &quot;m&quot;
road:      &quot;Bahnhofsstrasse 17&quot;
town:      &quot;Borken&quot;
account:	

 0:	
    bankaccNr:	      3
    customerstatus:	  &quot;private client&quot;
    accountbalance:   -3127
    openingdate:      &quot;2001-01-23&quot;
    iban:	          &quot;DE37500105174133576347&quot;
  
 1:	
    bankaccNr:	      16
    customerstatus:	  &quot;corporate client&quot;
    accountbalance:   71764
    openingdate:      &quot;2014-07-09&quot;
    iban:	          &quot;DE09500105177725527397&quot;

postalcode:	46325

That is the JSON I get when I call this API. 0 and 1 are not part of the model. They are automatically generated when this to accounts get mapped to the customer.

Customer:

@ManyToMany(targetEntity = Account.class, mappedBy = &quot;customer&quot;, cascade = CascadeType.ALL)
private Set&lt;Account&gt; account= new HashSet&lt;&gt;();
	
public Customer() {}
	
public Customer(String name, String lastname, Date birth, char sex, String road, int postalcode, String town) {
    this.name=name;
	this.lastname=lastname;
	this.birth=birth;
	this.sex=sex;
	this.road=road;
	this.postalcode=postalcode;
	this.town=town;			
}
//getters/setters...

Should I save it as an object or as a hashmap. How do I handle this object? What If a customer has more than one account?

@RequestMapping(value = { &quot;/{id}/liquid&quot; }, method = RequestMethod.GET)
public Konto getLiquid(@PathVariable(value = &quot;id&quot;) Long customer_id){   
		
	String url = &quot;http://localhost:8081/mybank/customer/data/{id}&quot;;
	Customer customer= restTemplate.getForObject(url, Kunde.class,kundeid);
	&#39;object or hashmap?&#39; 
	HashMap &lt;String,Map&gt; answer= restTemplate.getForObject(url, new HashMap&lt;&gt;().getClass(),kundeid);
	        
	String urlForServiceB = &quot;http://localhost:8082/liquicontroller/liquiditat/{value}&quot;;
	    
    int firstvalue = ???
    // get http response --&gt; true or false
	return restTemplate.getForObject(urlForServiceB, String.class,firstvalue);
}

How can I send multiple requests to the other API?

答案1

得分: 0

客户应该拥有一个帐户列表。如果根据您的业务需求,如果是多对多映射,则帐户应该拥有客户列表。

从上面的描述来看,每个客户都有多个帐户,因此我们可以在客户中拥有帐户列表。

@OneToManyMapping
List<Account> account;

RestTemplate将从实体映射JSON到您的客户。

List<Account> account = customer.getListOfAccounts();

for (Account ac : account) {
    // ac将持有每个帐户对象
}

为了使上述内容起作用,响应应该在客户内包含嵌套的列表。如果没有这种情况,那么对于每个帐户ID,必须反复调用以下内容:

RestTemplate restTemplate = new RestTemplate();
Customer customer = restTemplate.getForObject(uri, Customer.class);
英文:

Customer should be having a list of account. If according to your business requirement if it many to many mapping then Account should be having list of customers.

From Above it looks like each customer having many accounts so we can have list of account in customer

@OneTOManyMapping  
List&lt;Account&gt; account

Resttemplate will map the json from entity to your customer.

List&lt;Account&gt; account = customer.getlistOfAccounts

for(Account ac:account){
// ac will have each account object

For above to work response should contain nested list within customer. If there is no such case then below has to be invoked again and again for each account id

RestTemplate restTemplate = new RestTemplate();
Customer Customer = restTemplate.getForObject(uri, Customer.class);

huangapple
  • 本文由 发表于 2020年9月7日 22:02:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63779120.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定