[英]Java Spring Boots Application Not able to extract data form json file
I created rest api in spring boot including Gradle support.我在 spring 引导中创建了 rest api,包括 Z7B5CC4FB56E11DC7520F716EC1736 支持。 The jdk version is 17. I am trying to extract the data from jason file by accessing rest methods such as GET. jdk 版本是 17。我正在尝试通过访问 rest 方法(例如 GET)从 jason 文件中提取数据。 I can see the application is running in localhost on port number 8080 by using tomcat server but the problem is when I issue the Get request with the parameters it showing following error.通过使用 tomcat 服务器,我可以看到应用程序在 localhost 的端口号 8080 上运行,但问题是当我使用显示以下错误的参数发出 Get 请求时。
{"error":"not-found-001","message":"1 not found"} {"error":"not-found-001","message":"1 not found"}
The request URL is: http://localhost:8080/result/1请求URL为:http://localhost:8080/result/1
Here is the json file code.这是 json 文件代码。
{
"id": 2,
"name": "Aberconwy",
"seqNo": 1,
"partyResults": [
{
"party": "LAB",
"votes": 8994,
"share": 33.00
},
{
"party": "CON",
"votes": 7924,
"share": 29.10
},
{
"party": "LD",
"votes": 5197,
"share": 19.10
},
{
"party": "PC",
"votes": 3818,
"share": 14.00
},
{
"party": "OTH",
"votes": 517,
"share": 1.90
},
{
"party": "GRN",
"votes": 512,
"share": 1.90
},
{
"party": "UKIP",
"votes": 296,
"share": 1.10
}
]
}
Here is the Interface.这里是界面。
public interface ResultService {
ConstituencyResult GetResult(Integer id);
void NewResult(ConstituencyResult result);
Map<Integer,ConstituencyResult> GetAll();
void reset();
}
Here is the code in controller.这是 controller 中的代码。
@RestController
public class ResultsController {
private final ResultService results;
public ResultsController(ResultService resultService) {
this.results = resultService;
}
@GetMapping("/result/{id}")
ConstituencyResult getResult(@PathVariable Integer id) {
ConstituencyResult result = results.GetResult(id);
if (result == null) {
throw new ResultNotFoundException(id);
}
return results.GetResult(id);
}
@PostMapping("/result")
ResponseEntity<String> newResult(@RequestBody ConstituencyResult result) {
if (result.getId() != null) {
results.NewResult(result);
return ResponseEntity.created(URI.create("/result/"+result.getId())).build();
}
return ResponseEntity.badRequest().body("Id was null");
}
@GetMapping("/scoreboard")
Scoreboard getScoreboard() {
return new Scoreboard();
}
}
Implementation of service.实施服务。
@Component
public class MapBasedRepository implements ResultService {
private final Map<Integer,ConstituencyResult> results;
public MapBasedRepository() {
results = new ConcurrentHashMap<>();
}
@Override
public ConstituencyResult GetResult(Integer id) {
return results.get(id);
}
@Override
public void NewResult(ConstituencyResult result) {
results.put(result.getId(), result);
}
@Override
public Map<Integer, ConstituencyResult> GetAll() {
return results;
}
@Override
public void reset() {
results.clear();
}
}
Here is the test case.这是测试用例。
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class ElectionsApiApplicationIntegrationTests {
@LocalServerPort
private int port;
private URL base;
@Autowired
private ObjectMapper objectMapper;
@Autowired
private TestRestTemplate template;
@Autowired
private ResultService resultService;
@BeforeEach
public void setUp() throws Exception {
this.base = new URL("http://localhost:" + port + "/");
}
@AfterEach
public void reset() throws Exception {
resultService.reset();
}
@Test
public void first5Test() throws Exception {
Scoreboard scoreboard = runTest(5);
assertNotNull(scoreboard);
System.out.println(1);
// assert LD == 1
// assert LAB = 4
// assert winner = noone
}
@Test
public void first100Test() throws Exception {
Scoreboard scoreboard = runTest(100);
assertNotNull(scoreboard);
// assert LD == 12
// assert LAB == 56
// assert CON == 31
// assert winner = noone
}
@Test
public void first554Test() throws Exception {
Scoreboard scoreboard = runTest(554);
assertNotNull(scoreboard);
// assert LD == 52
// assert LAB = 325
// assert CON = 167
// assert winner = LAB
}
@Test
public void allTest() throws Exception {
Scoreboard scoreboard = runTest(650);
assertNotNull(scoreboard);
// assert LD == 62
// assert LAB == 349
// assert CON == 210
// assert winner = LAB
// assert sum = 650
}
private Scoreboard runTest(int numberOfResults) throws Exception {
for (int i = 1; i <= numberOfResults; i++ ) {
Class<?> clazz = this.getClass();
InputStream is = clazz.getResourceAsStream(String.format("/sample-election-results/result%s.json",String.format("%03d",i)));
ConstituencyResult cr = objectMapper.readValue(is, ConstituencyResult.class);
template.postForEntity(base.toString()+"/result", cr,String.class);
}
ResponseEntity<Scoreboard> scores = template.getForEntity(base.toString()+"/scoreboard", Scoreboard.class);
return scores.getBody();
}
}
Here is the screenshot of the result.这是结果的屏幕截图。
I don't know how ResultNotFoundException is implemented, but if {"error":"not-found-001","message":"1 not found"} is the message you receive from this Exception, you obviously don't have id = 1 in your json file and this is the reason of the exception我不知道 ResultNotFoundException 是如何实现的,但是如果 {"error":"not-found-001","message":"1 not found"} 是您从该异常中收到的消息,那么您显然没有id = 1 在您的 json 文件中,这是异常的原因
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.