gpt4 book ai didi

java - Spring Boot Rest Controller 使用 Jackson 和 Hibernate 将 POJO 转换为 JSON 时出错

转载 作者:行者123 更新时间:2023-12-02 08:43:26 25 4
gpt4 key购买 nike

所以这个问题已经被问过好几次了,我在 SO 和其他问题网站上看到了类似的答案。答案似乎总是要么将 Jackson 依赖项放在类路径中,要么确保 POJO 类具有公共(public) getter(我已经尝试过,但无法摆脱此错误消息)。我有一个连接到 MySQL 数据库的 Spring Boot 应用程序,我几乎没有任何配置,因为我想看看 Spring Boot 可以自动配置到什么程度。我在 pom.xml 中有几个 Spring Boot 启动器,我假设它们会处理依赖项?当我测试请求路径时,我得到堆栈跟踪。

No converter found for return value of type: class com.portfolio.bork.webapp.model.Project
org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class com.portfolio.bork.webapp.model.Project
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:230)
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:124)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:879)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:793)
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)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:367)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1639)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)

控制台中显示一条错误消息,

.w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class com.portfolio.bork.webapp.model.Project]
.w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class java.util.LinkedHashMap]

这是我的 pom.xml

<?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.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.portfolio.bork</groupId>
<artifactId>webapp</artifactId>
<version>01</version>
<packaging>war</packaging>
<name>webapp</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-data-jpa</artifactId>
</dependency>
<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-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<!-- <exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions> -->
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!-- <scope>runtime</scope> -->
</dependency>

<!-- for testing as of now -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>8.5.27</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.3</version>
<scope>test</scope>
</dependency>
</dependencies>

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

</project>

我的 Controller :

package com.portfolio.bork.webapp.restcontroller;

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

import com.portfolio.bork.webapp.services.db.ProjectDao;
import com.portfolio.bork.webapp.model.Project;


@RestController
public class ProjectRestController {

private ProjectDao projectDao;

// get project
@RequestMapping(value = "/project/{projectId}", method = RequestMethod.GET )
public Project getProjectById( @PathVariable String projectId) {
Long projectIdLong = Long.parseLong(projectId);
return projectDao.getProjectById(projectIdLong);
}


// for constructor injection
public ProjectRestController(ProjectDao projectDao) {
this.projectDao = projectDao;
}

public void setProjectDao(ProjectDao projectDao) {
this.projectDao = projectDao;
}

}

父类Project.java,

package com.portfolio.bork.webapp.model;

import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table (name = "project")
public class Project{

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column (name = "p_id")
private Long id;
@Column( name="p_title" )
private String title;
@Column( name = "p_date_modified")
private String dateModified;
@OneToMany(mappedBy = "project")
private List<ContentBlock> contentBlocks;

public Project() {
}


public Project(Long id, String title, String dateModified, List<ContentBlock> contentBlocks) {
this.id = id;
this.title = title;
this.dateModified = dateModified;
this.contentBlocks = contentBlocks;
}

public Long getId() {
return this.id;
}

public void setId(Long id) {
this.id = id;
}

public String getTitle() {
return this.title;
}

public void setTitle(String title) {
this.title = title;
}

public String getDateModified() {
return this.dateModified;
}

public void setDateModified(String dateModified) {
this.dateModified = dateModified;
}

public List<ContentBlock> getContentBlocks() {
return this.contentBlocks;
}

public void setContentBlocks(List<ContentBlock> contentBlocks) {
this.contentBlocks = contentBlocks;
}

@Override
public String toString() {
return "{" +
" id='" + getId() + "'" +
", title='" + getTitle() + "'" +
", dateModified='" + getDateModified() + "'" +
"}";
}


}

child 类,

package com.portfolio.bork.webapp.model;

import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table (name = "project")
public class Project{

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column (name = "p_id")
private Long id;
@Column( name="p_title" )
private String title;
@Column( name = "p_date_modified")
private String dateModified;
@OneToMany(mappedBy = "project")
private List<ContentBlock> contentBlocks;

public Project() {
}


public Project(Long id, String title, String dateModified, List<ContentBlock> contentBlocks) {
this.id = id;
this.title = title;
this.dateModified = dateModified;
this.contentBlocks = contentBlocks;
}

public Long getId() {
return this.id;
}

public void setId(Long id) {
this.id = id;
}

public String getTitle() {
return this.title;
}

public void setTitle(String title) {
this.title = title;
}

public String getDateModified() {
return this.dateModified;
}

public void setDateModified(String dateModified) {
this.dateModified = dateModified;
}

public List<ContentBlock> getContentBlocks() {
return this.contentBlocks;
}

public void setContentBlocks(List<ContentBlock> contentBlocks) {
this.contentBlocks = contentBlocks;
}

@Override
public String toString() {
return "{" +
" id='" + getId() + "'" +
", title='" + getTitle() + "'" +
", dateModified='" + getDateModified() + "'" +
"}";
}


}

最佳答案

您明确告诉 Maven 在运行应用程序时不要包含提供转换器的 jackson-databind,因为您说它仅用于 测试。消除全部依赖; spring-boot-starter-web 将自动包含它。

关于java - Spring Boot Rest Controller 使用 Jackson 和 Hibernate 将 POJO 转换为 JSON 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61239863/

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