开发过程中遇到的java问题和解决

  • Can not deserialize instance of java.lang.Integer out of START_OBJECT token

这个问题出现的原因是@RequestBody修饰的参数对象的数据字段与POST json参数不一致。传递的POST json参数是: { “user”: {“name”: “admin”, “password”: “admin”} }, @RequestBody User的定义如下:

1
2
3
4
5
6
7
8

public class User {
private String name;

private: String password;

...
}

这样POST json参数不能正确解析到@RequestBody User的参数。修改POST json参数格式为: { “name”: “admin”, “password”: “admin” }。问题解决了。

  • @RequestBody gives empty JsonObject when making a POST Request

这个问题出现的原因是@RequestBody参数为单个对象,这是不能正确解析POST json参数的。可以使用包含POST json字段类对象,也可以使用@RequestBody HashMap<String, Object>。POST json参数格式为: { “id”: 1 },修改@RequestBody HashMap<String, Integer>后解决问题。

  • mybatis generator 生成代码时错误提示The specified target project directory src does not exist

出现这个问题的原因是使用mvn package打包是,mybatis generator没有生成文件。修改pom.xml文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<configurationFile>
mybatis-generator/generatorConfig.xml
</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<dependency>
<groupId>com.itfsw</groupId>
<artifactId>mybatis-generator-plugin</artifactId>
<version>1.2.12</version>
</dependency>
</dependencies>
</plugin>

</plugins>

</build>

问题解决了。

  • HttpMessageNotReadableException: Required request body is missing

造成这个问题的原因是因为我在Controller接口方法的参数使用@RequestBody Integer id,但是传递的Integer类型body没有匹配的Integer类。去除方法参数中的@RequestBody解决问题。
参考:stackoverflow - Get integer on requestbody

给世界留下最美好的财富,感谢您支持并鼓励我继续创作!