Brixton.RC1
对我来说,例如 for .properties 的内容协商机制在切换到 Spring Cloud或Brixton.RC2
从Angel.SR5
和之后在 Spring Cloud Config Server 中不起作用Angel.SR6
。
当我使用gradlew bootRun
or启动服务时会出现问题java -jar ...
。它在我的集成测试中工作(见Working Integration-Test)。
使用场景:
我想访问testing
应用程序配置文件中的配置my-service
,所以我打电话http://localhost:8888/my-service/testing.properties
。
预期结果:
some.property=1234
some.other.property=hello there
实际结果:
没有Accept
-Header:
<!DOCTYPE html>
<html>
<head>
<title>Error 406</title>
</head>
<body>
<h1>Error 406: Not Acceptable</h1>
<br/>
Could not find acceptable representation
</body>
</html>
使用Accept
-Header application/json
:
{
"timestamp": 1461140158009,
"status": 406,
"error": "Not Acceptable",
"exception": "org.springframework.web.HttpMediaTypeNotAcceptableException",
"message": "Could not find acceptable representation",
"path": "/config/my-service/default.properties"
}
从示例中可以看出,内容协商机制似乎适用于错误处理,但对于配置访问却不是。
工作集成测试:
我写了以下Spock-Test
@WebIntegrationTest({"server.port=0", "management.port=0"})
@ActiveProfiles("testing")
@ContextConfiguration(loader = SpringApplicationContextLoader.class, classes = ConfigurationServiceApplication.class)
class ConfigurationAccessTest extends Specification {
@Autowired
TestserverInfo testserverInfo
def "testing profile returns testing properties"() {
given:
RestTemplate rest = new TestRestTemplate(null, null)
Properties properties = new Properties()
when:
String result = rest.getForObject( testserverInfo.getBasePath() + "/my-service-testing.properties", String.class );
properties.load( new StringReader(result) )
then:
properties['test'] == 'Testing Profile World'
properties['my.long.testing.property'] == 'Testing Profile Property'
}
到目前为止我已经做过的事情:
- 为这个正在工作的场景写了一个上面的Spock-Test所有提到的版本中都有效
Spring Cloud Config Server
- 查看
ConfigServerMvcConfiguration
任何明显的配置错误 自己提供一个 WebMvcConfigurer 并像上面引用的配置类一样初始化内容协商:
@EnableWebMvc @Configuration public class ConfigMvcConfiguration extends WebMvcConfigurerAdapter { private final Logger logger = LoggerFactory.getLogger(ConfigMvcConfiguration.class); @Override public void configureContentNegotiation(ContentNegotiationConfigurer configurer) { configurer.mediaType("properties", MediaType.valueOf("text/plain")); configurer.mediaType("yml", MediaType.valueOf("text/yaml")); configurer.mediaType("yaml", MediaType.valueOf("text/yaml")); logger.info("media-types added"); } }
任何人都可以重现此问题或为我提供有关如何解决它的任何指导吗?