0

我正在尝试使用RESTful Web Servicewith Spring,我的参考代码是本指南中的参考代码。我正在询问的服务返回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": "!" } ]
    } ]
}

我创建了以下对象(为简洁起见,省略了它们的所有方法):

@JsonIgnoreProperties(ignoreUnknown = true)
public class Token {
    private String ent;
    private int index;
    private String lemma;
    private String pos;
    private String tag;
    private String text;
}

@JsonIgnoreProperties(ignoreUnknown = true)
public class Dependency {
    private int parent;
    private String type;
    private int child;
}

@JsonIgnoreProperties(ignoreUnknown = true)
public class Content {
    private List<Token> tokens;
    private List<Dependency> dependencies;
}

@JsonIgnoreProperties(ignoreUnknown = true)
public class Message {
    private long id;
    private List<Content> content;
}

使用以下代码,我可以成功反序列化一个Message对象:

@RequestMapping("/message")
public String message() {
    return "{\"content\": [{\"dependencies\": [{\"child\": 1,\"type\": \"PUNCT\",\"parent\": 0},{\"child\": 2,\"type\": \"NPADVMOD\",\"parent\": 0},{\"child\": 3,\"type\": \"PUNCT\",\"parent\": 0}],\"tokens\": [{\"index\": 0,\"text\": \"Hello\",\"tag\": \"UH\",\"lemma\": \"hello\",\"pos\": \"INTJ\",\"ent\": \"\"},{\"index\": 1,\"text\": \",\",\"tag\": \",\",\"lemma\": \",\",\"pos\": \"PUNCT\",\"ent\": \"\"},{\"index\": 2,\"text\": \"World\",\"tag\": \"NNP\",\"lemma\": \"world\",\"pos\": \"PROPN\",\"ent\": \"PERSON\"},{\"index\": 3,\"text\": \"!\",\"tag\": \".\",\"lemma\": \"!\",\"pos\": \"PUNCT\",\"ent\": \"\"}]}],\"id\": 1}";
}

@RequestMapping("/decode")
public Boolean decode() {
    RestTemplate restTemplate = new RestTemplate();

    Message response = restTemplate.getForObject("http://localhost:8080/message", Message.class);
    logger.info("I got: {}", response);

    return Boolean.TRUE;
}

正如我在日志中看到的:

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 Service使用以下代码直接连接到:

@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]

我在网上和这个网站上搜索以找到解决方案,但我想不出任何能带来有意义结果的关键字......有什么帮助吗?

为什么我会收到此错误? 如何解决?

谢谢!

PS:我只是尝试实现一个dumb service连接到该remote service服务并将该服务接收的JSON作为字符串返回的a,并且我修改了decode service已经能够反序列化JSON以从该dumb service而不是从该中获取数据remote service并且它可以工作!我大吃一惊!这没有意义!无论如何,作为参考,代码如下(请告诉我是否可以避免使用dumb service):

@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;
}
4

0 回答 0