gpt4 book ai didi

java - 使用 spring-boot : Content Negotiation failed! 未找到类型返回值的转换器

转载 作者:行者123 更新时间:2023-12-01 23:50:53 25 4
gpt4 key购买 nike

我正在尝试使用 spring-boot 2.1.9 和 spring REST 在浏览器上以 JSON 格式获取 java POJO 的内容。非常基本的例子!但我得到了这篇文章标题上显示的异常(exception)情况。当我使用 @XmlRootElement 注释 POJO 类时,我在浏览器上获取 XML 内容。但是当我尝试在浏览器上获取 POJO 列表时,我得到与 JSON 相同的错误。为什么内容协商对 XML 部分有效,而对 JSON 完全无效?感谢您的每一个回答。

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Oct 03 15:17:06 CEST 2019
There was an unexpected error (type=Internal Server Error, status=500).
No converter found for return value of type: class com.mayo.vina.restapi4.domain.Limit
org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class com.mayo.vina.restapi4.domain.Limit
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:234)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:181)
at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:82)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:123)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:893)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:798)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)

我已经尝试过 Stackoverflow 中提供的所有答案。使用 jackson-databind 扩展 POM 文件,使用 @ResponseBody、@GetMapping 而不是 @RequestMapping,使用 Produce = "application/json"...没有什么对我有用。

package com.mayo.vina.restapi4;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Restapi4Application {

public static void main(String[] args) {
SpringApplication.run(Restapi4Application.class, args);
}

}
-------------------------------------------------------------------------
package com.mayo.vina.restapi4.controller;

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

import com.mayo.vina.restapi4.domain.Limit;

@RestController
public class LimitController
{

@RequestMapping("/limit")
public Limit limit()
{
return new Limit(1, 10000);
}
}
---------------------------------------------------------------------------
package com.mayo.vina.restapi4.domain;

//@XmlRootElement
public class Limit
{
private int minimum;
private int maximum;

public Limit()
{
}

public Limit(int minimum, int maximum)
{
this.minimum = minimum;
this.maximum = maximum;
}

public int getMinimum()
{
return minimum;
}

public void setMinimum(int minimum)
{
this.minimum = minimum;
}

public int getMaximum()
{
return maximum;
}

public void setMaximum(int maximum)
{
this.maximum = maximum;
}
}
--------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.mayo.vina</groupId>
<artifactId>restapi4</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>restapi4</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

最佳答案

以下解决了我的问题:

所以,是的,只是重申一下,进入我的 Maven 存储库并删除 fastxml 文件夹并重新运行 Maven 就解决了我的问题。

关于java - 使用 spring-boot : Content Negotiation failed! 未找到类型返回值的转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58221237/

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