首先,我已经在stackoverflow中阅读了与此主题相关的所有问题,但我找不到解决方案。我已经做了好几天了。
我有这个用spring mvc构建的restFull网络应用程序。当我尝试检索用户时,我收到此错误:
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [com.src.model.UserDTO] and content type [text/html;charset=UTF-8]
org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessag eConverterExtractor.java:84)
org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:627)
org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:1)
org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:454)
org.springframework.web.client.RestTemplate.execute(RestTemplate.java:409)
org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:385)
com.src.web.controller.HomeController.getHome(HomeController.java:42)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:219)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:100)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:604)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:565)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
请求从不到达服务器,因此问题不是响应内容类型,因为没有响应。所以我在那里失去了。我在这里展示我的课程和配置,如果遗漏了一些信息,请告诉我,我会更新问题。谢谢!
客户控制器:
@Controller
public class HomeController extends GenericAbstractController{
@RequestMapping(value = "/home", method = RequestMethod.GET)
public String getHome(HttpServletRequest request, Model model) {
Principal userPrincipal = request.getUserPrincipal();
List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
acceptableMediaTypes.add(MediaType.APPLICATION_XML);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_XML);
headers.setAccept(acceptableMediaTypes);
HttpEntity<UserDTO> entity = new HttpEntity<UserDTO>(headers);
ResponseEntity<UserDTO> result = restTemplate.exchange("http://localhost:8080/rideon/users/{id}",
HttpMethod.GET, entity, UserDTO.class, userPrincipal.getName());
model.addAttribute("user", result.getBody());
return "home";
}
服务器控制器:
@Controller
public class UsersController {
private static final Logger LOGGER = LoggerFactory.getLogger(UsersController.class);
@Autowired
private UserService userService;
@RequestMapping(value = "users/{id}", method = RequestMethod.GET, headers = "Accept=application/xml")
@ResponseBody
public UserDTO getUser(@PathVariable("id") String id) {
return userService.getUserById(id);
}
用户:
@XmlRootElement
@Entity(name = "users")
public class UserDTO implements Serializable {
@Id
private String email;
private String password;
private String name;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
应用上下文:
<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<bean id="marshallingHttpMessageConverter"
class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="jaxbMarshaller" />
<property name="unmarshaller" ref="jaxbMarshaller" />
</bean>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
<bean class="org.springframework.http.converter.FormHttpMessageConverter" />
<bean class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
<bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter" />
<bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter" />
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.src.model.UserDTO</value>
<value>com.src.model.MultimediaDTO</value>
<value>com.src.model.BicycleDTO</value>
<value>com.src.model.FriendshipRequestDTO</value>
</list>
</property>
</bean>
依赖关系:
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.9</version>
<scope>runtime</scope>
</dependency>
答案 0 :(得分:16)
好的,我终于找到了问题。响应实际上是由错误的安全配置生成的html错误页面。实际上上面的代码似乎没问题。谢谢!