Spring MVC:没有预设内容类型为“null”的 [class com.gcu.model.OrderList] 转换器

问题描述 投票:0回答:1

我的 Spring MVC 应用程序遇到问题,我尝试返回

OrderList
类型的对象作为 HTTP 响应,但收到以下警告消息:

WARN 21264 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver:已解决[org.springframework.http.converter.HttpMessageNotWritableException:没有带有预设内容类型的[class com.gcu.model.OrderList]转换器'空']

OrderList
类是一个简单的POJO,带有用于XML序列化的
@XmlRootElement
@XmlElement
注释。它包含
OrderModel
对象的列表。

这是我的

OrderList
课程:

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

}

以下是相关依赖项

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

这是我的 REST 服务实现

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

我检查了我的配置,似乎缺少一个消息转换器来处理 OrderList 对象序列化为 XML 格式。但是,我不确定如何在我的 Spring MVC 应用程序中正确配置此消息转换器。

有人可以提供有关如何配置消息转换器来处理 Spring MVC 中 OrderList 对象的 XML 序列化的指导吗?任何帮助将不胜感激。

java maven spring-mvc xmlhttprequest jaxb
1个回答
0
投票
        <!-- 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>

在此处删除手动版本控制后,我更改了 javax 代码中的 jakarta 关键字,它神奇地工作了

© www.soinside.com 2019 - 2024. All rights reserved.