我已经在这个问题上投入了很多时间。我有一个spring数据后端,因此它返回了hateoas对象。我正在尝试将后端的结果提取到Resource
对象中。发布数据的工作原理也将返回发布的对象。但是,在获取对象列表时,它总是返回一个空对象。
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
</dependencies>
FeignClient
@FeignClient(name="products", url = "http://localhost:8084")
public interface ProductClient {
@GetMapping("/products")
Resources<Product> getProducts();
@RequestMapping(method = RequestMethod.POST, value = "/products")
Resource<Product> addProduct(Product product);
}
RestController
@RestController
@RequestMapping("products")
public class ProductController {
private ProductClient productClient;
@Autowired
public ProductController(ProductClient productClient) {
this.productClient = productClient;
}
@GetMapping
public Resources<Product> getProduct(){
Resources<Product> productResources = productClient.getProducts();
return productResources;
}
@PostMapping
public Resource<Product> addProduct(@RequestBody Product product){
Resource<Product> product2 = productClient.addProduct(product);
return product2;
}
}