Spring REST api返回未知的json格式

时间:2017-01-24 14:23:29

标签: spring-boot spring-restcontroller

我的API开始返回这样的JSON:

{
  "links": [
    {
      "rel": "self",
      "href": "http://127.0.0.1:8888/books/4/instances"
    }
  ],
  "content": [
    {
      "donatorName": "Lee Anderson",
      "content": [],
      "links": [
        {
          "rel": "self",
          "href": "http://127.0.0.1:8888/book-instances/4"
        },
        {
          "rel": "bookInstance",
          "href": "http://127.0.0.1:8888/book-instances/4"
        },
        {
          "rel": "reservations",
          "href": "http://127.0.0.1:8888/book-instances/4/reservations"
        },
        {
          "rel": "library",
          "href": "http://127.0.0.1:8888/book-instances/4/library"
        },
        {
          "rel": "book",
          "href": "http://127.0.0.1:8888/book-instances/4/book"
        }
      ]
    },
    {
      "donatorName": "Leroy Guerrero",
      "content": [],
      "links": [
        {
          "rel": "self",
          "href": "http://127.0.0.1:8888/book-instances/5"
        },
        {
          "rel": "bookInstance",
          "href": "http://127.0.0.1:8888/book-instances/5"
        },
        {
          "rel": "reservations",
          "href": "http://127.0.0.1:8888/book-instances/5/reservations"
        },
        {
          "rel": "library",
          "href": "http://127.0.0.1:8888/book-instances/5/library"
        },
        {
          "rel": "book",
          "href": "http://127.0.0.1:8888/book-instances/5/book"
        }
      ]
    }
  ]
}

这不是hal + json,我找不到使用“content”和“links”(没有下划线)字段的约定。我究竟做错了什么?我想要“_links”和“embedded”。

我的pom.xml是:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo2</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

我的存储库是简单的PagingAndSortingRepository类。

package demo;

import java.util.List;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Page;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
//import org.springframework.hateoas.PagedResources;

@RepositoryRestResource(collectionResourceRel = "libraries", path = "libraries")
public interface LibraryRepository extends PagingAndSortingRepository<Library, Long> {

}

我错过了什么?或者包括超过我需要的东西?

1 个答案:

答案 0 :(得分:0)

好的,因为我添加了&#34; spring.data.rest.defaultMediaType = application / xml&#34;到application.properties。一定要爱上魔法。