简体   繁体   English

Spring Rest客户端实现:无法提取响应:没有为xstreamMarshaller找到适合的响应类型的HttpMessageConverter

[英]Spring Rest Client implementation : Could not extract response: no suitable HttpMessageConverter found for response type with xstreamMarshaller

I am developing client server application using spring mvc and rest. 我正在使用spring mvc和rest开发客户端服务器应用程序。 Its simple calculator service in which client calls the methods from server to execute operations. 它简单的计算器服务,客户端从服务器调用方法来执行操作。

This is my rest client code restClient.java: 这是我的休息客户端代码restClient.java:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.blog.samples.client;

/**
 *
 * @author bhushan.baviskar
 */
import com.blog.samples.domain.Calculator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.client.RestTemplate;

public class restClient {

  public static void main(String [] args)
  {
      restClient tmp = new restClient();
      tmp.calltoserver();
  }
  public void calltoserver() {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("../../../../appContext.xml", restClient.class);
    RestTemplate restTemplate = applicationContext.getBean("restTemplate", RestTemplate.class);
    String url = "http://localhost:8080/rest/calc/4&3&+";
    Calculator calObj = (Calculator) restTemplate.getForObject(url, Calculator.class);
    System.out.println("details " + calObj.getDetails());
    System.out.println("done");
  }
}

And this is my appContext.xml file : 这是我的appContext.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd">

  <bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
        <property name="messageConverters">
      <bean id="messageConverter" class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
        <property name="marshaller" ref="xstreamMarshaller" />
        <property name="unmarshaller" ref="xstreamMarshaller" />
      </bean>
    </property>
  </bean>

  <bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">
    <property name="aliases">
    <props>
      <prop key="Calculator">com.blog.samples.webservices.rest.CalcController</prop>
    </props>
    </property>
  </bean>

</beans>

I am getting response in json format but when I execute the restclient.java file it says : 我正在以json格式获得响应,但是当我执行restclient.java文件时,它说:

DEBUG: [Dec-11 16:54:39,706] web.client.RestTemplate - GET request for "http://localhost:8080/rest/calc/4&3&+" resulted in 200 (OK)

Exception in thread "main" org.springframework.web.client.RestClientException: **Could not extract response: no suitable HttpMessageConverter found for response type** [com.blog.samples.domain.Calculator] and content type [text/plain;charset=UTF-8]
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:84)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:446)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:401)
    at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:199)
    at com.blog.samples.client.restClient.calltoserver(restClient.java:27)
    at com.blog.samples.client.restClient.main(restClient.java:21)
------------------------------------------------------------------------

I am new to the Spring Rest client Development so any help will be appreciated. 我是Spring Rest客户端开发的新手,所以任何帮助都将不胜感激。

If anyone knows pl. 如果有人知道pl。 tell me How to handle the response? 告诉我如何处理回复?

This is because the MappingJacksonHttpMessageConverter is not registered to your restTemplate. 这是因为MappingJacksonHttpMessageConverter未注册到restTemplate。 By default all types of MessageConverters present in your classpath will be registered. 默认情况下,将在类路径中存在所有类型的MessageConverter。

You should either remove property messageConverters for bean restTemplate in you xml to have default messageconverters or you have to add MappingJacksonHttpMessageConverter to your messageConverters list in your xml. 您应该在xml中删除bean restTemplate的属性messageConverters以使用默认的messageconverters,或者必须将MappingJacksonHttpMessageConverter添加到xml中的messageConverters列表中。

Hope this helps 希望这可以帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android版Spring-无法提取响应:找不到适合响应类型的HttpMessageConverter - Spring for Android - Could not extract response: no suitable HttpMessageConverter found for response type RestClientException无法提取响应:找不到合适的HttpMessageConverter作为响应类型 - RestClientException Could not extract response: no suitable HttpMessageConverter found for response type 无法提取响应:找不到合适的HttpMessageConverter - Could not extract response: no suitable HttpMessageConverter found 休息模板:无法提取响应:当我们得到 xml 响应时,没有找到适合响应类型的 HttpMessageConverter,未绑定到 JAVA 对象 - Rest Template:Could not extract response: no suitable HttpMessageConverter found for response type when we got xml response, not bind to JAVA object 无法提取响应:没有找到适合响应类型 class 和内容类型 [text/html] 的 HttpMessageConverter - Could not extract response: no suitable HttpMessageConverter found for response type class and content type [text/html] 没有找到适合响应类型的 HttpMessageConverter - no suitable HttpMessageConverter found for response type 找不到适合响应类型和内容类型的HttpMessageConverter - no suitable HttpMessageConverter found for response type and content type 没有为响应类型找到合适的HttpMessageConverter - Custom RestTemplate - No suitable HttpMessageConverter found for response type - Custom RestTemplate 无法提取响应:jaxb2marshaller没有合适的HttpMessageConverter - Could not extract response: no suitable HttpMessageConverter with jaxb2marshaller 找不到适合响应类型[类com.avada.rest.UsersController $ Users]的HttpMessageConverter - no suitable HttpMessageConverter found for response type [class com.avada.rest.UsersController$Users]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM