반응형
스프링: @ResponseBody "ResponseEntity"
를 반환합니다.
컨트롤러에서 json 어레이를 만듭니다.만약 내가 돌아온다면List<JSONObject>
괜찮습니다.
@RequestMapping(value="", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<JSONObject> getAll() {
List<Entity> entityList = entityManager.findAll();
List<JSONObject> entities = new ArrayList<JSONObject>();
for (Entity n : entityList) {
JSONObject entity = new JSONObject();
entity.put("id", n.getId());
entity.put("address", n.getAddress());
entities.add(entity);
}
return entities;
}
JSON 어레이와 HTTP 상태 코드를 반환해야 합니다.
@RequestMapping(value="", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody ResponseEntity<List<JSONObject>> getAll() {
List<Entity> entityList = entityManager.findAll();
List<JSONObject> entities = new ArrayList<JSONObject>();
for (Entity n : entityList) {
JSONObject Entity = new JSONObject();
entity.put("id", n.getId());
entity.put("address", n.getAddress());
entities.add(entity);
}
return new ResponseEntity<JSONObject>(entities, HttpStatus.OK); // XXX
}
이클립스 XXX 행의 이클립스
Multiple markers at this line
- The constructor ResponseEntity<JSONObject>(List<JSONObject>, HttpStatus) is undefined
- Type mismatch: cannot convert from ResponseEntity<JSONObject> to
ResponseEntity<List<JSONObject>>
- Type mismatch: cannot convert from ResponseEntity<JSONObject> to JSONObject
json+http 회신 어떻게 해야 하나요?하나의 json 개체 + http 상태 코드를 반환하기 위한 작업 코드가 있습니다.
@RequestMapping(value="/{address}", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody ResponseEntity<JSONObject> getEntity(@PathVariable("address") int address) {
Entity n = entityManager.findByAddress(address);
JSONObject o = new JSONObject();
o.put("id", n.getId());
o.put("address", n.getAddress());
return new ResponseEntity<JSONObject>(o, HttpStatus.OK);
}
대신
return new ResponseEntity<JSONObject>(entities, HttpStatus.OK);
해라
return new ResponseEntity<List<JSONObject>>(entities, HttpStatus.OK);
이제 나는 돌아간다Object
더 나은 해결책은 모르겠지만 효과가 있어요
@RequestMapping(value="", method=RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody ResponseEntity<Object> getAll() {
List<Entity> entityList = entityManager.findAll();
List<JSONObject> entities = new ArrayList<JSONObject>();
for (Entity n : entityList) {
JSONObject Entity = new JSONObject();
entity.put("id", n.getId());
entity.put("address", n.getAddress());
entities.add(entity);
}
return new ResponseEntity<Object>(entities, HttpStatus.OK);
}
개인적으로 메서드 서명을 다음과 같이 변경하는 것이 좋습니다.
public ResponseEntity<?>
이를 통해 오류 메시지를 서비스의 단일 항목으로 반환할 수 있으며, 정상일 경우 항목 목록을 반환할 수 있습니다.
반품할 때 어떤 유형도 사용하지 않습니다(이 경우 사용되지 않습니다).
return new ResponseEntity<>(entities, HttpStatus.OK);
왜 다른 답이 나에게 효과가 없었는지 모르겠지만(오류 500) 이 방법은 유효합니다.
@GetMapping("")
public String getAll() {
List<Entity> entityList = entityManager.findAll();
List<JSONObject> entities = new ArrayList<JSONObject>();
for (Entity n : entityList) {
JSONObject Entity = new JSONObject();
entity.put("id", n.getId());
entity.put("address", n.getAddress());
entities.add(entity);
}
return entities.toString();
}
나는 이것에 늦었지만 이것과 관련된 해결책을 좀 더 넣고 싶다.
@GetMapping
public ResponseEntity<List<JSONObject>> getRole() {
return ResponseEntity.ok(service.getRole());
}
언급URL : https://stackoverflow.com/questions/26320106/spring-return-responsebody-responseentitylistjsonobject
반응형
'sourcecode' 카테고리의 다른 글
'가 Angular 컴포넌트인 경우 이 모듈의 일부인지 확인합니다. (0) | 2023.02.15 |
---|---|
스프링 부츠를 사용한 심플한 임베디드 Kafka 테스트 예시 (0) | 2023.02.15 |
TypeScript 정적 클래스 (0) | 2023.02.15 |
2개의 JSON 객체 비교 (0) | 2023.02.15 |
Spring MVC REST 컨트롤러에서 HTTP 헤더 정보에 액세스하려면 어떻게 해야 합니까? (0) | 2023.02.15 |