私は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サービスツールで自分のサービスをテストするときしかし、私はエラーが発生します。
応答:
{"timestamp":1464066878392,"status":405,"error":"Method Not Allowed","exception":"org.springframework.web.HttpRequestMethodNotSupportedException","message":"Request method 'POST' not supported","path":"/users"}
問題は何ですか?
私はリポジトリがAutowired'、ないコンストラクタ@ 'すべきだと思います。なぜRepoが既にAutowiredのときにUserServiceImplのコンストラクタが必要なのでしょうか? – cst1992
'@ EnableAutoConfiguration'の代わりに' @ SpringBootApplication'を使用してください。それがなければコンポーネントのスキャンや豆の検出はありません。 –