REST-SparkJava在使用GET时不更新对象JSON
在我获取文本的页面上进行更改后,我遇到了更新对象json的问题。下面是我的代码和响应API 响应API:REST-SparkJava在使用GET时不更新对象JSON,java,json,rest,spark-java,Java,Json,Rest,Spark Java,在我获取文本的页面上进行更改后,我遇到了更新对象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
[
{ "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"
}
]
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);
}
}
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
}
public class Hello {
public static void main(String[] args) throws IOException {
try {
new NewsController(new NewsService());
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class JsonUtil {
public static String toJson(Object object) {
return new Gson().toJson(object);
}
public static ResponseTransformer json() {
return JsonUtil::toJson;
}
}
Java服务:
[
{ "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"
}
]
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);
}
}
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
}
public class Hello {
public static void main(String[] args) throws IOException {
try {
new NewsController(new NewsService());
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class JsonUtil {
public static String toJson(Object object) {
return new Gson().toJson(object);
}
public static ResponseTransformer json() {
return JsonUtil::toJson;
}
}
JavaPOJO:
[
{ "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"
}
]
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);
}
}
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
}
public class Hello {
public static void main(String[] args) throws IOException {
try {
new NewsController(new NewsService());
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class JsonUtil {
public static String toJson(Object object) {
return new Gson().toJson(object);
}
public static ResponseTransformer json() {
return JsonUtil::toJson;
}
}
Javamain:
[
{ "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"
}
]
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);
}
}
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
}
public class Hello {
public static void main(String[] args) throws IOException {
try {
new NewsController(new NewsService());
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class JsonUtil {
public static String toJson(Object object) {
return new Gson().toJson(object);
}
public static ResponseTransformer json() {
return JsonUtil::toJson;
}
}
JavaJSON:
[
{ "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"
}
]
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);
}
}
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
}
public class Hello {
public static void main(String[] args) throws IOException {
try {
new NewsController(new NewsService());
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class JsonUtil {
public static String toJson(Object object) {
return new Gson().toJson(object);
}
public static ResponseTransformer json() {
return JsonUtil::toJson;
}
}
问题在哪里?如果我重新启动jetty服务器,JSON将更新。否则不会。如果我理解正确,您在重复呼叫服务时总是得到相同的结果?您希望得到不断变化的条目,因为您获取它们的原始源发生了变化 这是因为当
新闻服务
实例化时,您只能从www.up.krakow.pl/uniwersytet/aktualnoci读取一次该信息。这在您的主要方法中只执行一次:
new NewsController(new 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);
}
}
公共类NewsService实现NewsServiceInterface{
私有LinkedHashMap存储=新建LinkedHashMap();
公共新闻服务()引发IOException{
}
@凌驾
公共列表getNews(){
Document doc=Jsoup.connect(“http://www.up.krakow.pl/uniwersytet/aktualnosci).get();
元素链接=文件选择(“div.page-header”);
List newsList=newarraylist();
用于(元素:链接){
String title=element.select(“a[href]”).text();//仅获取文本
字符串url=”http://www.up.krakow.pl“+element.select(“a[href]”).attr(“href”);//仅获取链接
如果(!store.containsKey(标题)){
store.put(标题、url);
}
}
对于(Map.Entry:store.entrySet()){
添加(新新闻(entry.getKey(),entry.getValue());
}
返回列表。反向(新闻列表);
}
}
这只是一个修复程序,用于获得始终相同的值。根据调用服务的频率,这可能会导致对正在查询的后端服务器的大量请求。在这种情况下,您应该添加某种缓存,例如,当最后一个数据太旧时,它只从后面获取新数据。但那是另一回事。如果我没有弄错的话,您在反复呼叫服务时总是得到相同的结果?您希望得到不断变化的条目,因为您获取它们的原始源发生了变化 这是因为当
新闻服务
实例化时,您只能从www.up.krakow.pl/uniwersytet/aktualnoci读取一次该信息。这在您的主要方法中只执行一次:
new NewsController(new 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);
}
}
公共类NewsService实现NewsServiceInterface{
私有LinkedHashMap存储=新建LinkedHashMap();
公共新闻服务()引发IOException{
}
@凌驾
公共列表getNews(){
Document doc=Jsoup.connect(“http://www.up.krakow.pl/uniwersytet/aktualnosci).get();
元素链接=文件选择(“div.page-header”);
List newsList=newarraylist();
用于(元素:链接){
String title=element.select(“a[href]”).text();//仅获取文本
字符串url=”http://www.up.krakow.pl“+element.select(“a[href]”).attr(“href”);//仅获取链接
如果(!store.containsKey(标题)){
store.put(标题、url);
}
}
对于(Map.Entry:store.entrySet()){
添加(新新闻(entry.getKey(),entry.getValue());
}
返回列表。反向(新闻列表);
}
}
这只是一个修复程序,用于获得始终相同的值。根据调用服务的频率,这可能会导致对正在查询的后端服务器的大量请求。在这种情况下,您应该添加某种缓存,例如,当最后一个数据太旧时,它只从后面获取新数据。但那是另一回事。谢谢你的回答。你说得对。我更改了代码,我会通知您结果谢谢您的回答。你说得对。我更改了代码,我将通知结果