标签: spring-hateoas

返回json意外,将"链接"拼写为"_links"并且结构不同,在Spring hateoas中

正如标题所说,我有一个资源对象Product扩展ResourceSupport.但是,我收到的回复具有属性"_links"而不是"links",并且具有不同的结构.

{
  "productId" : 1,
  "name" : "2",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/products/1"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

基于HATEOAS参考,预期是:

{
  "productId" : 1,
  "name" : "2",
  "links" : [
    {
      "rel" : "self"
      "href" : "http://localhost:8080/products/1"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

这是有意的吗?有没有办法改变它,或者如果不是结构那么就是"链接"?

我通过以下代码段添加了selfLink:

product.add(linkTo(ProductController.class).slash(product.getProductId()).withSelfRel());
Run Code Online (Sandbox Code Playgroud)

我使用以下构建文件的spring boot:

dependencies {
    compile ("org.springframework.boot:spring-boot-starter-data-rest") {
        exclude module: "spring-boot-starter-tomcat"
    }

    compile "org.springframework.boot:spring-boot-starter-data-jpa"
    compile "org.springframework.boot:spring-boot-starter-jetty"
    compile "org.springframework.boot:spring-boot-starter-actuator"

    runtime "org.hsqldb:hsqldb:2.3.2"

    testCompile "junit:junit"
}
Run Code Online (Sandbox Code Playgroud)

java spring hateoas spring-hateoas spring-boot

10
推荐指数
4
解决办法
7908
查看次数

Spring Data Rest Ambiguous Association Exception

新添加的LinkCollectingAssociationHandlerMappingException由于我的域类中的模糊关联而引发的.

links数组如下所示:

[<http://localhost:8080/rooms/2/roomGroups>;rel="roomGroups", <http://localhost:8080/rooms/2/userGroup>;rel="userGroup", <http://localhost:8080/rooms/2/room>;rel="room", <http://localhost:8080/rooms/2/originatingConferences>;rel="originatingConferences", <http://localhost:8080/rooms/2/user>;rel="user"]

它正在尝试在抛出异常时添加另一个"房间"关系.

问题是它似乎在添加我已明确标记的关系链接 @RestResource(exported = false)

以下是我认为导致此问题的关系示例:

@JsonIgnore
@RestResource(exported = false)
@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.room", cascade = {CascadeType.REMOVE})
private Set<RoomsByUserAccessView> usersThatCanDisplay = new HashSet<>(); 
Run Code Online (Sandbox Code Playgroud)

该类型RoomsByUserAccessView具有由a Room和a 组成的嵌入式id User.

我还注释了嵌入式id属性:

@JsonIgnore
@RestResource(exported = false)
private RoomsByUserAccessViewId pk = new RoomsByUserAccessViewId();
Run Code Online (Sandbox Code Playgroud)

和它的属性如下:

@JsonIgnore
@RestResource(exported = false)
private Room room;

@JsonIgnore
@RestResource(exported = false)
private User userWithAccess;

public RoomsByUserAccessViewId() {
    //
}
Run Code Online (Sandbox Code Playgroud)

在序列化为JSON时,如何才能正确忽略这些关系?

我的代码在DATAREST-262之前工作(https://github.com/spring-projects/spring-data-rest/commit/1d53e84cae3d09b09c4b5a9a4caf438701527550)

我尝试访问房间/端点时返回的完整错误消息如下: …

spring json spring-mvc spring-data-rest spring-hateoas

9
推荐指数
1
解决办法
2450
查看次数

为具有复合ID的实体自定义HATEOAS链接生成

我已经配置了RepositoryRestResource一个PageAndSortingRepository访问包含复合ID的实体的on :

@Entity
@IdClass(CustomerId.class)
public class Customer {
    @Id BigInteger id;
    @Id int startVersion;
    ...
}

public class CustomerId {
    BigInteger id;
    int startVersion;
    ...
}

@RepositoryRestResource(collectionResourceRel = "customers", path = "customers", itemResourceRel = "customers/{id}_{startVersion}")
public interface CustomerRepository extends PagingAndSortingRepository<Customer, CustomerId> {}
Run Code Online (Sandbox Code Playgroud)

"http://<server>/api/customers/1_1"例如,当我访问服务器时,我获得了正确的资源作为json,但是_links部分中的href是错误的,并且对于我查询的任何其他客户也是如此:"http://<server>/api/customer/1"

即:

{
  "id" : 1,
  "startVersion" : 1,
  ...
  "firstname" : "BOB",
  "_links" : {
    "self" : {
      "href" : "http://localhost:9081/reps/api/reps/1" <-- This should be /1_1
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我想这是因为我的复合ID,但我对如何更改此默认行为感到兴奋.

我已经看过这个 …

spring spring-mvc spring-data-jpa spring-hateoas

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

所有Spring-MVC控制器的ALPS元数据

我有一个关于春天hateoas /数据休息中ALPS支持的问题.似乎只有在拥有spring数据库存储库时才会公开ALPS元数据.所以我的问题是为什么只有弹簧数据休息库的支持,我可以为所有spring mvc控制器启用吗?是否有任何示例来公开所有控制器的ALPS元数据?

也许我还没有理解ALPS的概念......

电贺

Meleagros

http://spring.io/blog/2014/07/14/spring-data-rest-now-comes-with-alps-metadata

spring spring-mvc spring-data-rest spring-hateoas

9
推荐指数
1
解决办法
1778
查看次数

如何使用Spring HATEOAS发布和发布链接

我正在尝试了解如何在Spring HATEOAS中创建和修改链接.

例如,假设我有两个集合,一个在api/users,另一个在api/event.我想将用户api/user/56与事件api/event/21相关联.为了论证,这是多对多的 - 用户可以参加许多活动,一个活动可能有很多用户.

据我了解,这样做的另一种方法是使用URI作为主键,因此我可以将以下内容发布到api/user/56/events;

{
    attends: "http://localhost:9090/api/event/21"
}
Run Code Online (Sandbox Code Playgroud)

然后端点需要能够解析该URL并提取ID(在本例中为21)和控制器(EventController.class),以便我可以持久保存.

问题1:这是在REST API方面处理Spring Hateoas中的关系的正确方法吗?

问题2:如何将控制器中的此URL解析为数据的可用句柄(例如,对相应控制器/方法的引用,主键等)

研究

RestTemplate可用于从请求映射方法内的控制器请求数据,如此;

RestTemplate restTemplate = new RestTemplate();
ResponseEntity<EventResource> response = restTemplate.getForEntity(attendsUrl, EventResource.class);
EventResource eventResource = response.getBody();
Run Code Online (Sandbox Code Playgroud)

但是我不相信eventResource应该返回一个Id字段作为数据的一部分 - 它不是很安静,这将在API上公开.一种方法是使参数"includePK = true",但这感觉不对 - 它只是隐藏了问题.此外,服务器以这种方式向它自己的API发出请求的想法似乎很迂回.

更新

这里有一个未解决的问题https://github.com/spring-projects/spring-hateoas/issues/292.基于该问题的一些评论(由用户kevinconaway)松散地我已经创建了一个快速的util类,它提供了一个简单的解决方案:SpringHateoasUtils.解决方案归结为;

String mapping = DISCOVERER.getMapping(targetClass, targetMethod);
UriTemplate template = new UriTemplate(mapping);
//values is key/value map of parameters that the referenced method accepts
Map<String, String> values = uriTemplate.match(uri);
Run Code Online (Sandbox Code Playgroud)

SpringHateoasUtils使这个稍微好一点,但它仍然觉得它应该是一个功能.我将寻求在春季代码中得到一些东西 - 当它清楚地发生了什么时,我会回答这个问题.

java rest spring hateoas spring-hateoas

9
推荐指数
1
解决办法
5740
查看次数

Spring HATEOAS&HAL:在_embedded中更改数组名称

我正在尝试使用Spring HATEOAS构建符合HAL的REST API.

在一些摆弄之后,我设法开始工作,大多是预期的.(样本)输出现在看起来像这样:

{
    "_links": {
        "self": {
            "href": "http://localhost:8080/sybil/configuration/bricks"
        }
    },
    "_embedded": {
        "brickDomainList": [
            {
                "hostname": "localhost",
                "port": 4223,
                "_links": {
                    "self": {
                        "href": "http://localhost:8080/sybil/configuration/bricks/localhost"
                    }
                }
            },
            {
                "hostname": "synerforge001",
                "port": 4223,
                "_links": {
                    "self": {
                        "href": "http://localhost:8080/sybil/configuration/bricks/synerforge001"
                    }
                }
            }
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

我不喜欢"brickDomainList"数组的名称.理想情况下应该说"砖块".我该怎么改变它?

这是产生输出的控制器:

@RestController
@RequestMapping("/configuration/bricks")
public class ConfigurationBricksController {

    private BrickRepository brickRepository;
    private GraphDatabaseService graphDatabaseService;

    @Autowired
    public ConfigurationBricksController(BrickRepository brickRepository, GraphDatabaseService graphDatabaseService) {

        this.brickRepository = brickRepository;
        this.graphDatabaseService = graphDatabaseService;
    }

    @ResponseBody
    @RequestMapping(method …
Run Code Online (Sandbox Code Playgroud)

java spring spring-hateoas

9
推荐指数
1
解决办法
3043
查看次数

如何在代理后面配置Spring HATEOAS?

我有Hateoas的Spring Data Rest作为我的支持.它是代理人的背后.

后端网址: backend.com

代理网址: proxy.com

当我查询代理网址时,例如http://proxy.com/items/1,我得到一个href带域名链接的回复backend.com.我需要域名proxy.com.

rest spring hateoas spring-data-rest spring-hateoas

9
推荐指数
3
解决办法
5361
查看次数

Java:编译冲突:返回类型与HATEOAS ResourceSupport.getId不兼容

所以我有这个HATEOAS实体.

@Entity
@Table(name="users")
public class User extends ResourceSupport {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    private long id;

    public User() {

    }

    public Long getId() {
    return new Long(id);
    }

    public void setId(Long id) {
    this.id = id.longValue();
    }
}  
Run Code Online (Sandbox Code Playgroud)

我的实体的id类型为long,但HATEOAS的ResourceSupport要求getId返回一个Link.

实体具有Long id,因为db具有long id,并且它是持久化实体.如何使用HATEOAS实现此实体?

java rest spring jpa spring-hateoas

8
推荐指数
2
解决办法
5121
查看次数

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万
查看次数

Spring Data Rest:从超媒体 Rest JPA 实体的响应中删除 _links 属性

我想自定义超媒体 Rest JPA 实体的响应,并想删除所有 _links 属性和自链接属性。我的客户端应用程序并不大,它知道要调用的确切 REST API。所以我觉得这些在 HTTP 数据包中传输的额外字节在我的应用程序中总是一个开销。

那么如何才能从响应中删除此链接属性呢?

现在 REST API 响应是:

{
  "_embedded" : {
    "questionsTypes" : [ {
      "queTypeID" : 2,
      "queDescription" : "Single choice rating selection",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/question_web/rest/QuestionsType/2"
        },
        "questionsType" : {
          "href" : "http://localhost:8080/question_web/rest/QuestionsType/2{?projection}",
          "templated" : true
        }
      }
    },{
      "queTypeID" : 5,
      "queDescription" : "Subjective questions",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/question_web/rest/QuestionsType/5"
        },
        "questionsType" : {
          "href" : "http://localhost:8080/question_web/rest/QuestionsType/5{?projection}",
          "templated" : true …
Run Code Online (Sandbox Code Playgroud)

spring spring-data-rest spring-hateoas

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