为/{repository}/{id}/{property} Spring-data-rest制作verify If-Match头部,可以按照以下步骤操作:
spring-boot-starter-data-rest
。@RestController
注解进行标记。@PathVariable
注解获取repository
、id
和property
的值。EntityLinks
对象获取实体的资源链接,并调用withSelfRel().getHref()
方法获取该资源的URL。HttpServletRequest
对象获取当前请求,然后使用addHeader()
方法添加If-Match
头部,参数为资源URL。以下是一个示例代码:
import org.springframework.data.rest.webmvc.support.DefaultedPageable;
import org.springframework.hateoas.EntityLinks;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
@RestController
public class MyController {
private final EntityLinks entityLinks;
public MyController(EntityLinks entityLinks) {
this.entityLinks = entityLinks;
}
@GetMapping("/{repository}/{id}/{property}")
public ResponseEntity<String> getProperty(
@PathVariable String repository,
@PathVariable String id,
@PathVariable String property,
HttpServletRequest request) {
String resourceUrl = entityLinks
.linkToItemResource(repository.getClass(), id)
.withSelfRel()
.getHref();
HttpHeaders headers = new HttpHeaders();
headers.add("If-Match", resourceUrl);
// 执行你的逻辑
return ResponseEntity.ok().headers(headers).body("Success");
}
}
请注意,这只是一个示例代码,具体的实现还需要根据你的业务逻辑进行调整。希望这个示例能对你有所帮助。