Issue
I'm working on a project client/server and I'm using spring boot and angular.
So I have a form and i want to take data from the input fields and send it to the back-end, my database ( mySQL ) but the problem is it only adds null fields in my database. I used a tutorial from devglen as inspiration and some tutorials from angular.io
Form input example:
<div class="form-group">
<label for="body">Body:</label>
<input type="text" class="form-control" id="body"
[ngModel]="article?.body" (ngModelChange)="article.body = $event" name="body">
</div>
Model class for the article i want to add:
export class Article {
id: string;
title: string;
abstract_art: string;
writer: string;
body: string;
}
My component for adding:
@Component({
selector: 'app-add',
templateUrl: './add-article.component.html'
})
export class AddArticleComponent {
article: Article = new Article();
writers: Writer[];
constructor(private router: Router, private articleService: ArticleService) {
}
createArticle(): void {
console.log(this.article);
this.articleService.createArticle( this.article).subscribe( data => { alert('Article created successfully.');
});
console.log('function called!');
}
get diagnostic() { return JSON.stringify(this.article); }
}
The service class:
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json',
'Authorization': 'my-auth-token'})
};
@Injectable()
export class ArticleService {
constructor(private http: HttpClient) {}
// private userUrl = 'http://localhost:8080/articles';
private articleUrl = '/api';
public getArticles() {
return this.http.get<Article[]>(this.articleUrl);
}
public deleteArticle(article) {
return this.http.delete(this.articleUrl + '/' + article.id, httpOptions);
}
public createArticle(article) {
// const art = JSON.stringify(article);
console.log(article);
return this.http.post<Article>(this.articleUrl, article);
}
}
And now for the back-end. Article Class
@Entity
@Getter @Setter
@NoArgsConstructor
@ToString @EqualsAndHashCode
@Table(name="article")
public class Article {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(name="title")
private String title;
@Column(name="abstract_art")
private String abstract_art;
@Column(name="writer")
private String writer;
@Column(name="body")
private String body;
public Article(String title,String abstract_art, String writer, String body) {
this.title = title;
this.body = body;
this.abstract_art = abstract_art;
this.writer = writer;
}
}
The repository :
@RepositoryRestResource
//@CrossOrigin(origins = "http://localhost:4200")
public interface ArticleRepository extends JpaRepository<Article,Integer> {
}
The article service:
@Service
public class ArticleServiceImpl implements ArticleService {
@Autowired
private ArticleRepository repository;
@Override
public Article create(Article article) {
return repository.save(article);
}
@Override
public Article delete(int id) {
Article article = findById(id);
if(article != null){
repository.delete(article);
}
return article;
}
@Override
public List<Article> findAll() {
return repository.findAll();
}
@Override
public Article findById(int id) {
return repository.getOne(id);
}
@Override
public Article update(Article art) {
return null;
}
}
And the controller:
@RestController
@RequestMapping({"/api"})
public class ArticleController {
@Autowired
private ArticleService article;
//Get all articles
@GetMapping
public List<Article> listAll(){
return article.findAll();
}
// Create a new Article
//@PostMapping
@PostMapping
public Article createArticle(Article art) {
return article.create(art);
}
// Get a Single Article
@GetMapping(value="/{id}")
public Article getArticleById(@PathVariable("id") int id ){
return article.findById(id);
}
// Delete a Note /art/
@DeleteMapping(value = "/{id}")
public void deleteArticle(@PathVariable("id") int id) {
article.delete(id);
}
@PutMapping
public Article update(Article user){
return article.update(user);
}
}
Additional information: I can get data from database and I can delete data from database.
Btw it's my first post so i'm sorry if i've missed some guidelines for posting. Thank you in advance for your answers. Have a good one!
Solution
@RestController is a convenience annotation that does nothing more than adding the @Controller and @ResponseBody annotations as well as allows the class to accept the requests that are sent to its path
@DOCS
@ResponseBody
annotation tells a controller that the object returned is automatically serialized into JSON and passed back into the HttpResponse object.
@RequestBody
annotation maps the HttpRequest body to a transfer or domain object, enabling automatic deserialization of the inbound HttpRequest body onto a Java object.
You missed @RequestBody
@RequestBody
marks that the Article input is retrieved from the body/content of the POST request. This is a notable difference between GET
and POST
as the GET
request does not contain a body.
Modified code
@PostMapping
public Article createArticle(@RequestBody Article art) {
return article.create(art);
}
Answered By - Vikas
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.