当包含spring数据其余部分时,spring以纯JSON而非HAL格式返回Resource [英] Spring returns Resource in pure JSON not in HAL Format when including spring data rest

查看:111
本文介绍了当包含spring数据其余部分时,spring以纯JSON而非HAL格式返回Resource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将默认控制器用于我的实体时,Spring Data Rest提供了一切,一切正常.输出看起来像这样:

When I use the default controller for my Entities, provided by Spring Data Rest everything works like it should. The output looks like this:

{
  "_links" : {
    "search" : {
      "href" : "http://localhost:8080/users/search"
    }
  },
  "_embedded" : {
    "users" : [ {
      "firstName" : "Max",
      "lastName" : "Mustermann",
      "email" : "mail@max-mustermann.de",
      "_links" : {
        "self" : {
          "href" : "http://localhost:8080/users/myadmin"
        }
      }
    } ]
  }
}

但是,如果我使用自己的控制器,输出将如下所示:

But if I use my own Controller the output looks like this:

[ {
  "firstName" : "Max",
  "lastName" : "Mustermann",
  "email" : "mail@max-mustermann.de",
  "links" : [ {
    "rel" : "self",
    "href" : "http://localhost:8080/user/myadmin"
  } ]
} ]

我的控制器

@RestController
@RequestMapping("/user")
@EnableHypermediaSupport(type = {HypermediaType.HAL})
public class UserController {

    @Autowired
    UserRepository userRepository;

    @RequestMapping(method=RequestMethod.GET)
    public HttpEntity<List<User>> getUsers(){
        ArrayList<User> users = Lists.newArrayList(userRepository.findAll());
        for(User user : users){
            user.add(linkTo(methodOn(UserController.class).getUser(user.getUsername())).withSelfRel());
        }
        return new ResponseEntity<List<User>>(users, HttpStatus.OK);
    }

    @RequestMapping(value="/{username}", method= RequestMethod.GET)
    public HttpEntity<User> getUser(@PathVariable String username){
        User user = userRepository.findByUsername(username);
        user.add(linkTo(methodOn(UserController.class).getUser(user.getUsername())).withSelfRel());
        return new ResponseEntity<User>(user, HttpStatus.OK);
    }
}

我的用户:

@Entity
@Table(name="users")
public class User extends ResourceSupport{
    @Id
    private String username;

    private String firstName;
    private String lastName;

    @JsonIgnore
    private boolean enabled;

    @JsonIgnore
    private String password;

    @Column(unique =  true)
    private String email;

    public User(){
        enabled = false;
    }
    //Getters and Setters
}

如果我删除spring数据剩余依赖项并包含spring-hateoas,spring-plugin-core和json-path(com.jayway.jsonpath),则它起作用.实体

if i delete the spring data rest dependency and include spring-hateoas, spring-plugin-core and json-path (com.jayway.jsonpath) it works.. But i want to use spring-data-rest for some other entities

两个问题:

  1. 为什么不包含默认的带有弹簧数据其余部分的HAL?
  2. 如何将HAL设置为输出格式

推荐答案

您需要返回ResourceSupport的子类型(通常为ResourceResources),以使HAL Jackson转换器启动.

You need to return a subtype of ResourceSupport (usually Resource or Resources) to let the HAL Jackson converter kick in.

此外,@EnableHypermediaSupport必须在JavaConfig配置类上使用,而不是在控制器上使用.

Also, @EnableHypermediaSupport has to be used on a JavaConfig configuration class, not the controller.

这篇关于当包含spring数据其余部分时,spring以纯JSON而非HAL格式返回Resource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆