编写spring.io REST控制器的正确方法是什么?

时间:2016-01-03 02:47:33

标签: java spring spring-mvc

我是一个非常新的春天,并尝试启动和运行webapp。我可以提供静态材料,但很难获得与数据库的连接。我肯定在做一些事情,也许有些事情是错误的。

我的代码

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

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

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

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

}

2 个答案:

答案 0 :(得分:3)

Spring错误消息中明确说明了这个问题: Failed to instantiate [com.parrit.WorkspaceController]: No default constructor found;

这意味着Spring期望你的WorkspaceController对象定义一个无参数的构造函数,但它没有。

答案 1 :(得分:3)

看起来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接口创建代理实例。 通过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