Amr*_*eya 3 java resttemplate spring-boot
我正在遵循 spring 网站https://spring.io/guides/gs/sumption-rest/的启动指南。
我没有遵循确切的教程,因为我正在使用另一个端点:http : //www.omdbapi.com ? s= rush。
我在将 JSON 转换为 POJO 时遇到问题。我没有收到任何错误或异常。有人能指出我哪里出错了吗?
你可以在这里找到完整的代码
这是我的 POJO:
package com.sample.restapi.model;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown=true)
public class SearchResponse {
private List<Search> search;
private String totalResults;
private String response;
public SearchResponse() {
}
public List<Search> getSearch() {
return search;
}
public void setSearch(List<Search> search) {
this.search = search;
}
public String getTotalResults() {
return totalResults;
}
public void setTotalResults(String totalResults) {
this.totalResults = totalResults;
}
public String getResponse() {
return response;
}
public void setResponse(String response) {
this.response = response;
}
@Override
public String toString() {
return "SearchResponse [search=" + search + ", totalResults=" + totalResults + ", response=" + response + "]";
}
}
Run Code Online (Sandbox Code Playgroud)
这是 Search.java
package com.sample.restapi.model;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown=true)
public class Search {
private String title;
private String year;
private String imdbID;
private String type;
private String poster;
public Search() {
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getImdbID() {
return imdbID;
}
public void setImdbID(String imdbID) {
this.imdbID = imdbID;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getPoster() {
return poster;
}
public void setPoster(String poster) {
this.poster = poster;
}
@Override
public String toString() {
return "Search [title=" + title + ", year=" + year + ", imdbID=" + imdbID + ", type=" + type + ", poster="
+ poster + "]";
}
}
Run Code Online (Sandbox Code Playgroud)
这是驱动程序类。
package com.sample.restapi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.client.RestTemplate;
import com.sample.restapi.model.SearchResponse;
@SpringBootApplication
public class ConsumerApplication {
private static final Logger log = LoggerFactory.getLogger(ConsumerApplication.class);
public static void main(String[] args) {
RestTemplate restTemplate = new RestTemplate();
SearchResponse searchResponse = restTemplate.getForObject("http://www.omdbapi.com?s=rush", SearchResponse.class);
log.info(searchResponse.toString());
}
}
Run Code Online (Sandbox Code Playgroud)
控制台输出是:
14:34:12.941 [main] INFO com.sample.restapi.ConsumerApplication - SearchResponse [search=null, totalResults=344, response=null]
Run Code Online (Sandbox Code Playgroud)
您在 json 中缺少正确的属性标识符,响应和您的类在大写和小写字母中存在差异。在您的课程中使用 @JsonProperty。
@JsonProperty("Search")
private List<Search> search = new ArrayList<Search>();
private String totalResults;
@JsonProperty("Response")
private String response;
Run Code Online (Sandbox Code Playgroud)
您还应该在 Search 类中添加 @JsonProperty 注释。
| 归档时间: |
|
| 查看次数: |
5582 次 |
| 最近记录: |