来自Hateoas Spring Boot的资源响应显示Whitelabel错误页面

时间:2018-01-25 17:59:45

标签: java spring-boot spring-hateoas

我不是专业的Spring Boot开发人员。我正在编写一个程序,在教程的帮助下,我正在使用Hateoas和Rest,当我从请求返回响应时,我得到一个whitelabel错误页面。

当我尝试使用System.out.println()编写值时,resourceobject本身看起来是正确的。

这是RestController方法。

QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); // DPI support
QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); //HiDPI pixmaps
qputenv("QT_SCALE_FACTOR", "1");

这是我在RestController-class

中使用的导入
@RequestMapping(method = RequestMethod.GET ,produces = {MediaType.APPLICATION_JSON_VALUE, "application/hal+json"})
    Resources<SubjectResource> readSubject(@PathVariable String userId) {

        this.validateUser(userId);
        List<SubjectResource>subjectResource=subjectRepository.findByTeacherUsername(userId).stream().map(SubjectResource:: new).collect(Collectors.toList());

        Resources <SubjectResource>r=new Resources<>(subjectResource);

        return new Resources<>(subjectResource);

    }

这是资源文件

import java.net.URI;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.hateoas.Link;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.Resources;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

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

import org.springframework.hateoas.Link;

在我的浏览器中我打字

  

http://localhost:8080/anna_n/subjects

有谁知道我做错了什么?谢谢/ Micke

1 个答案:

答案 0 :(得分:1)

首先让我们从一个示例开始,与您的代码进行比较:

@RestController
@ExposesResourceFor(Booleans.class)
@RequestMapping("/booleans")
public class BooleansController {

    public static final String TRUE = "true";
    public static final String FALSE = "false";
    public static final String TRUEFALSE = "truefalse";

    @Autowired
    private EntityLinks entityLinks;

    @RequestMapping(value = "/{booleansId}",
            produces = { "application/hal+json", "application/json" },
            method = RequestMethod.GET)
    public ResponseEntity<Booleans> getBooleans(@PathVariable("booleansId") String booleansId) {
        return getResource(booleansId)
                .map(r -> new ResponseEntity<>(r, HttpStatus.OK))
                .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    private Optional<Booleans> getResource(String id)  {
        switch (id) {
            case TRUE:
                final Booleans trueInstance = new Booleans().addValuesItem(true);
                trueInstance.add(entityLinks.linkToSingleResource(Booleans.class, TRUE));
                return Optional.of(trueInstance);
            case FALSE:
                final Booleans falseInstance = new Booleans().addValuesItem(false);
                falseInstance.add(entityLinks.linkToSingleResource(Booleans.class, FALSE));
                return Optional.of(falseInstance);
            case TRUEFALSE:
                final Booleans truefalseInstance = new Booleans().addValuesItem(true).addValuesItem(false);
                truefalseInstance.add(entityLinks.linkToSingleResource(Booleans.class, TRUEFALSE));
                return Optional.of(truefalseInstance);
            default:
                return Optional.empty();
        }
    }

}

资源:

public class Booleans extends ResourceSupport {

    @JsonProperty("values")
    private List<Boolean> values = new ArrayList<Boolean>();

    public Booleans addValuesItem(Boolean valuesItem) {
        this.values.add(valuesItem);
        return this;
    }

    public List<Boolean> getValues() {
        return values;
    }
    public void setValues(List<Boolean> values) {
       this.values = values;
    }

}

尝试使用curl:

curl "http://localhost:8085/myapp/booleans/truefalse"

{
  "_links": {
    "self": {
      "href": "http://localhost:8085/myapp/booleans/truefalse"
    }
  },
  "values": [
    true,
    false
  ]
}

显着的差异是@RequestMapping(value = "/{booleansId}",,您的代码中缺少这些差异。这是我的第一个赌注。试试@RequestMapping(value = "/{userId}/subjects"

我更新了这个答案,使用EntityLinks和@ExposesResourceFor注释以及类和方法级别@RequestMapping来演示如何确保资源的正确JSON HAL链接。有关构建适当资源的方法,请参阅ResourceAssemblerSupport