Spring Hateoas Jsonapi Hinzufügen enthält die Antwort
Posted: 10 Jun 2025, 10:35
Ich arbeite daran, Spring Hateoas JSON API als Ersatz für die Kurbel zu verwenden. In der Dokumentation. Ich konnte die erwartete Antwort erhalten, wenn ich eine Liste von Objekten abzurufen, dh wenn ich < /p>
tat
Ich habe versucht, @JsonAPirelationship zu verwenden, aber diese Beziehung innerhalb des Objekts und nicht die enthaltene Taste
tat
Code: Select all
import static com.toedter.spring.hateoas.jsonapi.MediaTypes.JSON_API_VALUE;
import org.springframework.hateoas.EntityModel;
@RestController
@RequestMapping(value ="/cont, produces = JSON_API_VALUE)
public class MovieController {
@RequestMapping("/getMovieList",produces = JSON_API_VALUE)
public CollectionModel getMovieList(){
var movies = service.getAllMovies();
return CollectionModel.of(movies);
}
}
< /code>
Ich verwende folgende Abhängigkeiten von Hateos und JsonAPI < /p>
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'com.toedter:spring-hateoas-jsonapi:2.1.3'
implementation 'org.springframework.boot:spring-boot-starter-hateoas'
< /code>
Und ich erhalte die erwartete Antwort, dh < /p>
{
"data": [
{
"id": "1",
"type": "movies",
"attributes": {
"title": "Star Wars"
}
},
{
"id": "2",
"type": "movies",
"attributes": {
"title": "Star Wars2"
}
}
]
}
< /code>
Dies ist meine Filmklasse < /p>
public class Movie{
@JsonApiId
public String id;
public String name;
public List directorList;
}
< /code>
Ich muss auch die Taste auch in die obige Filmliste -Antwort aufgenommen haben, dh < /p>
{
"data": [
{
"id": "1",
"type": "movies",
"attributes": {
"title": "Star Wars"
}
},
{
"id": "2",
"type": "movies",
"attributes": {
"title": "Star Wars2"
}
}
],
"included":[
{
"id": "1",
"type": "director",
"attributes": {
"name": "director"
}
}
]
}