使用Spring JPA和REST显示数据

使用Spring JPA和REST显示数据,spring,rest,spring-mvc,spring-data-jpa,spring-rest,Spring,Rest,Spring Mvc,Spring Data Jpa,Spring Rest,在这些教程之后,我成功地制作了一个与JPA相结合的RESTful Spring应用程序。目前,我的驱动程序存储库中有3个驱动程序 我的问题是,当我使用localhost:8080/driver时,它会说: { "_embedded" : { "driver" : [ { "_links" : { "self" : { "href" : "http://localhost:8080/driver/1" }, "driver" : { "hre

在这些教程之后,我成功地制作了一个与JPA相结合的RESTful Spring应用程序。目前,我的驱动程序存储库中有3个驱动程序

我的问题是,当我使用localhost:8080/driver时,它会说:

{
  "_embedded" : {
"driver" : [ {
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/driver/1"
    },
    "driver" : {
      "href" : "http://localhost:8080/driver/1"
    }
  }
}, {
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/driver/2"
    },
    "driver" : {
      "href" : "http://localhost:8080/driver/2"
    }
  }
}, {
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/driver/3"
    },
    "driver" : {
      "href" : "http://localhost:8080/driver/3"
    }
  }
} ]
  },
  "_links" : {
    "self" : {
  "href" : "http://localhost:8080/driver"
   },
    "profile" : {
  "href" : "http://localhost:8080/profile/driver"
   },
   "search" : {
    "href" : "http://localhost:8080/driver/search"
}
  },
 "page" : {
"size" : 20,
"totalElements" : 3,
"totalPages" : 1,
"number" : 0
  }
}
{
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/driver/1"
    },
    "driver" : {
      "href" : "http://localhost:8080/driver/1"
    }
  }
}
当我进入一个特定的驱动程序页面,比如localhost:8080/driver/1,它会说:

{
  "_embedded" : {
"driver" : [ {
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/driver/1"
    },
    "driver" : {
      "href" : "http://localhost:8080/driver/1"
    }
  }
}, {
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/driver/2"
    },
    "driver" : {
      "href" : "http://localhost:8080/driver/2"
    }
  }
}, {
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/driver/3"
    },
    "driver" : {
      "href" : "http://localhost:8080/driver/3"
    }
  }
} ]
  },
  "_links" : {
    "self" : {
  "href" : "http://localhost:8080/driver"
   },
    "profile" : {
  "href" : "http://localhost:8080/profile/driver"
   },
   "search" : {
    "href" : "http://localhost:8080/driver/search"
}
  },
 "page" : {
"size" : 20,
"totalElements" : 3,
"totalPages" : 1,
"number" : 0
  }
}
{
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/driver/1"
    },
    "driver" : {
      "href" : "http://localhost:8080/driver/1"
    }
  }
}
在我的驱动程序实体类中,我有诸如firstName、lastName、phone之类的字段。我的问题是:有没有一种方法可以在上面显示localhost:8080/driver或localhost:8080/driver/1?因此,它看起来与此类似:

 {
  "firstName" : "Frodo",
  "lastName" : "Baggins",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/people/1"
    }
   }
}
我知道这些字段被正确地保存在数据库中,因为我可以成功地搜索它们,但除了使用curl发布外,我还没有找到如何显示它们的示例

提前感谢任何能提供帮助的人

编辑:这是我的入门课程:

@SpringBootApplication
 public class StartServer implements    ApplicationListener<ContextRefreshedEvent> {


private static final Logger log = LoggerFactory.getLogger(StartServer.class);


public static void main(String[] args) {
    SpringApplication.run(StartServer.class, args);

}

@Override
public void onApplicationEvent(final ContextRefreshedEvent event) {

}

@Bean
public CommandLineRunner demo(DriverRepository repository) {
    return (args) -> {
        repository.save(new Driver("Jack", "Bauer"));
        repository.save(new Driver("JackY", "Aasd"));
        repository.save(new Driver("JackMe", "Commou"));

        // fetch all customers
        log.info("Drivers found with findAll():");
        log.info("-------------------------------");
        for (Driver driver : repository.findAll()) {
            log.info(driver.toString());
        }
        log.info("");

        Driver driver = repository.findOne(1L);
        log.info("Customer found with findOne(1L):");
        log.info("--------------------------------");
        log.info(driver.toString());
        log.info("");

        // fetch customers by last name
        log.info("Driver found with findByLastName('Bauer'):");
        log.info("--------------------------------------------");
        for (Driver bauer : repository.findByLastName("Bauer")) {
            log.info(bauer.toString());
        }
        log.info("");
    };
}



}

您没有属性,只有字段。为要公开的字段添加一个getter/setter。@M.Deinum谢谢!我完全忘了这些。