На данный момент это мой код.
UserController.java
Код: Выделить всё
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.List;
@RestController
@RequestMapping("/api/users")
public class UserController {
@Autowired
private UserService userService;
@GetMapping
public List getAllUsers() throws IOException {
return userService.findAll();
}
}
Код: Выделить всё
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Service
public class UserService {
private static final String FILE_PATH = "C:\\Users\\test\\Users.json";
private final ObjectMapper objectMapper = new ObjectMapper();
private List getUsers() throws IOException {
File file = new File(FILE_PATH);
if (file.exists()) {
return objectMapper.readValue(file, objectMapper.getTypeFactory().constructCollectionType(List.class, User.class));
}
return new ArrayList();
}
public List findAll() throws IOException {
return getUsers();
}
}
Код: Выделить всё
[
{"user_id": 9,
"username": "user9",
"password": xxxx,
"name": "abcd",
"college": "test",
"is_edited": false,
"is_deleted": false,
"user_type_id": 1
},
{
"user_id": 10,
"username": "user10",
"password": xxxx,
"name": "abcd",
"college": "test",
"is_edited": false,
"is_deleted": false,
"user_type_id": 2
}
]
Код: Выделить всё
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.util.ArrayList` from Object value (token `JsonToken.START_OBJECT`)
at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 1]
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59) ~[jackson-databind-2.17.2.jar:2.17.2]
at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1767) ~[jackson-databind-2.17.2.jar:2.17.2]
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1541) ~[jackson-databind-2.17.2.jar:2.17.2]
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1488) ~[jackson-databind-2.17.2.jar:2.17.2]
at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.handleNonArray(CollectionDeserializer.java:402) ~[jackson-databind-2.17.2.jar:2.17.2]
Подробнее здесь: https://stackoverflow.com/questions/791 ... ng-to-read