Spring 弹簧不';不使用RESTful Web服务,但正确地反序列化相应的字符串,为什么?
我试图使用Spring 弹簧不';不使用RESTful Web服务,但正确地反序列化相应的字符串,为什么?,spring,rest,spring-boot,java-8,jackson2,Spring,Rest,Spring Boot,Java 8,Jackson2,我试图使用Spring使用restfulweb服务,我的参考代码就是其中的一个。我询问的服务返回JSON,如下所示: { "id": 1, "content": [ { "dependencies": [ { "parent": 0, "child": 1, "type": "PUNCT" }, { "parent": 0, "child": 2, "type": "NPADVMOD" }, { "parent": 0, "child": 3, "
Spring
使用restfulweb服务
,我的参考代码就是其中的一个。我询问的服务返回JSON
,如下所示:
{ "id": 1,
"content": [
{ "dependencies": [
{ "parent": 0, "child": 1, "type": "PUNCT" },
{ "parent": 0, "child": 2, "type": "NPADVMOD" },
{ "parent": 0, "child": 3, "type": "PUNCT" } ],
"tokens": [
{ "ent": "", "index": 0, "lemma": "hello", "pos": "INTJ", "tag": "UH", "text": "Hello" },
{ "ent": "", "index": 1, "lemma": ",", "pos": "PUNCT", "tag": ",", "text": "," },
{ "ent": "PERSON", "index": 2, "lemma": "world", "pos": "PROPN", "tag": "NNP", "text": "World" },
{ "ent": "", "index": 3, "lemma": "!", "pos": "PUNCT", "tag": ".", "text": "!" } ]
} ]
}
我创建了以下对象(为了简洁起见,省略了它们的所有方法):
正如我在日志中看到的:
17:26:34.609 [qtp990226843-17] INFO a.c.j.controllers.ExampleController - Message[content = [Content[dependencies = [Dependency[child = 1, parent = 0, type = PUNCT], Dependency[child = 2, parent = 0, type = NPADVMOD], Dependency[child = 3, parent = 0, type = PUNCT]], tokens = [Token[ent = , index = 0, lemma = hello, pos = INTJ, tag = UH, text = Hello], Token[ent = , index = 1, lemma = ,, pos = PUNCT, tag = ,, text = ,], Token[ent = PERSON, index = 2, lemma = world, pos = PROPN, tag = NNP, text = World], Token[ent = , index = 3, lemma = !, pos = PUNCT, tag = ., text = !]]]], id = 1]
如果我尝试使用以下代码直接连接到RESTful Web服务:
@RequestMapping("/remote")
public Boolean remote() {
RestTemplate restTemplate = new RestTemplate();
Map<String, String> env = System.getenv();
String host = env.getOrDefault("MY_HOST", "localhost");
String port = env.getOrDefault("MY_HOST", "7890");
String url = String.format("http://%s:%s", host, port);
String content = "Hello, World!";
URI uri = UriComponentsBuilder.fromHttpUrl(url).queryParam("content", content).build().encode().toUri();
Message response = REST_TEMPLATE.getForObject(uri, Message.class);
logger.info("I got: {}", response);
return Boolean.TRUE;
}
我在网上搜索,在这个网站上寻找解决方案,但我想不出任何关键字,导致有意义的结果。。。有什么帮助吗
为什么会出现此错误?如何修复它?
谢谢
PS:我刚刚尝试实现一个哑服务
,它连接到远程服务
,并将该服务接收到的JSON作为字符串返回,我修改了解码服务
,该服务已经能够对JSON进行反序列化,从而从哑服务
而不是远程服务
获取数据,而且它工作正常!我大吃一惊!这没有道理!
无论如何,代码如下(请告诉我是否可以避免使用哑服务
):
@RequestMapping(“/dumb”)
公共字符串dumb(){
Map env=System.getenv();
字符串host=env.getOrDefault(“SPACY_主机”、“本地主机”);
String port=env.getOrDefault(“SPACY_主机”、“7890”);
字符串url=String.format(“http://%s:%s”、主机、端口);
String content=“你好,世界!”;
URI URI=UriComponentsBuilder.fromHttpUrl(url).queryParam(“content”,content.build().encode().tori();
RestTemplate RestTemplate=新RestTemplate();
String response=restTemplate.getForObject(uri,String.class);
info(“我得到:{}”,响应);
返回响应;
}
@请求映射(“/decode”)
公共布尔解码(){
RestTemplate RestTemplate=新RestTemplate();
消息响应=restTemplate.getForObject(“http://localhost:8080/dumb“,Message.class);
info(“我得到:{}”,响应);
返回Boolean.TRUE;
}
将RestTemplate注入控制器,不要为每个请求创建RestTemplate。问题是您的RestTemplate实例中没有注册的消息转换器。请提供一些代码好吗?我试图扩展这个例子,但没有成功。。。非常感谢。
17:26:34.609 [qtp990226843-17] INFO a.c.j.controllers.ExampleController - Message[content = [Content[dependencies = [Dependency[child = 1, parent = 0, type = PUNCT], Dependency[child = 2, parent = 0, type = NPADVMOD], Dependency[child = 3, parent = 0, type = PUNCT]], tokens = [Token[ent = , index = 0, lemma = hello, pos = INTJ, tag = UH, text = Hello], Token[ent = , index = 1, lemma = ,, pos = PUNCT, tag = ,, text = ,], Token[ent = PERSON, index = 2, lemma = world, pos = PROPN, tag = NNP, text = World], Token[ent = , index = 3, lemma = !, pos = PUNCT, tag = ., text = !]]]], id = 1]
@RequestMapping("/remote")
public Boolean remote() {
RestTemplate restTemplate = new RestTemplate();
Map<String, String> env = System.getenv();
String host = env.getOrDefault("MY_HOST", "localhost");
String port = env.getOrDefault("MY_HOST", "7890");
String url = String.format("http://%s:%s", host, port);
String content = "Hello, World!";
URI uri = UriComponentsBuilder.fromHttpUrl(url).queryParam("content", content).build().encode().toUri();
Message response = REST_TEMPLATE.getForObject(uri, Message.class);
logger.info("I got: {}", response);
return Boolean.TRUE;
}
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri Jun 02 16:53:42 BST 2017
There was an unexpected error (type=Internal Server Error, status=500).
org.springframework.web.util.NestedServletException: Request processing failed;
nested exception is org.springframework.web.client.RestClientException:
Could not extract response: no suitable HttpMessageConverter found for response
type [class ai.context_scout.joshi.model.Message] and content type
[text/plain;charset=utf-8]
@RequestMapping("/dumb")
public String dumb() {
Map<String, String> env = System.getenv();
String host = env.getOrDefault("SPACY_HOST", "localhost");
String port = env.getOrDefault("SPACY_HOST", "7890");
String url = String.format("http://%s:%s", host, port);
String content = "Hello, World!";
URI uri = UriComponentsBuilder.fromHttpUrl(url).queryParam("content", content).build().encode().toUri();
RestTemplate restTemplate = new RestTemplate();
String response = restTemplate.getForObject(uri, String.class);
logger.info("I got: {}", response);
return response;
}
@RequestMapping("/decode")
public Boolean decode() {
RestTemplate restTemplate = new RestTemplate();
Message response = restTemplate.getForObject("http://localhost:8080/dumb", Message.class);
logger.info("I got: {}", response);
return Boolean.TRUE;
}