0

我已经定义了以下控制器

@Controller

公共类 HelloController {

@RequestMapping(value = "/config/{name:.*}", produces = MediaType.TEXT_PLAIN_VALUE, method = RequestMethod.GET)
@ResponseBody
ResponseEntity<String> getValue(@PathVariable String name) {
    String value = "Hello World";
    return new ResponseEntity<String>(HttpStatus.OK);
}

}

当我从浏览器前 ping 网址时: http://localhost:8080/example/config/test.abc

该请求工作正常。

但是当我用 url http://localhost:8080/example/config/test.uri ping

它只是用错误来炸毁页面: The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers.

我尝试了 MessageConverters 和 configureContentNegotiation 似乎没有任何工作。我想知道 spring 是否将 test.uri 视为无效模式或保留关键字。

我尝试过的环境。弹簧 4/Tomcat 7 和 8 弹簧 5/Tomcat 9

4

3 回答 3

2

尝试不同的正则表达式。

代替

.*

这意味着:除换行符之外的各种数量的任何字符

尝试

[a-z]*\.[a-z]*

这意味着不同数量的 az + 一个点 + 不同数量的 az

如果这是你想要的。

如果您不需要任何模式,则只需使用

{name}

查看

https://www.logicbig.com/tutorials/spring-framework/spring-web-mvc/uri-pattern.html

https://regexr.com/

但我会考虑您是否可以调整您的 API,例如:

@RequestMapping(value = "/config/{name}/{type}", ...

我认为在你的 URI 中期望点不是一个好主意。点表示您正在请求文件。

查看:

带有点 (.) 的 Spring MVC @PathVariable 被截断

于 2019-03-21T15:19:40.123 回答
0

我终于可以使用 ContentNegotiationConfigurer 并设置 uri 的内容类型来解决问题。

    @Override
public void configureContentNegotiation(ContentNegotiationConfigurer contentNegotiationConfigurer) {
    contentNegotiationConfigurer.mediaType("uri", MediaType.TEXT_PLAIN);
}
于 2019-03-21T22:01:31.027 回答
0

Spring 认为最后一个点后面的任何内容都是文件扩展名

为了克服这个

通过添加正则表达式映射修改我们的@PathVariable 定义

@RequestMapping(value = "/config/{name:.+}"

一个安全的猜测是

.abc不是扩展类型,.uri是一个扩展,所以也许这就是你的第一个 URL 有效的原因,

于 2019-03-21T18:56:24.990 回答