如何创建指向返回 void 的 Spring 控制器操作的链接 [英] How to create link pointing to Spring controller action that returns void

查看:35
本文介绍了如何创建指向返回 void 的 Spring 控制器操作的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 spring mvc,hateoas.我有一个看起来像

I am using spring mvc, hateoas. I have a controller action that looks like

@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 格式是这样的

Now what I want is to publish url to access each profiles' image. JSON format is some thing like this

{
    "profileId" : 342308,
    "userId" : 342308,
    "firstname" : "Henry",
    "lastname" : "Seol",
    "title" : "Mr.",
    "largeImageUrl" : "https://<host>/image/<id>/<publicUrl>/<filename1.jpg>",
    "thumbImageUrl" : "https://<host>/image/<id>/<publicUrl>/<filename2.jpg>"
}

我想添加该链接来代替largeImageUrl"和thumbImageUrl"的值.

I want to add that link in place of the value for "largeImageUrl" and "thumbImageUrl".

如果我使用hateoas的linkTo函数,它说控制器对应的方法不应该返回void.

If I use linkTo function of hateoas it says controller's corresponding method should not return void.

如何创建这种动态链接并将其添加到资源中?

How to create these kind of dynamic link and add it to the resource?

推荐答案

您可以使用

public static ControllerLinkBuilder linkTo(Class<?> controller, Method method, Object... parameters) {

所以它应该是这样的

Link link = linkTo(
  ImageController.class,
  ImageController.class
    .getMethod("image", Integer.class, String.class, String.class, HttpServletRequest.class, HttpServletResponse.class),
  1,
  "url",
  "file"
).withRel("image");

注意:有一个更短的方法

public static ControllerLinkBuilder linkTo(Method method, Object... parameters) {

但它有一个错误,因此它不起作用

but it has a bug therefore it does not work

这篇关于如何创建指向返回 void 的 Spring 控制器操作的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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