![](/img/trans.png)
[英]Convert List<Map<String, String>> to Object in Java Spring boot jpa
[英]How to map from JPA Object in spring boot
大家好:我有一個小問題希望你幫我解決。 我的問題是下一個,我必須根據谷歌地圖和經緯度找到更短的商店距離,無論如何,我有做這些的查詢,我的問題是當我調用jparepository
它返回一個數組和我需要以特定格式返回給我。
這是我試圖從jparepository
獲得的jparepository
@RequestMapping(value = "api/promos/distance/", method = RequestMethod.GET)
public ResponseEntity<?> distance(@RequestParam(required = false) Double latitude, Double longitude, Long radius) {
try {
return ResponseEntity.status(HttpStatus.OK)
.body(locationDAO.findDistance(latitude,longitude,radius));
} catch (Exception ex){
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ex.toString());
}
}
這是我的帶有參數的存儲庫並返回 2 個值
@Query(value = "select sp.id as id, \n" +
" ( 6371 * acos( cos( radians(:latitude) ) \n" +
" * cos( radians( sp.latitude )) \n" +
" * cos( radians( sp.longitude ) - radians(:longitude)) \n" +
" + sin( radians(:latitude) ) \n" +
" * sin( radians( sp.latitude ) ) ) ) AS distance \n" +
" from promo_locations sp \n" +
"having distance < :radius ORDER BY distance ASC", nativeQuery = true)
List<Object> findDistance(
@Param("latitude") Double latitude,
@Param("longitude") Double longitude,
@Param("radius") Long radius);
它向我展示了這個
[
[
1,
137.94383549078196
],
[
2,
137.94383549078196
]
]
但我需要以這種格式向我展示:
[
{
"id" : 1,
"distance" : 137.94383549078196
},
{
"id" : 2,
"distance" :137.94383549078196
}
]
如果有人知道如何映射或如何轉換,我將非常感謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.