我在解析来自 Spring RepositoryRestResource 的 HATEOAS 响应时遇到了一些麻烦。诚然,我对其中一些库有点陌生。
我的存储库正在工作并产生如下良好响应:
{
_embedded: {
tracks: [
{
trackName: "Oreville Kart Club",
latitude: "40.5212757",
longitude: "-75.6920605",
address1: "532 Old Topton Rd",
address2: "",
city: "Mertztown",
state: "PA",
zip: "19539-0000",
phoneNumber: "123-456-7890",
administratorId: "1",
locale: null,
_links: {
self: {
href: "http://localhost:10100/track-service/tracks/1"
},
track: {
href: "http://localhost:10100/track-service/tracks/1"
}
}
},
{
trackName: "Oakland Valley Race Park(OVRP)",
latitude: "41.474944",
longitude: "-74.616462",
address1: "305 Oakland Valley Rd",
address2: "",
city: "Cuddebackville",
state: "NY",
zip: "12729-0000",
phoneNumber: "976-432-1234",
administratorId: "2",
locale: null,
_links: {
self: {
href: "http://localhost:10100/track-service/tracks/2"
},
track: {
href: "http://localhost:10100/track-service/tracks/2"
}
}
}
]
},
_links: {
self: {
href: "http://localhost:10100/track-service/tracks"
},
profile: {
href: "http://localhost:10100/track-service/profile/tracks"
},
search: {
href: "http://localhost:10100/track-service/tracks/search"
}
},
page: {
size: 20,
totalElements: 2,
totalPages: 1,
number: 0
}
}
现在这是我的代码,我认为这是正确的。我已尽力遵循我在网上找到的一些示例。
@RestController
@EnableZuulProxy
@RequestMapping("/api")
public class ApiGatewayController {
@LoadBalanced
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
@Autowired
RestTemplate restTemplate;
@RequestMapping(method = RequestMethod.GET, value = "/tracks")
public Collection<Pair<Long, String>> listTracks() {
ParameterizedTypeReference<Resources<Track>> ptr = new ParameterizedTypeReference<Resources<Track>>() { } ;
ResponseEntity<Resources<Track>> entity = restTemplate.exchange("http://track-service/tracks", HttpMethod.GET, null, ptr);
return entity.getBody()
.getContent()
.stream()
.map(t -> Pair.of(t.getTrackId(), t.getTrackName()))
.collect(Collectors.toList());
}
class Track {
private Long trackId;
private String trackName;
public Long getTrackId() {
return trackId;
}
public void setTrackId(Long trackId) {
this.trackId = trackId;
}
public String getTrackName() {
return trackName;
}
public void setTrackName(String trackName) {
this.trackName = trackName;
}
}
}
奇怪的是我收到以下错误,我只是不知道为什么。杰克逊似乎无法解析响应,但它是标准的 HATEOS HAL 响应
Could not read document: Can not construct instance of com.apex.microservice.ApiGatewayController$Track: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?) at [Source: java.io.PushbackInputStream@27f652c; line: 4, column: 7] (through reference chain: org.springframework.hateoas.Resources["_embedded"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.apex.microservice.ApiGatewayController$Track: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?) at [Source: java.io.PushbackInputStream@27f652c; line: 4, column: 7] (through reference chain: org.springframework.hateoas.Resources["_embedded"])
我不确定这是类型擦除问题,还是构造函数的问题。我相信我的 ParameterizedTypeRequest 是正确的,我不确定这里需要什么构造函数。我尝试了一些变化无济于事。如果有人能在这里启发我,我将不胜感激。