Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何创建指向返回void的Spring控制器操作的链接_Spring_Spring Mvc_Spring Data Rest_Spring Hateoas - Fatal编程技术网

如何创建指向返回void的Spring控制器操作的链接

如何创建指向返回void的Spring控制器操作的链接,spring,spring-mvc,spring-data-rest,spring-hateoas,Spring,Spring Mvc,Spring Data Rest,Spring Hateoas,我使用的是SpringMVC,hateoas。我有一个控制器动作,看起来像 @RequestMapping(value = "/images/{userId}/{publicUrl}/{fileName:.+}", method = RequestMethod.GET) public void image(@PathVariable Integer userId, @PathVariable String publicUrl, @PathVariable String fileName, Htt

我使用的是SpringMVC,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://<host>/image/<id>/<publicUrl>/<filename1.jpg>",
    "thumbImageUrl" : "https://<host>/image/<id>/<publicUrl>/<filename2.jpg>"
}
{
“profileId”:342308,
“用户ID”:342308,
“名字”:“亨利”,
“lastname”:“Seol”,
“职务”:“先生”,
“大图像URL”:https:///image///",
“thumbImageUrl”:”https:///image///"
}
我想添加那个链接来代替“largeImageUrl”和“thumbImageUrl”的值

若我使用hateoas的linkTo函数,它表示控制器的相应方法不应返回void

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

您可以使用

public static ControllerLinkBuilder linkTo(Class<?> controller, Method method, Object... parameters) {
注意: 有一个较短的方法

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

但是它有一个bug,因此它不起作用

您可以始终使用
linkTo(…).slash(…)…
作为最后手段。我想知道,当您实际返回图像时,为什么该方法的返回类型是
void
。使用
HttpServletRequest
HttpServletResponse
作为参数是不可行的。您能否建议我如何通过不使用HttpServletRequest或HttpServletResponse作为参数或此控制器的任何其他解决方案来返回此控制器中的映像?一种替代方法是返回
ResponseEntity
。我假设,
InputStreamResource
可以使用
new InputStreamResource(image.getObjectContent())
创建。虽然在这个特定示例中可能会发生更改,但也有其他情况下,void是可以的,并且是经过设计的,例如,action应用了副作用,并且不返回任何内容。
public static ControllerLinkBuilder linkTo(Method method, Object... parameters) {