[英]restAPI json array in Object extract
I am getting an error: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.util.ArrayList<com.hatach.backend.models.我收到一个错误: com.fasterxml.jackson.databind.exc.MismatchedInputException:无法反序列化类型的值`java.util.ArrayList<com.hatach.backend.models。
Probably because my data type during ObjectMapper in Main or ArrayList<> in Entity is wrong... I don't know how to go about fixing it because I don't seem to understand where the error is specifically, or if I should go about it a different way (ie: not do business logic in main and create a serviceImpl and and maybe a dto package.可能是因为我在 Main 中的ObjectMapper或 Entity 中的ArrayList<>期间的数据类型错误...我不知道如何 go 修复它,因为我似乎不明白错误具体在哪里,或者我是否应该 go关于它的不同方式(即:不在 main 中执行业务逻辑并创建一个 serviceImpl ,也许还有一个 dto package。
I guess this question is 2 folds: (1) how to fix this error.我想这个问题有两个方面:(1)如何解决这个错误。 (2) what's the good standardized best way to go about stamping json data in restAPIs, which we can then sort by messing with the url. (2)关于在restAPIs中标记json数据的go的良好标准化最佳方法是什么,然后我们可以通过弄乱url对其进行排序。 ie localhost:8080/recipes?name=scrambledEggs即 localhost:8080/recipes?name=scrambledEggs
1) JSON File 1) JSON 文件
"recipes": [
{
"name": "scrambledEggs",
"ingredients": [
"1 tsp oil",
"2 eggs",
"salt"
],
"instructions": [
"Beat eggs with salt",
"Heat oil in pan",
"Add eggs to pan when hot",
"Gather eggs into curds, remove when cooked",
"Salt to taste and enjoy"
]
},
{
"name": "garlicPasta",
"ingredients": [
"500mL water",
"100g spaghetti",
"25mL olive oil",
"4 cloves garlic",
"Salt"
],
"instructions": [
"Heat garlic in olive oil",
"Boil water in pot",
"Add pasta to boiling water",
"Remove pasta from water and mix with garlic olive oil",
"Salt to taste and enjoy"
]
2) Entity 2) 实体
public class RecipesInfo {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private ArrayList<RecipesFile> recipes;
//get + set
public class RecipesFile {
private String name;
private ArrayList<String> ingredients;
private ArrayList<String> instructions;
//get + set
3) Main file 3) 主文件
ublic class ApiApplication {
public static void main(String[] args) {
SpringApplication.run(ApiApplication.class, args);
}
@Bean
CommandLineRunner runner(RecipesService recipesService) {
return args -> {
ObjectMapper mapper = new ObjectMapper();
TypeReference<ArrayList<RecipesInfo>> typeReference = new TypeReference<ArrayList<RecipesInfo>>() {};
InputStream inputStream =TypeReference.class.getResourceAsStream("/json/data.json");
try {
ArrayList<RecipesInfo> recipes = mapper.readValue(inputStream, typeReference);
recipesService.save(recipes);
System.out.println("data saved: recipes");
} catch (IOException e) {
System.out.println("unable to save data: recipes");
System.out.println(e);
}
};
A JSON should contain an array or a JSON object. JSON 应该包含一个数组或一个 JSON object。 In your case, based on your entity, you should add curly brackets around your recipes
array在您的情况下,根据您的实体,您应该在recipes
数组周围添加大括号
{
"recipes": [
{
"name": "scrambledEggs",
"ingredients": [
"1 tsp oil",
"2 eggs",
"salt"
],
"instructions": [
"Beat eggs with salt",
"Heat oil in pan",
"Add eggs to pan when hot",
"Gather eggs into curds, remove when cooked",
"Salt to taste and enjoy"
]
},
{
"name": "garlicPasta",
"ingredients": [
"500mL water",
"100g spaghetti",
"25mL olive oil",
"4 cloves garlic",
"Salt"
],
"instructions": [
"Heat garlic in olive oil",
"Boil water in pot",
"Add pasta to boiling water",
"Remove pasta from water and mix with garlic olive oil",
"Salt to taste and enjoy"
]
]
}
After that you can simply deserialize a single RecipesInfo
instead of its collection之后,您可以简单地反序列化单个RecipesInfo
而不是它的集合
RecipesInfo recipes = mapper.readValue(inputStream, RecipesInfo.class);
Also, I noticed your inner RecipesFile
class is not static
.另外,我注意到您的内部RecipesFile
class 不是static
。 Object Mapper won't be able to create an instance of RecipesFile
class without that Object 映射器将无法创建RecipesFile
class 的实例
public class RecipesInfo {
...
public static class RecipesFile {
private String name;
private ArrayList<String> ingredients;
private ArrayList<String> instructions;
//get + set
...
Regarding the second question, you can check the following Stackoverflow article .关于第二个问题,可以查看下面的Stackoverflow 文章。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.