Pinwheeler
Pinwheeler

Reputation: 1111

What is the right way to write a spring.io REST controller?

I'm very new to spring and trying to get a webapp up and running. I can serve the static material, but am having a hard time getting a connection to a database. I'm definitely doing something and perhaps several things wrong.

My code

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

The idea is to have a controller that saves a new repository object anytime someone goes to the save route. What am I doing wrong/where can I go to learn what I am doing wrong.

Error message that I'm getting Here's an example :

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

I have that class, and I think that I have that class in the bean search paths but I really am not super sure.

Here is that class in case it's something there

package com.parrit.models;

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

public interface WorkspaceRepository extends JpaRepository<Workspace, Long> {

}

Upvotes: 0

Views: 180

Answers (2)

spork
spork

Reputation: 1265

The issue is clearly stated in the Spring error message: Failed to instantiate [com.parrit.WorkspaceController]: No default constructor found;

This means Spring expects your WorkspaceController object to define a no-arg constructor, but it does not.

Upvotes: 3

Manuel Vieda
Manuel Vieda

Reputation: 296

Looks that Spring require a non-argument constructor.... try to move the @Autowired annotation from the constructor to the attribute and double check that WorkspaceRepository is a 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);
    }
}

And check this thread: http://forum.spring.io/forum/spring-projects/container/97753-how-to-autowire-a-bean-without-default-constructor

Update:

Set up Spring to create proxy instances for those JPA Repo interfaces. Either via JavaConfig:

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

@EnableJpaRepositories
class Config {}

or via XML configuration:

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

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

Upvotes: 3

Related Questions