REST - 当我使用 GET 时,SparkJava 不更新对象 JSON

REST - SparkJava not update object JSON when I use GET

在获取文本的页面上进行更改后,更新对象 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 主要:

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;
    }
}

问题出在哪里?如果我重新启动码头服务器,JSON 会更新。否则不行。

如果我没理解错的话,你在重复调用你的服务时总是得到相同的结果?您希望更改条目是因为您获取它们的原始来源发生了变化?

这是因为当 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);
    }
}

这只是为了始终获得相同的值而进行的修复。根据您的服务被调用的频率,这可能会导致对您正在查询的后端服务器发出大量请求。在这种情况下,您应该添加某种缓存,例如,当最后一个数据太旧时,它只会从后面获取新数据。但那是另外一回事了。