Поиск в .json с использованием SpringJAVA

Программисты JAVA общаются здесь
Anonymous
Поиск в .json с использованием Spring

Сообщение Anonymous »

json-файл с такими данными

Код: Выделить всё

[
{
"id": 25651,
"title": "German Tour: Parliament Quarter & Reichstag glass dome",
"price": 14,
"currency": "$",
"rating": 4.8,
"specialOffer": false,
"supplierId": 1
},
{
"id": 6960,
"title": "Skip the Line: Pergamon Museum Berlin Tickets",
"price": 21,
"currency": "$",
"rating": 4.8,
"specialOffer": false,
"supplierId": 2
}
]
а вот Activity.java, сопоставленный с этим

Код: Выделить всё

import com.fasterxml.jackson.annotation.*;
import lombok.*;

@Data
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class Activity {
private Long id;
private String title;
private int price;
private String currency;
private double rating;
private boolean specialOffer;
}
У меня есть ActivityController, который выглядит так

Код: Выделить всё

@GetMapping("/filter")
public void filter(@RequestParam(name = "search", required = false, defaultValue = "NONE") String searchString, Model model) {
try {
model.addAttribute("searchString", searchString);
//create ObjectMapper instance
ObjectMapper objectMapper = new ObjectMapper();
//read JSON file and convert to a list of activities
var fileInputStream  = new ClassPathResource("static/activities.json").getInputStream();
var activities = objectMapper.readValue(fileInputStream, new TypeReference() {
});
var filteredData = searchFilter(title, activities);
System.out.println("filteredData = " + filteredData);
model.addAttribute("activities", activities);
} catch (Exception e) {
e.printStackTrace();
}
}
Если я хочу запросить его, например /api/filter?search=german. и он будет проходить через все поля в полях «Активность», как мне этого добиться?
Спасибо за помощь

Подробнее здесь: https://stackoverflow.com/questions/790 ... ing-spring

Вернуться в «JAVA»