Tag: spring hateoas

使用MockServletContext进行unit testing

我已经使用Gradle设置了spring boot应用程序。 现在我明白了@EnableAutoConnfiguration根据类路径中的依赖关系来配置应用程序。 我很高兴避免所有的管道,但事情开始发生,我希望不会。 这是我的依赖项: dependencies { compile(‘org.springframework.boot:spring-boot-starter-web:1.2.3.RELEASE’) compile ‘org.springframework.hateoas:spring-hateoas:0.17.0.RELEASE’ compile ‘org.springframework.plugin:spring-plugin-core:1.2.0.RELEASE’ compile ‘org.springframework.boot:spring-boot-starter-data-jpa’ compile ‘com.google.guava:guava:18.0’ compile ‘com.fasterxml.jackson.datatype:jackson-datatype-jsr310’ compile ‘commons-beanutils:commons-beanutils:1.9.2’ runtime ‘org.hsqldb:hsqldb:2.3.2’ testCompile ‘org.springframework.boot:spring-boot-starter-test’ testCompile ‘com.jayway.jsonpath:json-path:2.0.0’ } 我的应用类: @ComponentScan(“org.home.project”) @SpringBootApplication //@EnableHypermediaSupport(type = EnableHypermediaSupport.HypermediaType.HAL) public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } UserController的一个片段: @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) public […]

Spring HATEOAS Resource Assembler和Resource Links包含许多变量

我正在使用Spring HATEOAS和Spring堆栈来处理REST API,并且我在链接到资源方面遇到了一些问题。 这是我的代码: 控制者: @RestController @RequestMapping(“/apporteurs/{idInt}/ribs”) public class RibController { @Autowired private RibResourceAssembler ribResourceAssembler; @Autowired private RibRepository ribRepository; /** * Methode GET permettant d’obtenir un Rib par son ID * * @param idRib ID du Rib * @return RibResource */ @RequestMapping(value = “/{idRib}”, method = RequestMethod.GET) @ResponseBody public RibResource getRibById(@PathVariable Long idInt, @PathVariable Long […]

消耗Spring Hateoas Pageable

我有一个使用HAteoas的Rest-Service,以前没有分页的工作。 现在我正在制作可分页的Json。 我用Spring-Hateoas的开箱即用function做到了这一点。 但现在我正在坚持消耗它,我想它确实没有很好的记录,如果是的话。 我的JSON如下所示: { “_embedded”: { “vertragResourceList”: [ { “identifier”: 728, “auszubildender”: “Rumm”, “beruf”: “Landwirt/in”, “betrieb”: “Mitterbauer Johann”, “betriebsNummer”: “e12d0949-67ae-4134-9dc2-fb67758b6b16”, “zustaendigeStelle”: “Irgendwo”, “beginn”: 529887600000, “status”: “RECENT”, “fachrichtung”: null, “schwerpunkt”: “Grünland oder Ackergras”, “ende”: 623113200000, “_links”: { “self”: { “href”: “http://localhost:8080/bbsng-app-rest/vertrag/728” } } }, { “identifier”: 803, “auszubildender”: “Gossen”, “beruf”: “Landwirt/in”, “betrieb”: “Beer Johann”, […]

基本路径未出现在ResourceProcessor自定义链接中

在Spring Data REST中,我使用ResourceProcessor创建自定义链接: @Component public class ServiceInstanceProcessor implements ResourceProcessor<Resource> { @Override public Resource process(Resource resource) { Long id = resource.getContent().getId(); ServiceInstanceController controller = methodOn(ServiceInstanceController.class); resource.add(linkTo(controller.getNodeSummary(id)) .withRel(“nodeSummary”)); resource.add(linkTo(controller.getHealthBreakdown(id)) .withRel(“healthBreakdown”)); resource.add(linkTo(controller.getRotationBreakdown(id)) .withRel(“rotationBreakdown”)); return resource; } } 但是,生成的链接不包括基本路径,即使我已将控制器标记为@BasePathAwareController ,即使默认链接包含基本路径: { … “_links” : { “self” : { “href” : “http://localhost:8080/api/serviceInstances/101” }, “serviceInstance” : { “href” : “http://localhost:8080/api/serviceInstances/101{?projection}”, “templated” […]

Spring Data Rest / Spring Hateoas自定义控制器 – PersistentEntityResourceAssembler

我正在尝试从RepositoryRestResource向自动生成的端点添加一些额外的业务逻辑。 请参阅以下代码: 资源: @RepositoryRestResource(collectionResourceRel=”event”, path=”event”) public interface EventRepository extends PagingAndSortingRepository { } 控制器: @RepositoryRestController @RequestMapping(value = “/event”) public class EventController { @Autowired private EventRepository eventRepository; @Autowired private PagedResourcesAssembler pagedResourcesAssembler; @RequestMapping(method = RequestMethod.GET, value = “”) @ResponseBody public PagedResources getEvents(Pageable pageable, PersistentEntityResourceAssembler persistentEntityResourceAssembler) { Page events = eventRepository.findAll(pageable); return pagedResourcesAssembler.toResource(events, persistentEntityResourceAssembler); } } 我查看了以下两篇stackoverflow文章: 我可以为自定义控制器镜像生成Spring-Data-Rest / […]

Spring HATEOAS ControllerLinkBuilder方法显着增加响应时间

设置:所以我有一个用java编写的RESTfull API,使用spring-boot和spring-hates hates添加资源链接(超媒体驱动的RESTful Web服务)。 我拥有的一切都是标准的,没有进行任何额外的设置或更改 问题 案例: 没有关于资源的链接 – Chrome TTFB avg。 (10次运行)1000个项目400毫秒 案例:资源上的1个自我参考链接 – Chrome TTFB avg。 (10次运行)1000个项目1500毫秒 我正在使用这个官方指南 这个问题 为什么只添加1个链接到我的资源,为处理请求增加了1秒。 每个资源需要大约5-7个链接,每个资源都有其他嵌入的链接? 对于9000个项目,每个项目只有1个链接(包括嵌套的项目),我必须等待30秒才能获得响应,并且没有链接~400毫秒。 PS附加代码是无关紧要的,因为我只是添加了教程中的代码,它会显着影响性能。 编辑1 正如我所建议的那样,我将从我的TextItem构造函数中添加示例代码 add(linkTo(methodOn(TestController.class).getTestItems()).withRel(“testLink”)); 编辑2 所以@Mathias Dpunkt提出的以下示例非常完美 private Method method = ReflectionUtils.findMethod(TestController.class, “getOne”, Integer.class); @Override public Resource process(Resource resource) { resource.add(linkTo(method, resource.getContent().getId()).withSelfRel()); return resource; } 新问题 控制器: @RestController @RequestMapping(“items”) @RequiredArgsConstructor(onConstructor = […]

在自定义@RepositoryRestController方法中填充实体链接

我使用Spring-data-rest来为一些JPA实体提供读取API。 对于写入,我需要发出Command对象而不是直接写入DB,因此我使用@RepositoryRestController和各种命令处理方法添加了一个自定义控制器: @RequestMapping(method = RequestMethod.POST) public @ResponseBody MyEntity post(@RequestBody MyEntity entity) { String createdId = commands.sendAndWait(new MyCreateCommand(entity)); return repo.findOne(createdId); } 我希望输出能够像spring-data-rest控制器的任何其他响应一样得到丰富,特别是我希望它将HATEOAS链接添加到它自身及其关系中。

使用spring boot在json输出中的日期格式

我正在使用spring boot来创建REST应用程序。 我有一个DTO,如下所示: public class Subject { private String uid; private String number; private String initials; private Date dateOfBirth; 我使用Spring-Hateos,我的控制器的reurn类型是ResponseEntity<Resources<Resource>> 。 我需要以“yyyy-mm-dd”格式显示日期。

调用spring数据rest存储库方法不返回链接

我有存储库“ClientRepository”: public interface ClientRepository extends PagingAndSortingRepository { } 当我请求http:// localhost:8080 / clients / 1时,服务器响应 { “algorithmId” : 1, “lastNameTxt” : “***”, “firstNameTxt” : “**”, “middleNameTxt” : “**”, “_links” : { “self” : { “href” : “http://localhost:8080/clients/1121495168” }, “client” : { “href” : “http://localhost:8080/clients/1121495168” } } } 响应具有预期的链接。 当我在另一个控制器中调用存储库inheritance的方法findOne时 @RestController public class SearchRestController { @Autowired public […]

如何避免Joda对象中的内容字段?

我在文档中使用Joda对象(DateTime和DateTimeZone),每当我通过REST接口访问它时,我会得到包含这样的字段的条目 lastAggregationDate: { content: “2016-07-12T17:58:43.643Z” } 代替 lastAggregationDate: “2016-07-12T17:58:43.643Z” 我宣布了Joda Jackson依赖项,我看到这些类型的de / serializers,所以我很困惑这里有什么工作。 我在一个稍微修改过的Spring示例项目中重复了这种行为,但是使用了Java的本机日期类型而不是Joda的。 我已经将一个出生属性的日期添加到Person对象,并修改了shouldRetrieveEntity测试以查找$.dateOfBirth.content 。 我已经确认正在使用序列化程序,看起来LocalDate对象被视为资源而不是简单属性。