我正在使用spring mvc,hateoas.我有一个看起来像的控制器动作
@RequestMapping(value = "/images/{userId}/{publicUrl}/{fileName:.+}",method = RequestMethod.GET)
public void image(@PathVariable Integer userId,@PathVariable String publicUrl,@PathVariable String fileName,HttpServletRequest request,HttpServletResponse response) throws Exception {
try(HellodoxAws aws = haws;){
.....
.....
response.setContentType(image.getObjectMetadata().getContentType());
response.setHeader("ETag",image.getObjectMetadata().getETag());
response.setHeader("Cache-Control",image.getObjectMetadata().getCacheControl());
response.setHeader("Last-Modified",image.getObjectMetadata().getLastModified().toString());
IoUtils.copy(image.getObjectContent(),response.getOutputStream());
}catch (Exception e) {
if(e instanceof AmazonS3Exception){
int statusCode = ((AmazonS3Exception) e).getStatusCode();
//System.out.println("Status Code : "+statusCode);
response.setContentType("image/jpeg");
if(statusCode==HttpStatus.NOT_MODIFIED.value()){
response.setHeader("ETag",((AmazonS3Exception) e).getAdditionalDetails().get("ETag"));
response.setHeader("Cache-Control",((AmazonS3Exception) e).getAdditionalDetails().get("Cache-Control"));
response.setHeader("Last-Modified",((AmazonS3Exception) e).getAdditionalDetails().get("Last-Modified"));
}
response.setStatus(statusCode);
}
}
}
这个动作完美无缺.
现在我想要的是发布url来访问每个配置文件的图像. JSON格式是这样的
{
"profileId" : 342308,"userId" : 342308,"firstname" : "Henry","lastname" : "SEOl","title" : "Mr.","largeImageUrl" : "https://
我想添加该链接来代替“largeImageUrl”和“thumbImageUrl”的值.
最佳答案
你可以使用
public static ControllerLinkBuilder linkTo(Class> controller,Method method,Object... parameters) {
所以应该是这样的
Link link = linkTo(
ImageController.class,ImageController.class
.getMethod("image",Integer.class,String.class,HttpServletRequest.class,HttpServletResponse.class),1,"url","file"
).withRel("image");
注意 :
有一个较短的方法
public static ControllerLinkBuilder linkTo(Method method,Object... parameters) {
但它有一个bug,因此它不起作用