gpt4 book ai didi

java - 编写 spring.io REST Controller 的正确方法是什么?

转载 作者:行者123 更新时间:2023-12-01 07:52:45 25 4
gpt4 key购买 nike

我对 Spring 很陌生,正在尝试启动并运行一个 Web 应用程序。我可以提供静态 Material ,但很难连接到数据库。我肯定做错了一些事情,也许还有几件事是错误的。

我的代码

package com.parrit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.parrit.models.Workspace;
import com.parrit.models.WorkspaceRepository;

@RestController
@RequestMapping("/workspace")
public class WorkspaceController {

private final WorkspaceRepository repository;

@Autowired
public WorkspaceController(WorkspaceRepository repository) {
Assert.notNull(repository, "Repository must not be null");
this.repository = repository;
}

@RequestMapping(method = RequestMethod.POST)
void save( @RequestBody String workspaceHTML) {
Workspace ws = new Workspace();
ws.setHTML(workspaceHTML);
repository.save(ws);
}
}

这个想法是让一个 Controller 在有人进入保存路径时保存一个新的存储库对象。我做错了什么/我可以去哪里了解我做错了什么。

我收到的错误消息这是一个例子:

Error creating bean with name 'workspaceController' defined in file [PROJECT_ROOT/target/classes/com/parrit/WorkspaceController.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.parrit.WorkspaceController]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.parrit.WorkspaceController.<init>()

我有那个类,并且我认为我在 bean 搜索路径中有那个类,但我真的不太确定。

这是该类,以防万一有东西

package com.parrit.models;

import org.springframework.data.jpa.repository.JpaRepository;

public interface WorkspaceRepository extends JpaRepository<Workspace, Long> {

}

最佳答案

看起来 Spring 需要一个无参构造函数......尝试将 @Autowired 注释从构造函数移动到属性并仔细检查 WorkspaceRepository 是否是 Spring bean。

@RestController
@RequestMapping("/workspace")
public class WorkspaceController {

@Autowired
private WorkspaceRepository repository;

@RequestMapping(method = RequestMethod.POST)
void save( @RequestBody String workspaceHTML) {
Workspace ws = new Workspace();
ws.setHTML(workspaceHTML);
repository.save(ws);
}
}

并检查此线程:http://forum.spring.io/forum/spring-projects/container/97753-how-to-autowire-a-bean-without-default-constructor

更新:

设置 Spring 为这些 JPA Repo 接口(interface)创建代理实例。通过 JavaConfig:

import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@EnableJpaRepositories
class Config {}

或通过 XML 配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

<jpa:repositories base-package="com.parrit.models"/>

</beans>

参见http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods

关于java - 编写 spring.io REST Controller 的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34572837/

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