code below return me list of two objects like this:
[
{
value: "ssss",
},
{
value: "ssss",
},
]
but i want to return list like this:
{
value1:"xxxx",
value2:"xxxx"
}
here is my code
@Override
public List<OrderModel> getOrderByCode(UserEntity user) throws ErrorException {
List<UserEntity> userOrder = userRepository.findByUserAndOrderCodeIn(user, Arrays.asList(OrderCodes.OK, OrderCodes.DONE));
List<OrderModel> om = new ArrayList<OrderModel>();
for(UserAnswerEntity userAnswerEntity : userAnswers){
OrderModel orderModel = new OrderModel();
orderModel.setValue(userAnswerEntity.getValue());
om.add(orderModel );
}
return om;
}
public class OrderModel{
String value;
//get,set
}
can someon tell me how can i return only list like above?
You are trying to make a custom response. One of the possible way is this
Map<String,String> map = new HashMap<String,String>();
map.put("key1", "value1");
map.put("key2", "value2");
System.out.println(map.toString().replace("{", "[").replace("}", "]"));
Change the return type of getOrderByCode() to string and convert your List object to JSON string.
private static final ObjectMapper jsonMapper = new ObjectMapper().setSerializationInclusion(Include.NON_NULL);
return jsonMapper.writeValueAsString(om);
for you example, you can use Map
instead of List
try this:
AtomicInteger index = new AtomicInteger()
Function<UserAnswerEntity, String> createIndex = (userAnswer) -> "value" + index.getAndIncrement();
return userAnswers.stream().collect(Collectors.toMap(createIndex, UserAnswerEntity::getValue));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.