javamavenspring-mvcxmlhttprequestjaxb

Spring MVC: No converter for [class com.gcu.model.OrderList] with preset Content-Type 'null'


I'm encountering an issue with my Spring MVC application where I'm trying to return an object of type OrderList as an HTTP response, but I'm receiving the following warning message:

WARN 21264 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter for [class com.gcu.model.OrderList] with preset Content-Type 'null']

The OrderList class is a simple POJO annotated with @XmlRootElement and @XmlElement for XML serialization. It contains a list of OrderModel objects.

Here's my OrderList class:

package com.gcu.model;

import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;

import java.util.ArrayList;
import java.util.List;

@XmlRootElement(name = "orders")
public class OrderList {

    private List<OrderModel> orders = new ArrayList<OrderModel>();

    public void setOrders(List<OrderModel> orders) {
        this.orders = orders;
    }

    @XmlElement(name = "order")
    public List<OrderModel> getOrders() {
        return orders;
    }

}

Here are the relevant dependencies

<dependency>
    <groupId>jakarta.xml.bind</groupId>
    <artifactId>jakarta.xml.bind-api</artifactId>
    <version>3.0.0</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.3.3</version>
    <scope>runtime</scope>
</dependency>

here is my REST service implementation

package com.gcu.business;

import org.springframework.web.bind.annotation.RestController;

import com.gcu.model.OrderList;
import com.gcu.model.OrderModel;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@RestController
@RequestMapping("/service")
public class OrdersRestService {

    @Autowired
    private OrdersBusinessInterface service;

    @GetMapping(path = "/getjson", produces = { MediaType.APPLICATION_JSON_VALUE })
    public List<OrderModel> getOrdersAsJson() {
        return service.getOrders();
    }

    @GetMapping(path="/getorder/{id}", produces = { MediaType.APPLICATION_JSON_VALUE })
    public ResponseEntity<?> getOrder(@PathVariable("id") String id)
    {
        try
        {
            OrderModel order = service.getOrder(id);
            if (order == null)
            {
                return new ResponseEntity<>(HttpStatus.NOT_FOUND);
            }
            else
            {
                return new ResponseEntity<>(order, HttpStatus.OK);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @GetMapping(path = "/getxml", produces = { MediaType.APPLICATION_XML_VALUE })
    public OrderList getOrdersAsXml() {
        OrderList orders = new OrderList();
        orders.setOrders(service.getOrders());
        return orders;
    }
}

I've checked my configuration, and it seems that I'm missing a message converter to handle the serialization of OrderList objects into XML format. However, I'm not sure how to properly configure this message converter in my Spring MVC application.

Could someone please provide guidance on how to configure a message converter to handle XML serialization for OrderList objects in Spring MVC? Any help would be greatly appreciated.


Solution

  •         <!-- REST API -->
            <dependency>
                <groupId>jakarta.xml.bind</groupId>
                <artifactId>jakarta.xml.bind-api</artifactId>
                REMOVE THIS <version>3.0.0</version>
            </dependency>
            <dependency>
                <groupId>com.sun.xml.bind</groupId>
                <artifactId>jaxb-impl</artifactId>
                <version>2.3.3</version>
                <scope>runtime</scope>
            </dependency>
    

    after removing the manual version control here I changed the jakarta keywords in my code for javax and it magically worked