标签: spring-hateoas

我可以使自定义控制器镜像生成Spring-Data-Rest/Spring-Hateoas生成的类的格式吗?

我正在尝试做一些我认为应该非常简单的事情.我有一个Question对象,设置有spring-boot,spring-data-rest和spring-hateoas.所有的基础工作都很好.我想添加一个自定义控制器,它返回一个List<Question>与我Repository/questions网址完全相同的格式,以便两者之间的响应兼容.

这是我的控制器:

@Controller
public class QuestionListController {

    @Autowired private QuestionRepository questionRepository;

    @Autowired private PagedResourcesAssembler<Question> pagedResourcesAssembler;

    @Autowired private QuestionResourceAssembler questionResourceAssembler;

    @RequestMapping(
            value = "/api/questions/filter", method = RequestMethod.GET,
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public @ResponseBody PagedResources<QuestionResource> filter(
            @RequestParam(value = "filter", required = false) String filter,
            Pageable p) {

        // Using queryDSL here to get a paged list of Questions
        Page<Question> page = 
            questionRepository.findAll(
                QuestionPredicate.findWithFilter(filter), p);

        // Option 1 - default resource assembler
        return …
Run Code Online (Sandbox Code Playgroud)

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

24
推荐指数
3
解决办法
6935
查看次数

如何使用Spring HATEOAS更改HAL链接格式

我正在使用Spring HATEOAS(0.16.0.RELEASE)构建Spring REST应用程序,我希望JSON链接输出看起来像:

_links: {
   self: {
     href: "https://<ip>/api/policies/321"
   }
}
Run Code Online (Sandbox Code Playgroud)

虽然它呈现如下:

   "links":
      [{
       "rel":"self",
       "href":"http://<ip>/api/policies/321"
      }]
Run Code Online (Sandbox Code Playgroud)

我正在使用HATEOAS ResourceResourceAssembler.

为什么我会使用这种格式而不是其他格式?我该怎么改变它?

rest spring hal spring-mvc spring-hateoas

22
推荐指数
1
解决办法
9631
查看次数

如何避免使用Spring Data Rest进行n + 1查询?

题.如何使用Spring Data REST避免n + 1查询?

背景.在查询Spring Data REST以获取资源列表时,每个生成的顶级资源都具有指向关联资源的链接,而不是将关联资源直接嵌入顶级资源中.例如,如果我查询数据中心列表,则关联的区域显示为链接,如下所示:

{
  "links" : [ {
    "rel" : "self",
    "href" : "http://localhost:2112/api/datacenters/1"
  }, {
    "rel" : "datacenters.DataCenter.region",
    "href" : "http://localhost:2112/api/datacenters/1/region"
  } ],
  "name" : "US East 1a",
  "key" : "amazon-us-east-1a"
}
Run Code Online (Sandbox Code Playgroud)

然而,非常典型的是,想要获得相关信息而不必进行n + 1次查询.为了坚持上面的例子,我可能想要在UI中显示数据中心及其相关区域的列表.

我试过的.我在my上创建了一个自定义查询,RegionRepository以获取给定数据中心键集的所有区域:

@RestResource(path = "find-by-data-center-key-in")
Page<Region> findByDataCentersKeyIn(
    @Param("key") Collection<String> keys,
    Pageable pageable);
Run Code Online (Sandbox Code Playgroud)

遗憾的是,此查询生成的链接与上面的数据中心查询生成的链接不重叠.以下是我为自定义查询获得的链接:

http://localhost:2112/api/regions/search/find-by-data-center-key-in?key=amazon-us-east-1a&key=amazon-us-east-1b

{
  "links" : [ ],
  "content" : [ {
    "links" : [ {
      "rel" : "self",
      "href" : "http://localhost:2112/api/regions/1"
    }, { …
Run Code Online (Sandbox Code Playgroud)

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

21
推荐指数
2
解决办法
6854
查看次数

解析自定义控制器中的实体URI(Spring HATEOAS)

我有一个基于spring-data-rest的项目,它也有一些自定义端点.

为了发送POST数据,我正在使用json

{
 "action": "REMOVE",
 "customer": "http://localhost:8080/api/rest/customers/7"
}
Run Code Online (Sandbox Code Playgroud)

这对于s​​pring-data-rest来说很好,但不适用于自定义控制器.

例如:

public class Action {
    public ActionType action;
    public Customer customer;
}

@RestController
public class ActionController(){
  @Autowired
  private ActionService actionService;

  @RestController
  public class ActionController {
  @Autowired
  private ActionService actionService;

  @RequestMapping(value = "/customer/action", method = RequestMethod.POST)
  public ResponseEntity<ActionResult> doAction(@RequestBody Action action){
    ActionType actionType = action.action;
    Customer customer = action.customer;//<------There is a problem
    ActionResult result = actionService.doCustomerAction(actionType, customer);
    return ResponseEntity.ok(result);
  }
}
Run Code Online (Sandbox Code Playgroud)

我打电话的时候

curl -v -X POST -H "Content-Type: application/json" -d '{"action": "REMOVE","customer": …
Run Code Online (Sandbox Code Playgroud)

java rest spring spring-data-rest spring-hateoas

18
推荐指数
2
解决办法
3967
查看次数

没有HATEOAS的Spring数据休息

我真的很喜欢Spring Data Rest为你写的所有样板代码,但我宁愿只是一个'常规?' 没有所有HATEOAS东西的REST服务器.主要原因是我在客户端使用Dojo Toolkit,并且它的所有小部件和存储都被设置为返回的json只是一个直接的项目数组,没有所有链接和类似的东西.有谁知道如何配置这个与java配置,以便我得到所有为我编写的mvc代码,但没有所有的HATEOAS东西?

spring-data-rest spring-hateoas

17
推荐指数
2
解决办法
9004
查看次数

如何禁用RepositoryRestHandlerMapping和EndpointHandlerMapping?

我目前正在使用Spring Boot,Hibernate和Spring-HATEOAS构建一个带REST接口的应用程序.我的数据模型定义为带@Entity注释的bean,我使用Spring的功能自动设置Hibernate存储库(创建扩展接口PagingAndSortingRepository).我的应用程序是完全注解驱动的,即无我有,web.xml但一切配置与Spring的注解一样@Configuration,@Bean等等,并开始从我的应用程序main的帮助下方法SpringApplication.run(MyApp.class, args);

这工作正常,但使用这种方法,a RepositoryRestHandlerMappingEndpointHandlerMapping创建.这些创造了我既不需要也不想要的一堆资源.我实现自己的控制器,因为他们需要做的不仅仅是标准逻辑.

如何防止此默认行为并禁用这些映射?

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

17
推荐指数
2
解决办法
7858
查看次数

如何删除Spring HATEOAS中的"_embedded"属性

我正在使用Spring Boot和HATEOAS来构建REST API,当我的API返回一个集合时,它被包装在一个"_embedded"属性中,如下所示:

{
   "_links":{
      "self":{
         "href":"http://localhost:8080/technologies"
      }
   },
   "_embedded":{
      "technologies":[
         {
            "id":1,
            "description":"A",
            "_links":{
               "self":{
                  "href":"http://localhost:8080/technologies/1"
               }
            }
         },
         {
            "id":2,
            "description":"B",
            "_links":{
               "self":{
                  "href":"http://localhost:8080/technologies/2"
               }
            }
         }
      ]
   }
}
Run Code Online (Sandbox Code Playgroud)

我希望响应是这样的:

{
   "_links":{
      "self":{
         "href":"http://localhost:8080/technologies"
      }
   },
   "technologies":[
      {
         "id":1,
         "description":"A",
         "_links":{
            "self":{
               "href":"http://localhost:8080/technologies/1"
            }
         }
      },
      {
         "id":2,
         "description":"B",
         "_links":{
            "self":{
               "href":"http://localhost:8080/technologies/2"
            }
         }
      }
   ]
}
Run Code Online (Sandbox Code Playgroud)

我的技术控制器:

@RestController
@ExposesResourceFor(Technology.class)
@RequestMapping(value = "/technologies")
public class TechnologiesController {
    ...
    @ResquestMapping(method = RequestMethod.GET, produces = "application/vnd.xpto-technologies.text+json")
    public Resources<Resource<Technology>> getAllTechnologies() …
Run Code Online (Sandbox Code Playgroud)

rest spring spring-hateoas spring-boot

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

如何配置由spring-hateoas注册的MappingJackson2HttpMessageConverter

我喜欢在我的项目中使用spring-hateoas并配置它@EnableHypermediaSupport.现在的问题是,这个神奇的配置注释会注册它自己MappingJackson2HttpMessageConverter,我的自定义转换器将被忽略.

背景:我在项目中添加了一些Jackson模块(比如JodaModule),我希望它们能够使用objectMapper.findAndRegisterModules();.这是通过覆盖完成configureMessageConverters(List<HttpMessageConverter<?>> converters)WebMvcConfigurationSupportWebMvcConfigurer.

我当前的配置如下所示:

@Configuration
@EnableHypermediaSupport(type = HAL)
public class WebMvcConfiguration extends WebMvcConfigurationSupport {

  @Override
  protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    converter.getObjectMapper().findAndRegisterModules();

    converters.add(converter);
  }
}
Run Code Online (Sandbox Code Playgroud)

是否有自定义的方式MappingJackson2HttpMessageConverterObjectMapper所使用的弹簧HATEOAS?

java spring json spring-mvc spring-hateoas

16
推荐指数
1
解决办法
8404
查看次数

以下方法不存在:'org.springframework.plugin.core.PluginRegistry org.springframework.plugin.core.PluginRegistry.of(java.util.List)'

pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-hateoas</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <version>2.2.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>

        <!-- The Bad Boy -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-rest-hal-browser</artifactId>
            <version>3.2.5.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.plugin</groupId>
            <artifactId>spring-plugin-core</artifactId> …
Run Code Online (Sandbox Code Playgroud)

spring-data-rest spring-hateoas spring-boot swagger-2.0

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

使用spring数据休息后发布嵌套对象?

我最近开始在我的应用程序中使用spring-data-rest.我有以下JPA实体:

@Entity
public class Super {
    @Id
    private long id;

    @JoinTable
    @OneToMany(cascade = CascadeType.ALL)
    private List<Child> children;
}

-----------------------------------------

@Entity
public class Super2 {
    @Id
    private long id;

    @JoinTable
    @OneToMany(cascade = CascadeType.ALL)
    private List<Child> children;
}

-----------------------------------------

@Entity
public class Child {
    @Id
    private long id;

    @Column
    private String childMetadata;
}
Run Code Online (Sandbox Code Playgroud)

我能想到的节能的新实例的2种方法SuperSuper2:

  1. 暴露一个@RestResourcefor Childclass - > Child在创建实例之前创建所有实例SuperSuper2- > ChildSuperor的有效负载中传递所有实例的URL Super2.
  2. 传递Child有效负载中的详细信息,Super …

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

15
推荐指数
1
解决办法
1862
查看次数