Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

no suitable HttpMessageConverter found for response type [com.src.model.UserDTO] and content type [text/html;charset=UTF-8]

first of all, i've already read all questions related with this topic in stackoverflow, but i can't find the solution. I have been working on this for days.

I have this restFull web app built with spring mvc. When i try to retrieve a User i get this error:

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)

The request never reach the server, so the problem isn't the response content-type, because there is no response. So i am loss there. I show here my classes and configs, if some info is missed, please let me know, and i will update the question. Thanks!

Client 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";
}

Server 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);
}

User:

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

Application-context:

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

Dependencies:

   <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>
like image 642
Fernando Vilas Avatar asked Sep 12 '13 19:09

Fernando Vilas


1 Answers

Ok I finally found the problem. The response is actually an html error page generated by a wrong security configuration. Indeed the above code seems to be ok. Thanks!

like image 121
Fernando Vilas Avatar answered Jun 18 '23 15:06

Fernando Vilas