Json resttemplate使用api错误
我正在使用resttemplate使用rest api,但出现了一些错误:Json resttemplate使用api错误,json,spring,Json,Spring,我正在使用resttemplate使用rest api,但出现了一些错误: Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class org.apache.xpath.operations.String] from String value; no single-String constructor/factory me
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class org.apache.xpath.operations.String] from String value; no single-String constructor/factory method (through reference chain: com.shuohao.liu.newlifeinus.web.model.WeatherSearch["forecast"]->com.shuohao.liu.newlifeinus.web.model.WeatherForecast["simpleforecast"]->com.shuohao.liu.newlifeinus.web.model.WeatherSimpleForecast["forecastday"]->com.shuohao.liu.newlifeinus.web.model.WeatherForecastday["date"]->com.shuohao.liu.newlifeinus.web.model.WeatherDate["pretty"])
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator._createFromStringFallbacks(StdValueInstantiator.java:428)
at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:299)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromString(BeanDeserializerBase.java:1056)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:136)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:123)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:464)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:98)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:295)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:464)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:98)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:295)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:230)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:207)
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:23)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:464)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:98)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:295)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:464)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:98)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:295)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:464)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:98)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:295)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2094)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:221)
... 86 more
以下是我如何使用resttemplate:
@Service
public class WeatherService {
public WeatherSearch getWeatherForecast(String city, String state, String country) {
RestTemplate template = new RestTemplate();
String url = "http://api.wunderground.com/api/252ab947c92cb32e/forecast10day/q/{state}/{city}.json";
Map<String, String> var = new HashMap<String, String>();
var.put("state", state);
var.put("city", city);
WeatherSearch search = new WeatherSearch();
try {
search = template.getForObject(url, WeatherSearch.class, var);
}catch(ResourceAccessException ex){
ex.printStackTrace();
System.out.println(ex.getClass());
}catch (Exception ex) {
ex.printStackTrace();
System.out.println(ex.getClass());
}
return search;
}
}
@服务
公营气象服务{
公共天气搜索getWeatherForecast(字符串城市、字符串州、字符串国家){
RestTemplate=新的RestTemplate();
字符串url=”http://api.wunderground.com/api/252ab947c92cb32e/forecast10day/q/{state}/{city}.json”;
Map var=newhashmap();
var.put(“状态”,状态);
var.put(“城市”,城市);
WeatherSearch=新的WeatherSearch();
试一试{
search=template.getForObject(url,WeatherSearch.class,var);
}捕获(ResourceAccessException ex){
例如printStackTrace();
System.out.println(例如getClass());
}捕获(例外情况除外){
例如printStackTrace();
System.out.println(例如getClass());
}
返回搜索;
}
}
json字符串太长,无法从中访问
最后,这里是我为这种json结构创建的对象:
@JsonIgnoreProperties(ignoreUnknown=true)
public class WeatherSearch {
@JsonProperty(value="forecast")
private WeatherForecast forecast;
}
@JsonIgnoreProperties(ignoreUnknown=true)
public class WeatherForecast {
@JsonProperty(value="simpleforecast")
private WeatherSimpleForecast simpleForecast;
}
@JsonIgnoreProperties(ignoreUnknown=true)
public class WeatherSimpleForecast {
@JsonProperty(value="forecastday")
private ArrayList<WeatherForecastday> forecastday;
}
@JsonIgnoreProperties(ignoreUnknown=true)
public class WeatherForecastday {
@JsonProperty(value="period")
private String period;
@JsonProperty(value="high")
private WeatherTemperature high;
@JsonProperty(value="low")
private WeatherTemperature low;
@JsonProperty(value="condition")
private String condition;
@JsonProperty(value="icon_url")
private String iconUrl;
@JsonProperty(value="date")
private WeatherDate date;
}
@JsonIgnoreProperties(ignoreUnknown=true)
public class WeatherDate {
@JsonProperty(value="pretty")
private String pretty;
@JsonProperty(value="day")
private String day;
@JsonProperty(value="month")
private String month;
@JsonProperty(value="year")
private String year;
@JsonProperty(value="monthname")
private String monthName;
@JsonProperty(value="monthname_short")
private String monthShortName;
@JsonProperty(value="weekday")
private String weekday;
@JsonProperty(value="weekday_short")
private String weekdayShort;
}
@JsonIgnoreProperties(ignoreUnknown=true)
公共类天气搜索{
@JsonProperty(value=“预测”)
私人天气预报;
}
@JsonIgnoreProperties(ignoreUnknown=true)
公共天气预报{
@JsonProperty(value=“simpleforecast”)
私人天气预报;
}
@JsonIgnoreProperties(ignoreUnknown=true)
公共类天气预报{
@JsonProperty(value=“forecastday”)
私人ArrayList预报日;
}
@JsonIgnoreProperties(ignoreUnknown=true)
公共类天气预报日{
@JsonProperty(value=“period”)
私有字符串周期;
@JsonProperty(value=“高”)
私人天气温度高;
@JsonProperty(value=“低”)
私人天气温度低;
@JsonProperty(value=“条件”)
私有字符串条件;
@JsonProperty(value=“icon\u url”)
私有字符串Iconur;
@JsonProperty(value=“日期”)
私人天气日期;
}
@JsonIgnoreProperties(ignoreUnknown=true)
公共类天气日期{
@JsonProperty(value=“pretty”)
私人串漂亮;
@JsonProperty(value=“day”)
私人弦日;
@JsonProperty(value=“月”)
私人串月;
@JsonProperty(value=“年”)
私人弦年;
@JsonProperty(value=“monthname”)
私人字符串monthName;
@JsonProperty(value=“monthname_short”)
私有字符串monthShortName;
@JsonProperty(value=“weekday”)
私人字符串工作日;
@JsonProperty(value=“weekday\u short”)
私人字符串工作日短;
}
删除导入org.apache.xpath.operations.String的import语句
。我猜您在模型或服务的某个地方使用了intellij 15,您已经导入了org.apache.xpath.operations.String
,这是错误的String
,只需删除它。是的,就是这样。我错过了org.apache.xpath.operatinis.String的导入。谢谢