앱을 Spring Boot로 마이그레이션한 후 Spring Data Rest를 사용하는 동안 @Id의 엔티티 속성이 더 이상 JSON으로 마샬링되지 않음을 확인했습니다.
이 질문은 이 SO 질문과 관련이 있습니다(Spring boot @ResponseBody는 엔티티 ID를 직렬화하지 않습니다).앱을 Spring Boot로 마이그레이션하고 spring-boot-starter-data-rest 종속성을 사용한 후에 내 엔티티 @Id 필드가 결과 JSON에서 더 이상 마샬링되지 않는다는 것을 알게 되었습니다.
이것은 제 요청 매핑이며 디버깅하는 동안 데이터가 반환되기 전에 변경되지 않는 것을 볼 수 있으므로 @Id 속성은 나중에 제거됩니다.
@RequestMapping(method = RequestMethod.GET, produces = {"application/json"})
public PagedResources<Receipt> receipts(Pageable pageable, PagedResourcesAssembler assembler) {
Page<Receipt> receipts = receiptRepository.findByStorerAndCreatedDateGreaterThanEqual("003845", createdStartDate, pageable);
PagedResources<Receipt> pagedResources = assembler.toResource(receipts, receiptResourceAssembler);
return pagedResources;
}
내 앱이 사용자가 그 값으로 검색할 수 있도록 허용하기 때문에 결과 JSON에 @Id 필드를 유지할 수 있는 설정이 있습니까?
감사합니다 :)
기본적으로 Spring Data Rest는 ID를 뱉지 않습니다.그러나 exposureIdsFor(...) 메서드를 통해 선택적으로 활성화할 수 있습니다.구성에서 이 작업을 수행할 수 있습니다.
@Configuration
public static class RepositoryConfig extends
RepositoryRestMvcConfiguration {
@Override
protected void configureRepositoryRestConfiguration(
RepositoryRestConfiguration config) {
config.exposeIdsFor(Class1.class, Class2.class);
}
}
Spring Data Rest 2.4 기준(Spring-boot 1.3.0을 사용하는 경우 과도 종속성).M5) RepositoryRestConfigurerAdapter를 사용할 수 있습니다.예를 들어.
@Configuration
class SpringDataRestConfig {
@Bean
public RepositoryRestConfigurer repositoryRestConfigurer() {
return new RepositoryRestConfigurerAdapter() {
@Override
public void configureRepositoryRestConfiguration(
RepositoryRestConfiguration config) {
config.exposeIdsFor(Class1.class, Class2.class);
}
}
}
}
노출 전Id토론 내용을 읽어 보십시오: https://github.com/spring-projects/spring-hateoas/issues/66
REST에서 리소스의 ID는 해당 URI입니다.클라이언트는 id를 명시적으로 사용하여 url을 구축하지 않습니다.예를 들어 UUID의 ID를 바꿀 수 있습니다.또는 URL 체계를 변경하기도 합니다.
@getter, @setters를 넣으면 json 결과에 노출될 것입니다, 그것이 당신에게 도움이 되기를 바랍니다.
@모델 클래스의 ID 주석이 마법을 부립니다.
public class Location {
@Id
private String woeid;
private String locationName;
그러면 mongo 개체는 다음과 같이 나타납니다.
{
"_id" : "2487889",
"_class" : "com.agilisys.weatherdashboard.Location",
"locationName" : "San Diego, CA"
}
언급URL : https://stackoverflow.com/questions/24936636/while-using-spring-data-rest-after-migrating-an-app-to-spring-boot-i-have-obser
'programing' 카테고리의 다른 글
| MariaDB 다시 시작 오류 "variable 'default-character-set = cp932'" (0) | 2023.07.03 |
|---|---|
| Firebase 관리자용 "serviceAccountCredentials.json"은 어디에서 얻을 수 있습니까? (0) | 2023.07.03 |
| Axios 응답 Vuex에서 작업을 호출하는 방법은 무엇입니까? (0) | 2023.07.03 |
| git - 병합 시 특정 커밋 건너뛰기 (0) | 2023.07.03 |
| C 스타일 배열에서 std:: 벡터를 초기화하는 방법은 무엇입니까? (0) | 2023.07.03 |