标签: spring-hateoas

如何让Spring MVC + HATEOAS将资源列表编码为HAL?

我有一个Spring HATEOAS Resource这样ModelResource extends Resource<Model>.

@RestController我要创建新的方法ModelS:

@RequestMapping(value = "", method = POST)
public ResponseEntity<ModelResource> postModel() {
    Model model = service.create().model;
    ModelResource resource = assembler.toResource(model);

    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(URI.create(resource.getLink("self").getHref()));

    return new ResponseEntity<>(resource, headers, CREATED);
}
Run Code Online (Sandbox Code Playgroud)

ModelResource从上面的方法返回的创建是HAL编码的:

$ curl -v -XPOST localhost:8080/models
> POST /models HTTP/1.1                                                                                                                                                       
> User-Agent: curl/7.32.0                                                                                                                                                     
> Host: localhost:8080                                                                                                                                                        
> Accept: */*                                                                                                                                                                 
>                                                                                                                                                                             
< HTTP/1.1 201 Created                                                                                                                                                        
< Date: Sun, 25 Jan 2015 11:51:50 GMT                                                                                                                                         
< Location: http://localhost:8080/models/0                                                                                                                                    
< …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc spring-hateoas

8
推荐指数
1
解决办法
6128
查看次数

用于将复杂实体发布到Spring Data REST/HATEOAS服务的Java客户端

据我所知,提供了将复杂对象转换为适当的HAL格式的方法.这当然可以用于编组框架本身中的对象. ResourceLink物体等

对于一个用例的缘故: Company 1是现有的Company在我的系统.我想添加一个Employee适合的新功能Company 1

下面是Employee您从基于Spring Data REST的服务收到的示例对象.Spring HATEOAS还提供了自己构建这些对象的方法.

{
    "id": null,
    "firstName": "bZWthNFk",
    "lastName": "GtTnrqka",
    "loginId": "zTk5rT",
    "active": true,
    "_links": {
        "company": {
            "href": "http://localhost/companies/1";
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,这似乎不适用于POST对象.据我了解,同一个对象必须被POST为:

{
    "id": null,
    "firstName": "bZWthNFk",
    "lastName": "GtTnrqka",
    "loginId": "zTk5rT",
    "active": true,
    "company": "http://localhost/companies/1"
}
Run Code Online (Sandbox Code Playgroud)

据我所知,HATEOAS或Data REST项目无法通过或通过其他方式生成对象以发布到有效的基于HAL的服务RestTemplate.事实上,我找不到任何方法可以在没有手动编组的情况下轻松地发布复杂对象.假设这个我错了吗?

如何构建一个有效的Java SDK进行服务到服务通信,利用HATEOAS原则,而无需使用此工具实际POST对象?


长话短说,我想发布这个对象而不必手工序列化关联的URI.

public class Employee {
    private Integer id;
    @NotNull
    private Company company;
    private String …
Run Code Online (Sandbox Code Playgroud)

java spring spring-data-rest spring-hateoas

8
推荐指数
1
解决办法
1374
查看次数

在Spring Boot应用程序中使用API​​网关时,HATEOAS路径无效

我有两个Spring启动应用程序,其中一个作为API网关(如本文所讨论的Spring示例).连接到第一个的另一个是使用spring-data-rest(spring-data-neo4j-rest)公开配置文件服务.

第一个应用程序从端口8080开始,并使用zuul将请求路由到第二个,如下所示:

zuul:
  routes:
    profiles:
      path: /profiles/**
      url: http://localhost:8083/profiles/  
Run Code Online (Sandbox Code Playgroud)

这一切都运行良好,并从第二个应用程序提供http:// localhost:8080/profiles的请求.但问题是响应中的HATEOAS链接不正确.调用第二个服务的响应是正确的:

{
    "_links": {
        "self": {
            "href": "http://localhost:8083/profiles{?page,size,sort}",
            "templated": true
        },
        "search": {
            "href": "http://localhost:8083/profiles/search"
        }
    },
    "_embedded": {
        "profiles": [
            {
                "name": "Andrew Rutter",
                "_links": {
                    "self": {
                        "href": "http://localhost:8083/profiles/0"
                    }
                }
            },
            {
                "name": "Andrew Rutter",
                "_links": {
                    "self": {
                        "href": "http://localhost:8083/profiles/1"
                    }
                }
            }
        ]
    },
    "page": {
        "size": 20,
        "totalElements": 2,
        "totalPages": 1,
        "number": 0
    }
}
Run Code Online (Sandbox Code Playgroud)

但当这回到我的API网关时,链接正在被重写

{
  "name": "Andrew Rutter", …
Run Code Online (Sandbox Code Playgroud)

spring spring-data-rest spring-hateoas spring-boot spring-cloud

8
推荐指数
1
解决办法
4396
查看次数

Spring DTO-DAO(资源 - 实体)映射在哪个应用层:Controller或Service?

我正在编写一个Spring(4.1.7)Web应用程序,该应用程序公开RESTful服务,并希望使用DTO"资源"对象在Controller和客户端浏览器之间进行通信,而不是公开我的持久性实体.

目前,该应用程序具有以下层:

  • 查看(JSP/JSON)
  • 控制器(一个或多个)
  • DAO(@Service)
  • DAO(@Repository)

我的问题是,我应该将DAO实体映射到DTO资源?我看了一下用一些例子Spring HATEOAS,它们显示Resource延长对象ResourceSupport的映射Controller.这是最好的方法,还是我应该从DAO服务返回资源?

我想补充Link的元素返回的资源(为自己和相关的资源),但看不出Link是否在处理的元素将得到解决Service,而不必知识它Controller和它的@RequestMapping.另一方面,我不知道Controller将映射弄乱是否也是一种好的做法.

java architecture spring spring-mvc spring-hateoas

8
推荐指数
1
解决办法
1万
查看次数

使用ManyToOne引用嵌入的Spring数据序列化

我有一个有趣的问题.我的数据模型如下:

A型:

@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
public class A {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;
}
Run Code Online (Sandbox Code Playgroud)

B型:

@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
public class B {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;
}
Run Code Online (Sandbox Code Playgroud)

嵌入式C:

@Embeddable
@JsonIgnoreProperties(ignoreUnknown = true)
public class C {
  @ManyToOne
  private A a;
  @ManyToOne
  private B b;
}
Run Code Online (Sandbox Code Playgroud)

和D型:

@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
public class D {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;

  @ElementCollection
  @OrderColumn(name = "ORDER_INDEX")
  @CollectionTable(
    name = "d_c_join",
    joinColumns = @JoinColumn(name …
Run Code Online (Sandbox Code Playgroud)

hateoas spring-data spring-data-jpa spring-data-rest spring-hateoas

8
推荐指数
1
解决办法
473
查看次数

使用Spring HATEOAS将枚举作为REST API公开的正确方法是什么(通过Spring Data REST)

我正在尝试将HATEOAS与Spring HATEOAS一起使用,并且需要使用Spring HATEOAS将enums作为REST API公开.

我尝试了三种方法如下:

@RestController
@RequestMapping(path = "/fruits")
public class FruitResourceController {

    @RequestMapping(method = RequestMethod.GET)
    public Fruit[] fruits() {
        return Fruit.values();
    }

    // NOTE: The `produces` attribute is only for browsers.
    @RequestMapping(path = "/with-resource", method = RequestMethod.GET,
            produces = MediaTypes.HAL_JSON_VALUE)
    public Resource<Fruit[]> fruitsWithResource() {
        Resource<Fruit[]> resource = new Resource<Fruit[]>(Fruit.values());
        Link selfLink = linkTo(methodOn(FruitResourceController.class).fruitsWithResource())
                .withSelfRel();
        resource.add(selfLink);
        return resource;
    }

    // NOTE: The `produces` attribute is only for browsers.
    @RequestMapping(path = "/with-resources", method = RequestMethod.GET,
            produces = MediaTypes.HAL_JSON_VALUE)
    public …
Run Code Online (Sandbox Code Playgroud)

rest hateoas spring-data-rest spring-hateoas

8
推荐指数
1
解决办法
971
查看次数

Spring Data Rest PUT vs PATCH LinkableResources

我正在使用Spring Data REST来公开我的实体及其关系.我在两个实体之间有一个OneToOne关系,我正在尝试更新/改变与PUT和PATCH的关系.

我注意到Spring Data REST只允许您更新链接资源 - JPA映射实体(OneToMany,ManyToOne等),它们也是AggregateRoots(有一个存储库) - 通过PATCH并被PUT忽略 .

这可以在LinkedAssociationSkippingAssociationHandler类中看到:

if (associationLinks.isLinkableAssociation(association)) {
  return;
}
Run Code Online (Sandbox Code Playgroud)

为什么是这样?这背后的原因是什么?

是因为设计要求我们将关联视为资源本身,如文档的这一部分所示?我可以通过带有Content-Type text/uri-list的PUT改变关系,但感觉不自然,需要额外的HTTP请求.

spring spring-data spring-data-jpa spring-data-rest spring-hateoas

8
推荐指数
1
解决办法
663
查看次数

关于Spring Flux/Mono响应的HATEOAS

我一直按照指南使用Spring HATEOAS:

https://spring.io/guides/gs/rest-hateoas/#initial

package hello;

import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@RestController
public class GreetingController {

    private static final String TEMPLATE = "Hello, %s!";

    @RequestMapping("/greeting")
    public HttpEntity<Greeting> greeting(@RequestParam(value = "name", required = false, defaultValue = "World") String name) {

        Greeting greeting = new Greeting(String.format(TEMPLATE, name));
        greeting.add(linkTo(methodOn(GreetingController.class).greeting(name)).withSelfRel());

        return new ResponseEntity<Greeting>(greeting, HttpStatus.OK);
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我想使用存储库并输出Flux/Mono响应:

@RestController
class PersonController {

    private final PersonRepository people;

    public PersonController(PersonRepository people) {
        this.people = people;
    }

    @GetMapping("/people")
    Flux<String> …
Run Code Online (Sandbox Code Playgroud)

spring spring-mvc spring-hateoas spring-webflux

8
推荐指数
1
解决办法
1779
查看次数

使用Jackson和Spring Hateoas的Jackson2HalModule反序列化json时的空id属性

我的实体:

public class User {

    private Integer id;
    private String mail;
    private boolean enabled;

    // getters and setters
}
Run Code Online (Sandbox Code Playgroud)

文件test.json(来自REST webservice的响应):

{
 "_embedded" : {
  "users" : [ {
    "id" : 1,
    "mail" : "admin@admin.com",
    "enabled" : true,
    "_links" : {
      "self" : {
        "href" : "http://localhost:8080/api/users/1"
      }
    }
  } ]
 }
}
Run Code Online (Sandbox Code Playgroud)

我的测试班:

public class TestJson {

    private InputStream is;
    private ObjectMapper mapper;

    @Before
    public void before() {
        mapper = new ObjectMapper();
        mapper.registerModule(new Jackson2HalModule());
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

        is = TestJson.class.getResourceAsStream("/test.json"); …
Run Code Online (Sandbox Code Playgroud)

java hal jackson spring-data-rest spring-hateoas

7
推荐指数
1
解决办法
2398
查看次数

Spring中有没有简单的HATEOAS + Pagination实现,无需数据休息?

我已经搜索了几天关于如何实现包含 HATEOAS 链接 + 使用 Spring boot 和 JPA 分页(无 spring 数据休息)的 Spring REST API,如下随机示例:

\n
{\n    "_links": {\n        "first": {\n            "href": "http://localhost:8080/api/albums-list?page=0&size=2&sort=title,desc"\n        },\n        "prev": {\n            "href": "http://localhost:8080/api/albums-list?page=0&size=2&sort=title,desc"\n        },\n        "self": {\n            "href": "http://localhost:8080/api/albums-list?page=1&size=2&sort=title,desc"\n        },\n        "next": {\n            "href": "http://localhost:8080/api/albums-list?page=2&size=2&sort=title,desc"\n        },\n        "last": {\n            "href": "http://localhost:8080/api/albums-list?page=4&size=2&sort=title,desc"\n        }\n    },\n    "page": {\n        "size": 2,\n        "totalElements": 10,\n        "totalPages": 5,\n        "number": 1\n    },\n    "_embedded": {\n        "albums": [\n            {\n                "id": 7,\n                "title": "Top Hits Vol 7",\n                "description": "Top hits vol 7. description",\n                "releaseDate": "10-03-1987",\n                "actors": [\n                    {\n                        "id": …
Run Code Online (Sandbox Code Playgroud)

java rest spring hateoas spring-hateoas

7
推荐指数
1
解决办法
6863
查看次数