1 回答
TA贡献1863条经验 获得超2个赞
请尝试以下操作。
负载 Bean 类:
class Bean {
Year year;
Dosage dosage;
//getters and setters
}
服务类:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
StringBuilder json = new StringBuilder();
String str;
while ((str = request.getReader().readLine()) != null) {
json.append(str);
}
// Using Jackson
ObjectMapper objectMapper = new ObjectMapper();
Bean bean = objectMapper.readValue(json.toString(), Bean.class);
System.out.println("Year ::" + bean.getYear());
System.out.println("Dosage ::" + bean.getDosage());
}
如果是 Maven 项目,则为 pom.xml 文件,否则相应地添加 jars:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
PS:忽略上面代码中的异常处理
添加回答
举报