主页
/
user-501838
Roman's questions
我正在尝试从天气 apishka 获得响应 apishka 向城市坐标请求发送这样的响应
[
{
"name": "Moscow",
"local_names": {
"it": "Mosca",
"zh": "莫斯科",
"lv": "Maskava",
"id": "Moskwa",
"ay": "Mosku",
"lt": "Maskva",
"ascii": "Moscow",
"sh": "Moskva",
"uz": "Moskva",
"st": "Moscow",
"co": "Moscù",
"bo": "མོ་སི་ཁོ།",
"fi": "Moskova",
"wo": "Mosku",
"dz": "མོསི་ཀོ",
"sl": "Moskva",
"ps": "مسکو",
"sc": "Mosca",
"hr": "Moskva",
"kv": "Мӧскуа",
"tk": "Moskwa",
"sr": "Москва",
"mg": "Moskva",
"ko": "모스크바",
"wa": "Moscou",
"sg": "Moscow",
"sv": "Moskva",
"ch": "Moscow",
"mn": "Москва",
"ta": "மாஸ்கோ",
"am": "ሞስኮ",
"an": "Moscú",
"io": "Moskva",
"su": "Moskwa",
"ro": "Moscova",
"oc": "Moscòu",
"feature_name": "Moscow",
"bg": "Москва",
"nn": "Moskva",
"en": "Moscow",
"li": "Moskou",
"ga": "Moscó",
"ak": "Moscow",
"ba": "Мәскәү",
"da": "Moskva",
"so": "Moskow",
"ru": "Москва",
"be": "Масква",
"is": "Moskva",
"eo": "Moskvo",
"cu": "Москъва",
"uk": "Москва",
"cs": "Moskva",
"sk": "Moskva",
"vo": "Moskva",
"ab": "Москва",
"qu": "Moskwa",
"kg": "Moskva",
"cy": "Moscfa",
"az": "Moskva",
"pl": "Moskwa",
"jv": "Moskwa",
"fo": "Moskva",
"ia": "Moscova",
"lg": "Moosko",
"hu": "Moszkva",
"vi": "Mát-xcơ-va",
"eu": "Mosku",
"mt": "Moska",
"nb": "Moskva",
"pt": "Moscou",
"tl": "Moscow",
"ar": "موسكو",
"he": "מוסקווה",
"gv": "Moscow",
"sw": "Moscow",
"de": "Moskau",
"gd": "Moscobha",
"ja": "モスクワ",
"yo": "Mọsko",
"dv": "މޮސްކޯ",
"cv": "Мускав",
"bi": "Moskow",
"fa": "مسکو",
"gn": "Mosku",
"no": "Moskva",
"iu": "ᒨᔅᑯ",
"na": "Moscow",
"bs": "Moskva",
"kk": "Мәскеу",
"ml": "മോസ്കോ",
"zu": "IMoskwa",
"za": "Moscow",
"ca": "Moscou",
"hi": "मास्को",
"ln": "Moskú",
"la": "Moscua",
"mi": "Mohikau",
"av": "Москва",
"gl": "Moscova - Москва",
"es": "Moscú",
"os": "Мæскуы",
"af": "Moskou",
"se": "Moskva",
"ht": "Moskou",
"nl": "Moskou",
"kn": "ಮಾಸ್ಕೋ",
"yi": "מאסקווע",
"ty": "Moscou",
"br": "Moskov",
"el": "Μόσχα",
"ce": "Москох",
"tt": "Мәскәү",
"tr": "Moskova",
"ku": "Moskow",
"te": "మాస్కో",
"ie": "Moskwa",
"sq": "Moska",
"et": "Moskva",
"ss": "Moscow",
"hy": "Մոսկվա",
"tg": "Маскав",
"kl": "Moskva",
"kw": "Moskva",
"mr": "मॉस्को",
"ka": "მოსკოვი",
"ky": "Москва",
"fr": "Moscou",
"my": "မော်စကိုမြို့",
"sm": "Moscow",
"fy": "Moskou",
"mk": "Москва",
"ms": "Moscow",
"th": "มอสโก",
"ug": "Moskwa",
"ur": "ماسکو",
"bn": "মস্কো"
},
"lat": 55.7504461,
"lon": 37.6174943,
"country": "RU",
"state": "Moscow"
}
]
但作为回应,我得到了这样的例外
Exception in thread "main" org.springframework.web.client.RestClientException: Error while extracting response for type [class com.romanperkov.spring.rest.entity.cord] and content type [application/json;charset=utf-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `com.romanperkov.spring.rest.entity.cord` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.romanperkov.spring.rest.entity.cord` out of START_ARRAY token
at [Source: (PushbackInputStream); line: 1, column: 1]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:120)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:741)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:674)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:315)
at com.romanperkov.spring.rest.Communication.test(Communication.java:81)
at com.romanperkov.spring.rest.App.main(App.java:35)
Caused by: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `com.romanperkov.spring.rest.entity.cord` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.romanperkov.spring.rest.entity.cord` out of START_ARRAY token
at [Source: (PushbackInputStream); line: 1, column: 1]
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:284)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:242)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:105)
... 5 more
Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `com.romanperkov.spring.rest.entity.cord` out of START_ARRAY token
at [Source: (PushbackInputStream); line: 1, column: 1]
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1468)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1242)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1190)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeFromArray(BeanDeserializer.java:604)
at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:190)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:166)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4526)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3521)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:273)
... 7 more
Process finished with exit code 1
我了解问题是apishka的响应以方括号开头,即它返回一个数组,但它没有名称,这种情况下如何进行?
描述要从响应 API 获取的实体的类
@Getter
@Setter
@NoArgsConstructor
public class cord {
private List<String> name;
}
问题的解决方法是将类指定为数组
String url="http://api.openweathermap.org/geo/1.0/direct?q=QWERTY&limit=5&appid=fe7e6bcec3fedc63d4a158abd1c3c3eb";
cord[] responseEntity=restTemplate.getForObject
(url.replace("QWERTY","москва"), cord[].class);
System.out.println(responseEntity[0]);
使用 Spring Boot 编写了一个电报机器人
一切都在 Intellij IDEA 中运行,但是当我尝试运行 JAR 文件时,它一直在抛出
Exception in thread "main"
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name .
这已经是我最终解决的第三个问题了。我整个头都碎了,一定有办法不用手鼓跳舞就能跑 JAR,
请帮忙。
例子
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.2)
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resourceHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No ServletContext set
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:638)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1352)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1195)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:308)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295)
at com.example.demo.UltraGangstaMegaBotApplication.main(UltraGangstaMegaBotApplication.java:21)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No ServletContext set
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653)
... 18 more
Caused by: java.lang.IllegalStateException: No ServletContext set
at org.springframework.util.Assert.state(Assert.java:76)
at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.resourceHandlerMapping(WebMvcConfigurationSupport.java:591)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
... 19 more
I:\Users\El_Chupa\Desktop\JAVA\JAVA Projects\gradleproject2\UltraGangstaMegaBot\out\artifacts\UltraGangstaMegaBot_jar>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>UltraGangstaMegaBot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--Библиотека для парсинга эмоджи-->
<dependency>
<groupId>com.vdurmont</groupId>
<artifactId>emoji-java</artifactId>
<version>5.1.1</version>
</dependency>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId>
<version>6.1.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.13</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
我正在编写代码,其中不同的方法适用于一个静态集合。我开始写,然后 NullPointerException 开始向我飞来,起初我以为我在某个地方搞砸了,然后我在一个干净的项目中为每个人检查它。创建了一个类,该类有一个 List list 类型的字段;,然后在我写的主要方法中 list.add("111"); 我得到 NullPointerException。为什么?该字段对于整个类都是静态的。运动忘记了Java,请帮忙。我使用 jdks\azul-11.0.14.1\bin\java.exe
问题:如何将元素添加到静态集合?
import java.util.ArrayList;
import java.util.List;
public class MiniDuckSimulator {
static List<String> point;
public static void main(String[] args) {
point.add("111");
System.out.println(point);
}
}