Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.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
Java Spring MVC@PathVariable试图使用矩阵参数_Java_Rest_Spring Mvc_Jersey - Fatal编程技术网

Java Spring MVC@PathVariable试图使用矩阵参数

Java Spring MVC@PathVariable试图使用矩阵参数,java,rest,spring-mvc,jersey,Java,Rest,Spring Mvc,Jersey,我正在将我的一个项目从Jersey迁移到Spring MVC的过程中,我的两个资源遇到了一个问题,它们做的事情几乎是一样的;只有一个用于获取属于登录用户的客户的统计信息,而另一个则使用客户ID执行相同的操作 这就是泽西岛的资源最初的样子: @GET @Produces(MediaType.APPLICATION_JSON) @Secured({Role.USER}) public List<Stat> getUserStats(@Context User user,

我正在将我的一个项目从Jersey迁移到Spring MVC的过程中,我的两个资源遇到了一个问题,它们做的事情几乎是一样的;只有一个用于获取属于登录用户的客户的统计信息,而另一个则使用客户ID执行相同的操作

这就是泽西岛的资源最初的样子:

@GET
@Produces(MediaType.APPLICATION_JSON)
@Secured({Role.USER})
public List<Stat> getUserStats(@Context User user,
                               @MatrixParam("from") String from,
                               @MatrixParam("to") String to) {
    return statService.getStats(user, from, to);
}

@GET
@Path("/{cid}")
@Produces(MediaType.APPLICATION_JSON)
@Secured({Role.ADMIN})
public List<Stat> getCustomerStats(@PathParam("cid") Long cid,
                                   @MatrixParam("from") String from,
                                   @MatrixParam("to") String to) {
    return statService.getStats(cid, from, to);
}
(我暂时将
@ModelAttribute
排除在等式之外,以免不必要地使事情复杂化。)

我正试着给这些资源打电话

http://localhost:8080/stats/from=2017-05-01;to=2017-05-31

适用于用户和

http://localhost:8080/stats/12345;from=2017-05-01;to=2017-05-31

对于管理员

后者始终有效,但第一个会导致出现
MethodArgumentTypeMismatchException
,并显示以下消息:

Failed to convert value of type 'java.lang.String' to required type
'java.lang.Long'; nested exception is java.lang.NumberFormatException:
For input string: \"from=2017-05-01;to=2017-05-31\"
我最初根本无法让矩阵参数工作,但通过谷歌搜索,我实现了以下配置:

@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        UrlPathHelper urlPathHelper = new UrlPathHelper();
        urlPathHelper.setRemoveSemicolonContent(false);
        urlPathHelper.setAlwaysUseFullPath(true);
        configurer.setUrlPathHelper(urlPathHelper);
    }
}

如果有人知道为什么第二个端点尝试使用所有东西,我肯定需要一些帮助。

您是否可以尝试绑定到map
@MatrixVariable map matrixVars
并从map-Aldo提取矩阵变量?您是否启用了矩阵变量
@fg78nc:通过绑定到地图,我得到了完全相同的结果。我使用的是JavaConfig,所以没有现成的属性,但是矩阵变量在我的项目中通常是有效的,除非它们前面有路径变量,比如在本例中。我试着更仔细地研究,但这似乎是一个模糊配置的滑坡,我希望我不必处理。stats是您的上下文路径?是的,因此解析路径没有问题,并且在没有路径变量的情况下,它可以完美地工作。
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        UrlPathHelper urlPathHelper = new UrlPathHelper();
        urlPathHelper.setRemoveSemicolonContent(false);
        urlPathHelper.setAlwaysUseFullPath(true);
        configurer.setUrlPathHelper(urlPathHelper);
    }
}