inflearn logo
강의

Course

Instructor

[Concepts are easy, Practice is intense] Spring Boot, Introduction!

19강 JSON create 메서드 질문이여!!

Resolved

543

pododo [DEL] 250325223720

0 asked

1

package com.example.firstproject.api;

import com.example.firstproject.dto.ArticleForm;
import com.example.firstproject.entity.Article;
import com.example.firstproject.repository.ArticleRepository;
// import lombok.AllArgsConstructor;
// import lombok.NoArgsConstructor;
// import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ArticleApiController {

private ArticleRepository articleRepository;
private ArticleForm dto;

@GetMapping("/api/articles")
public List<Article> index() {
return articleRepository.findAll();
}

@GetMapping("/api/articles/{id}")
public Article index(@PathVariable Long id) {
return articleRepository.findById(id).orElse(null);
}

@PostMapping("/api/articles")
public Article create(@RequestBody ArticleForm dto) {
Article article = dto.toEntity();
return articleRepository.save(article);
}
}

 

PostMapping하고 있는데, Talend API Tester에서 아래와 같이 실행시키면 안돼여

에러메시지는 아래와 같습니다

Cannot invoke "com.example.firstproject.repository.ArticleRepository.save(Object)" because "this.articleRepository" is null

라고 떠여... 그래서 ArticleRepository를 확인해보면

ArticleRepository는

package com.example.firstproject.repository;

import com.example.firstproject.entity.Article;
import org.springframework.data.repository.CrudRepository;
import java.util.ArrayList;

public interface ArticleRepository extends CrudRepository<Article, Long> {
@Override
ArrayList<Article> findAll();
}

으로 썜이랑 똑같이 작성했어요

 

쌤.... 제가 뭘 잘못 했나여............뭘 고치면 되나여......... 살려주십셔.,,,,,,,,,,,,,.

spring-boot restapi json createmethod 안돼여

Answer 1

1

hongpark

@Autowired를 리파지터리에 적용해야쥬?

0

pododo [DEL] 250325223720

ㅎㅎㅎㅎ 감사함다!

23강 댓글 생성 메소드 오류

0

94

2

15.14 강 에러 문제 질문

0

63

1

21강 자율 과제중 update

0

135

2

28강 DB mysql연동 관련 질문

0

291

1

27강 관련 질문

0

209

1

22강 관련질문

0

315

2

21강 create테스트 질문

0

361

1

9강 질문 있습니다

0

293

1

로그창에 내용이 출력이 안됩니다.

0

370

1

index()함수 질문있습니다.

0

369

3

수정완료를 눌렀을때 첫번째 데이터는 전송이 안되고 다시 수정완료 버튼을 눌러야 데이터가 전송 됩니다.

0

484

1

강의와 책 질문

0

438

1

20강 질문있습니다.

0

401

1

20강에서 Article create 부분

0

292

1

16강 질문있습니다.

0

267

1

mustache의 article의 정의가 뭔가요?

0

342

2

17강 Unique index or primary key violation

1

1839

2

8강 에러 질문있습니다.

0

274

1

15강 DB연결

0

344

1

추상클래스를 사용 할 수있을까요?

0

245

1

22강 과제 질문

0

375

1

22강 질문입니다.

0

371

1

19강 create POST 500 에러

0

458

1

15강 data.sql코드가 작동하지 않습니다.

1

665

2