【问题标题】:Pass JSON string in REST URL在 REST URL 中传递 JSON 字符串
【发布时间】:2013-10-14 13:50:57
【问题描述】:

第一次使用 REST 并尝试在 REST 请求 URL 中将 JSON 字符串作为路径变量发送,我正在做什么以从客户端发送请求:

JSONObject json = new JSONObject();
try {
    json.put("Test", "123");
    json.put("tracks", "1234");
    jj = json.toString();
} catch (JSONException e1) {
    // TODO Auto-generated catch block
       e1.printStackTrace();
}

   String url =  "http://localhost:8090/webApp/restresource/"+jj+".do";
   st = (String) restTemplate.postForObject(url,jj, String.class, jj);

休息服务器:

@RequestMapping(value = "/restresource/{code}", method=RequestMethod.POST, consumes= "application/json")
public @ResponseBody String useJson(@PathVariable String jj) {

    String result;

    JSONObject jObject = new JSONObject();
    try {
        result = jObject.getJSONObject(jj).toString();

    } catch (JSONException jse) {
        jse.printStackTrace();
    }

return result;

}

我得到的例外是not enough variable values to expand "Test"

编辑:我的请求网址如下所示:

http://localhost:8090/app/restResource/%7B%22%22:%22123%22,%22tracks%22:%221234%22%7D.do

甚至看到了 HttpClientErrorException!任何帮助表示赞赏!

【问题讨论】:

  • 你为什么要使用 JSON 作为 GET 参数传输机制呢?另外,请注意,您在发送时添加了“.do”后缀,但您的 RequestMapping 模式不包含它,因此整个字符串将被解析为 JSON,从而导致异常。另外,看起来你没有分享你的真实代码,因为你甚至没有在 useJson 中使用 jj 变量。
  • 在测试时,我没有使用.do,当我在url末尾添加它时失败了它通过显示结果。使用jj,刚刚编辑

标签: java rest spring-mvc


【解决方案1】:

我会做的是:

HttpEntity<Object> entity = new HttpEntity<Object>(json); // Create HTTP Entity to post to rest endpoint
restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());
restTemplate.postForEntity(url, entity, Object.class);

在您的服务器端,您不需要将其作为 PathVariable 访问,而是可以使用

@RequestMapping(value = "/restresource", method=RequestMethod.POST, consumes= "application/json")
public @ResponseBody String useJson(@RequestBody JSonObject object) {

因此spring框架自动将json字符串转换为指定的对象类型。 您可以相应地调整您的网址,然后重试。

【讨论】:

  • 用于获取消息转换器异常,但在配置文件中添加后它就消失了。
  • 我有所有的 jars 注释、数据绑定和核心,但它仍然说,找不到 objectMapper。不是包含在核心中吗?
  • 你在类路径中有这个依赖吗 org.codehaus.jacksonjackson-mapper-asl1.8.5
  • 2.2.1 jackson core 支持这个吗?当我添加 1.9.6 mapper-acl 时,它说缺少 org.codehaus.jackson.version!
  • 这是我现在得到的Could not write request: no suitable HttpMessageConverter found for request type [org.json.JSONObject] 配置文件有这个:&lt;bean id="restTemplate" class="org.springframework.web.client.RestTemplate"&gt; &lt;property name="messageConverters"&gt; &lt;array&gt; &lt;bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" &gt; &lt;property name="supportedMediaTypes" value="application/json" /&gt; &lt;/bean&gt; &lt;/array&gt; &lt;/property&gt; &lt;/bean&gt;
【解决方案2】:

你可以在下面使用

 <bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
    <property name="messageConverters">
        <list>     
             <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
        </list>
    </property>
</bean>



<dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.7.1</version>
</dependency>

<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>org.springframework.web</artifactId>
        <version>3.1.1.RELEASE</version>
</dependency>

【讨论】:

    猜你喜欢
    • 2020-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-30
    • 1970-01-01
    • 2014-02-20
    • 2016-01-11
    • 1970-01-01
    相关资源
    最近更新 更多