2016-05-24 7 views
-1

私はspring bootとspring mvcで新しいサービスを作成します。私は単純なspring mvcサービスのテストでエラーが発生しました

UserEntity.class:

@Entity 
@Table(name = "users") 
public class UserEntity { 

    private long id; 
    private String username; 
    private String password; 
    private boolean active; 
    private boolean login; 

    public UserEntity(UserDto dto) { 
     this.id = dto.getId(); 
     this.username = dto.getUsername(); 
     this.password = dto.getPassword(); 
     this.active = dto.isActive(); 
    } 

    // getters&setters... 
} 

UserDto.class:

public class UserDto { 

    private long id; 
    private String username; 
    private String password; 
    private boolean active; 

    public UserDto(long id, String username, String password, boolean active) { 
     this.id = id; 
     this.username = username; 
     this.password = password; 
     this.active = active; 
    } 
    // getters&setters... 
} 

UserRepository:

@Repository 
public interface UserRepository extends JpaRepository<UserEntity, Long> { 

} 

UserServiceImpl.class:(とUserServiceのインタフェース)

@Service 
@Transactional 
public class UserServiceImpl implements UserService { 

    private final UserRepository repo; 

    @Autowired 
    public UserServiceImpl(UserRepository repo) { 
     this.repo = repo; 
    } 

    @Override 
    public boolean saveUser(UserDto dto) { 
     UserEntity user = new UserEntity(dto); 
     repo.save(user); 
     return true; 
    } 
} 

UserController.class:

@RestController 
public class UserController { 

    private final UserService service; 

    @Autowired 
    public UserController(UserService service) { 
     this.service = service; 
    } 

    @RequestMapping(value = "/users", method = RequestMethod.POST) 
    public void createUser(@RequestBody UserDto userDto) { 
     service.saveUser(userDto); 
    } 

} 

Application.class:

@EnableAutoConfiguration 
public class Application { 

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

} 

私の春ブートプロジェクトが正常に起動します。私はIntelliJのテストRESTfulなWebサービスツールで自分のサービスをテストするときしかし、私はエラーが発生します。

enter image description here

応答:

{"timestamp":1464066878392,"status":405,"error":"Method Not Allowed","exception":"org.springframework.web.HttpRequestMethodNotSupportedException","message":"Request method 'POST' not supported","path":"/users"} 

enter image description here

問題は何ですか?

+0

私はリポジトリがAutowired'、ないコンストラクタ@ 'すべきだと思います。なぜRepoが既にAutowiredのときにUserServiceImplのコンストラクタが必要なのでしょうか? – cst1992

+1

'@ EnableAutoConfiguration'の代わりに' @ SpringBootApplication'を使用してください。それがなければコンポーネントのスキャンや豆の検出はありません。 –

答えて

0

私の提案は、UserControllerクラスとUserServiceImplクラスからコンストラクタを削除することです。そのための必要はありません。代わりに、宣言に@Autowired注釈を割り当てます。また、私はあなたがそれらを作る必要はないと思う。final

UserServiceImpl.class:

@Service 
@Transactional 
public class UserServiceImpl implements UserService { 

    @Autowired 
    private UserRepository repo; 

    @Override 
    public boolean saveUser(UserDto dto) { 
     UserEntity user = new UserEntity(dto); 
     repo.save(user); 
     return true; 
    } 
} 

UserController.class:

@RestController 
public class UserController { 

    @Autowired  
    private UserService service; 

    public UserController(UserService service) { 
     this.service = service; 
    } 

    @RequestMapping(value = "/users", method = RequestMethod.POST) 
    public void createUser(@RequestBody UserDto userDto) { 
     service.saveUser(userDto); 
    } 
} 
+1

フィールド注入よりもコンストラクタ注入が好ましい。また、一方から他方に移動しても問題は解決しません。 –

関連する問題