В моем тестовом приложении я пытаюсь создать API Springboot.У меня есть один API, через который я получаю список объектов, которые содержат дубликат идентификатора (sid).Теперь я хочу отфильтровать этот список по sid и объединить значения (custRefId) с соответствующими идентификаторами.
Вывод списка из Api:
[
{
"sId": 101,
"sName": "Nike",
"custRefId": "S1234567890a1001INR"
},
{
"sId": 201,
"sName": "Addidas",
"custRefId": "S1234567895a1004INR"
},
{
"sId": 501,
"sName": "U.S Polo",
"custRefId": "S1234567000a1258INR"
},
{
"sId": 501,
"sName": "U.S Polo",
"custRefId": "S1234567000a1011INR"
},
{
"sId": 501,
"sName": "U.S Polo",
"custRefId": "S1234567899a1008INR"
}
]
Требуемый выход:
{
"sId": 101,
"sName": "Nike",
"custRefId": "S1234567890a1001INR"
},
{
"sId": 201,
"sName": "Addidas",
"custRefId": "S1234567895a1004INR"
},
{
"sId": 501,
"sName": "U.S Polo",
"custRefId": {"S1234567000a1258INR","S1234567000a1011INR","S1234567899a1008INR"}
}
enter code here
Мой тестовый бин
package com.example.easynotes.model;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import com.example.easynotes.controller.CompositeKeyEntity;
@Entity(name = "TestApi")
@Table(name="test")
public class TestApi implements Serializable{
/**
*
*/
private static final long serialVersionUID = -6972984895555407355L;
@EmbeddedId
private CompositeKeyEntity CompositeKeyEntity;
@Column(name = "S_Id")
private Long sId;
@Column(name = "S_Name")
private String sName;
@Column(name = "B_Id")
private int bId;
@Column(name = "B_Name")
private String bName;
@Column(name = "Login_Date")
private Date loginDate;
@Column(name = "Amount")
private int amount;
@Column(name = "B_Acc")
private String bAccount;
public TestApi() {
super();
}
public TestApi(com.example.easynotes.controller.CompositeKeyEntity compositeKeyEntity, Long sId, String sName,
int bId, String bName, Date loginDate, int amount, String bAccount) {
super();
CompositeKeyEntity = compositeKeyEntity;
this.sId = sId;
this.sName = sName;
this.bId = bId;
this.bName = bName;
this.loginDate = loginDate;
this.amount = amount;
this.bAccount = bAccount;
}
public CompositeKeyEntity getCompositeKeyEntity() {
return CompositeKeyEntity;
}
public void setCompositeKeyEntity(CompositeKeyEntity compositeKeyEntity) {
CompositeKeyEntity = compositeKeyEntity;
}
public Long getsId() {
return sId;
}
public void setsId(Long sId) {
this.sId = sId;
}
public String getsName() {
return sName;
}
public void setsName(String sName) {
this.sName = sName;
}
public int getbId() {
return bId;
}
public void setbId(int bId) {
this.bId = bId;
}
public String getbName() {
return bName;
}
public void setbName(String bName) {
this.bName = bName;
}
public Date getLoginDate() {
return loginDate;
}
public void setLoginDate(Date loginDate) {
this.loginDate = loginDate;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
public String getbAccount() {
return bAccount;
}
public void setbAccount(String bAccount) {
this.bAccount = bAccount;
}
}
Test Api Repository:
package com.example.easynotes.repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import com.example.easynotes.controller.CompositeKeyEntity;
import com.example.easynotes.model.TestApi;
@Repository
public interface TestRepo extends JpaRepository<TestApi, CompositeKeyEntity>{
@Query(
value = "SELECT * FROM test WHERE S_Id = :sId",
nativeQuery = true
)
public TestApi getSDataById(@Param("sId")long sId);
@Query(value="SELECT h.S_Id,h.S_Name,h.B_Id,h.B_Name,h.Login_Date,h.Amount,h.B_Acc,h.S_Acc,h.B_Ref_Id,h.CRR FROM test h order by h.Login_Date asc",nativeQuery = true )
public List<TestApi> getSellerData();
@Query(
value = "SELECT * FROM test WHERE S_Id = :sId and S_Name =:sName",
nativeQuery = true
)
public List<TestApi> findDataBySIDAndSName(long sId, String sName);
}
TestController Класс:
package com.example.easynotes.controller;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.easynotes.model.TestApi;
import com.example.easynotes.model.TestBean;
import com.example.easynotes.repository.TestRepo;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
@RestController
@RequestMapping("/LCRF")
public class TestController {
List<TestApi> lcrf = new ArrayList<TestApi>();
List<TestApi> tmp = new ArrayList<TestApi>();
@Autowired
TestRepo lRepo;
@Autowired
Assembler assembler;
@GetMapping("/allData")
public List<TestBean> getAll()
{
getMergeIds(assembler.sBBeanList(lRepo.getSellerData()));
return assembler.sBBeanList(lRepo.getSellerData());
}
@GetMapping("/allData/{sellerId}")
public TestApi getSellerDetail(@PathVariable(value = "sellerId")Long sellerId)
{
return lRepo.getSDataById(sellerId);
}
@GetMapping("/allData/{sId}/{sName}")
public List<TestApi> getDataByIdAndName(@PathVariable(value = "sId")Long sId,@PathVariable(value = "sId")String sName)
{
lcrf = lRepo.findDataBySIDAndSName(sId, sName);
return lcrf;
}
public List<TestMergeIds> getMergeIds(List<TestBean> testBean)
{
List<TestBean> testList = new ArrayList<TestBean>();
Multimap<Long, String> multiMap = ArrayListMultimap.create();
TestMergeIds testMergeIdsBean = new TestMergeIds();
for(TestBean bean: testBean) {
multiMap.put(bean.getsId(), bean.getCustRefId());
}
Set<Long> keys = multiMap.keySet();
List<String> targetList = new ArrayList<>();
for (Long key : keys) {
}
System.out.println("Print list values ---111---"+targetList);
return null;
}
}
Класс CompositeKeyEntity:
package com.example.easynotes.controller;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;
@Embeddable
public class CompositeKeyEntity implements Serializable{
private static final long serialVersionUID = 1581433221916043048L;
@Column(name="S_Acc")
private String sAccount;
@Column(name="B_Ref_Id")
private String bRefId;
@Column(name="CRR")
private String crr;
public String getsAccount() {
return sAccount;
}
public void setsAccount(String sAccount) {
this.sAccount = sAccount;
}
public String getbRefId() {
return bRefId;
}
public void setbRefId(String bRefId) {
this.bRefId = bRefId;
}
public String getCrr() {
return crr;
}
public void setCrr(String crr) {
this.crr = crr;
}
}
Класс ассемблера:
package com.example.easynotes.controller;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import com.example.easynotes.model.TestApi;
import com.example.easynotes.model.TestBean;
@Component
public class Assembler {
public List<TestBean> sBBeanList(final List<TestApi> testApiList)
{
final List<TestBean> SellerBuyerBeanList = new ArrayList<>(testApiList.size());
if(!testApiList.isEmpty())
{
for(final TestApi lcrfApiEntity : testApiList) {
final TestBean sellerBuyerBeanlist = new TestBean();
BeanUtils.copyProperties(lcrfApiEntity, sellerBuyerBeanlist);
sellerBuyerBeanlist.setCustRefId(formSellerBuyerRefId(lcrfApiEntity.getCompositeKeyEntity()));
SellerBuyerBeanList.add(sellerBuyerBeanlist);
}
}
return SellerBuyerBeanList;
}
private String formSellerBuyerRefId(final CompositeKeyEntity compositeKeyEntity)
{
StringBuilder sBuilder=new StringBuilder();
sBuilder.append(compositeKeyEntity.getsAccount());
sBuilder.append(compositeKeyEntity.getbRefId());
sBuilder.append(compositeKeyEntity.getCrr());
return sBuilder.toString();
}
}
enter code here