Java 在RESTURL中传递JSON字符串

Java 在RESTURL中传递JSON字符串,java,rest,spring-mvc,Java,Rest,Spring Mvc,第一次使用REST并尝试将JSON字符串作为REST请求URL中的路径变量发送,我要做的是从客户端发送请求: JSONObject json = new JSONObject(); try { json.put("Test", "123"); json.put("tracks", "1234"); jj = json.toString(); } catch (JSONException e1) { // TODO Auto-generated catch bloc

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

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);
Rest服务器:

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

}
我得到的例外是
变量值不足,无法展开“Test”

编辑:我的请求url如下所示:

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

甚至看到了HttpClientErrorException!感谢您的帮助

我要做的是:

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);
因此,spring框架会自动将json字符串转换为指定的对象类型。
您可以相应地调整URL并重试。

您可以使用下面的

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

org.codehaus.jackson
杰克逊地图绘制者
1.7.1
org.springframework
org.springframework.web
3.1.1.1发布

用于获取消息转换器异常,但在配置文件中添加此异常后,它消失了。我有所有JAR注释、数据绑定和核心,但仍然显示“找不到objectMapper”。它不是包含在core中吗?在classpath org.codehaus.jackson映射器asl 1.8.5中是否有此依赖项2.2.1 jackson core是否支持此依赖项?当我添加1.9.6 mapper acl时,它说org.codehaus.jackson.version丢失了!这就是我现在得到的
无法写入请求:没有为请求类型[org.json.JSONObject]找到合适的HttpMessageConverter
配置文件有以下内容:
为什么要使用json作为GET参数传输机制?另外,请注意,在发送时添加了“.do”后缀,但RequestMapping模式不包含该后缀,因此整个字符串将被解析为JSON,从而导致异常。另外,看起来您没有共享真正的代码,因为您甚至没有在
useJson
中使用
jj
变量。在测试时,我使用了不带
。do
的变量,但在url末尾添加该变量以显示结果时失败了。使用刚刚编辑的
jj
 <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>