我在获取文本的页面上更改后更新对象json有问题。在我的代码和响应API下面。
响应API:
[
{ "title": "„Jak wykorzystać media i nowoczesne technologie w edukacji?” – warsztaty dla nauczycieli",
"url": "http://www.up.krakow.pl/uniwersytet/aktualnosci/1772-jak-wykorzystac-media-i-nowoczesne-technologie-w-edukacji-warsztaty-dla-nauczycieli"
}
]
Java服务:
public class NewsService implements NewsServiceInterface {
private Document doc = Jsoup.connect("http://www.up.krakow.pl/uniwersytet/aktualnosci").get();
private Elements links = doc.select("div.page-header");
private LinkedHashMap<String, String> store = new LinkedHashMap<>();
public NewsService() throws IOException {
}
@Override
public List<News> getNews() {
List<News> newsList = new ArrayList<>();
for (Element element : links) {
String title = element.select("a[href]").text(); // get only text
String url = "http://www.up.krakow.pl" + element.select("a[href]").attr("href"); // get only link
if (!store.containsKey(title)) {
store.put(title, url);
}
}
for (Map.Entry<String, String> entry : store.entrySet()) {
newsList.add(new News(entry.getKey(), entry.getValue()));
}
return Lists.reverse(newsList);
}
}
Java控制器:
public class NewsController {
private static final String API_CONTEXT = "/api/v1";
public NewsController(final NewsService newsService) {
get(API_CONTEXT + "/getnews", (request, response) -> {
return newsService.getNews();
}, json());
}
Java POJO:
public class News implements Serializable {
@Expose
@SerializedName("id")
private String id;
@Expose
@SerializedName("title")
private String title;
@Expose
@SerializedName("url")
private String url;
@Expose
@SerializedName("counterAllNews")
private String counterAllNews;
public News() {
}
public News(String title, String url) {
this.title = title;
this.url = url;
}
// getter and setter
}
Java Main:
public class Hello {
public static void main(String[] args) throws IOException {
try {
new NewsController(new NewsService());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Java JSON:
public class JsonUtil {
public static String toJson(Object object) {
return new Gson().toJson(object);
}
public static ResponseTransformer json() {
return JsonUtil::toJson;
}
}
哪里有问题?如果我重新启动jetty服务器,JSON会更新。否则不会。
答案 0 :(得分:2)
如果我理解正确,在重复拨打您的服务时,您始终会得到相同的结果?并且您希望更改条目,因为您获取它们的原始来源会发生变化吗?
这是因为在实例化NewsService
时,您只能从 www.up.krakow.pl/uniwersytet/aktualnosci 中读取该信息。这只在你的主要方法中完成一次:
new NewsController(new NewsService());
更改NewsService
实施,以便您在每次获取时重新获取新闻数据:
public class NewsService implements NewsServiceInterface {
private LinkedHashMap<String, String> store = new LinkedHashMap<>();
public NewsService() throws IOException {
}
@Override
public List<News> getNews() {
Document doc = Jsoup.connect("http://www.up.krakow.pl/uniwersytet/aktualnosci").get();
Elements links = doc.select("div.page-header");
List<News> newsList = new ArrayList<>();
for (Element element : links) {
String title = element.select("a[href]").text(); // get only text
String url = "http://www.up.krakow.pl" + element.select("a[href]").attr("href"); // get only link
if (!store.containsKey(title)) {
store.put(title, url);
}
}
for (Map.Entry<String, String> entry : store.entrySet()) {
newsList.add(new News(entry.getKey(), entry.getValue()));
}
return Lists.reverse(newsList);
}
}
这只是获取始终相同值的修复方法。根据您的服务调用频率,这可能会导致向您查询的后端服务器发出大量请求。在这种情况下,您应该添加某种缓存,例如,当最后一个太旧时,仅从后面获取新数据。但那是一个不同的故事。