gpt4 book ai didi

rest - 找不到响应类型为[com.src.model.UserDTO]和内容类型为[text/html; charset = UTF-8]的HttpMessageConverter

转载 作者:行者123 更新时间:2023-12-04 13:19:41 25 4
gpt4 key购买 nike

首先,我已经阅读了stackoverflow中与此主题相关的所有问题,但是我找不到解决方案。我已经为此工作了好几天。

我有这个用spring mvc构建的restFull Web应用程序。当我尝试检索用户时,出现此错误:

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)

请求 永远不会到达服务器,因此问题不在于响应内容类型,因为没有响应。所以我在那里迷失了。我在这里显示我的类(class)和配置,如果缺少一些信息,请告诉我,我将更新问题。谢谢!

客户端 Controller :
@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 :
@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>

最佳答案

好的,我终于找到了问题。响应实际上是由错误的安全配置生成的html错误页面。实际上,上面的代码似乎还可以。谢谢!

关于rest - 找不到响应类型为[com.src.model.UserDTO]和内容类型为[text/html; charset = UTF-8]的HttpMessageConverter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18772771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com