【问题标题】:How to fetch data using RestTemplate in Spring如何在 Spring 中使用 RestTemplate 获取数据
【发布时间】:2017-06-30 09:08:32
【问题描述】:

我的任务是编写代码,通过这样的 URL 从 REST API 获取数据

curl -k -v -XPOST -H "username: password" -H "Content-type: application/json" http://localhost:8181/api/rest/person/query -d '
    {
        "name": "person's name",
        "role": "role code",
        "programme": "programme code"
    }'

这是我的代码

   public JsonUser fetchJsonUserByName(String name) throws IOException{
       String jsonName = "'{'name': "+"'"+name+"'}'";
       String url = "/person/query -d "+jsonName;
       ResponseEntity<JsonUser> response = restTemplate.getForEntity(url, JsonUser.class);
           try {
            if(response.hasBody()) return response.getBody();
               else return null;
        } catch (HttpStatusCodeException e) {
            if(e.getStatusCode() == HttpStatus.NOT_FOUND) return null;
            throw new IOException("error fetching user", e);
        } catch (Exception e) {
            throw new IOException("error fetching user", e);
        }
   }

当我运行我的代码时,我得到了错误代码 500,我哪里错了?

【问题讨论】:

    标签: json resttemplate spring-web


    【解决方案1】:

    首先,这个问题中没有关于Spring Integration 的内容。请注意以后的标签选择。

    你在这里的错误是你错过了你在 curl 中使用POST HTTP 方法的部分。但是对于RestRemplate,您选择GET

    /**
     * Retrieve an entity by doing a GET on the specified URL.
     * The response is converted and stored in an {@link ResponseEntity}.
     * <p>URI Template variables are expanded using the given URI variables, if any.
     * @param url the URL
     * @param responseType the type of the return value
     * @param uriVariables the variables to expand the template
     * @return the entity
     * @since 3.0.2
     */
    <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables) throws RestClientException;
    

    我猜你的意思是这样的:

    /**
     * Create a new resource by POSTing the given object to the URI template,
     * and returns the response as {@link ResponseEntity}.
     * <p>URI Template variables are expanded using the given URI variables, if any.
     * <p>The {@code request} parameter can be a {@link HttpEntity} in order to
     * add additional HTTP headers to the request.
     * @param url the URL
     * @param request the Object to be POSTed (may be {@code null})
     * @param uriVariables the variables to expand the template
     * @return the converted object
     * @since 3.0.2
     * @see HttpEntity
     */
    <T> ResponseEntity<T> postForEntity(String url, Object request, Class<T> responseType, Object... uriVariables)
            throws RestClientException;
    

    【讨论】:

    • 感谢您的回复。接下来的问题我会小心的。
    • 顺便说一句,-d 是 curl 选项。如果通过RestTemplate 发帖,您必须将您的jsonName 移动到request 参数中。现在您的网址不正确
    • 再次感谢您提供的有用信息。我也猜到了我的网址的原因。
    • 对吗?! ResponseEntity response = restTemplate.postForEntity(url,JsonUser.class,responseType,jsonName);
    • 不,您的 jsonName 是请求正文,而不是赞歌。更多HTTP协议请阅读
    【解决方案2】:

    请咨询HTTP response codes, explained,在那里你会看到 500 的意思是“我搞砸了”,这是服务器的观点。这是服务器错误,不是你的。

    当然,服务器错误可能是由于输入格式错误造成的,例如您发送了完全错误的 URL /person/query -d '{'name': 'name'}'

    【讨论】:

    • 我已经在终端中使用命令手动测试过,服务器仍然可以正常使用此输入类型 '{'name': 'name'}' 。很难理解为什么代码不起作用。
    • 问题不在于它是错误的参数,而是您将它放在URL而不是正文中。另一个答案充分说明了这一点。
    猜你喜欢
    • 2021-08-10
    • 1970-01-01
    • 2017-10-30
    • 2018-04-05
    • 1970-01-01
    • 2016-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多