0
Hello, I’m doing a CRUD with springboot but I’m having a mistake in only one line, and I would like to know how to solve, because I started studying the tool recently, thank you!!
ERROR: Inferred type’S' for type Parameter’S' is not Within its bound; should extend 'com.matera.blog.model.Post'
Below are the codes:
Blogapplication.java (Main class):
package com.matera.blog.blog;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class BlogApplication {
public static void main(String[] args) {
SpringApplication.run(BlogApplication.class, args);
}
}
Postcontroller.java (Controller.):
package com.matera.blog.controller;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;
import com.matera.blog.model.Post;
import com.matera.blog.service.PostService;
@Controller
public class PostController {
@Autowired
private PostService service;
@GetMapping("/")
public ModelAndView findAll() {
ModelAndView mv = new ModelAndView("/post");
mv.addObject("posts", service.findAll());
return mv;
}
@GetMapping("/add")
public ModelAndView add(Post post) {
ModelAndView mv = new ModelAndView("/postAdd");
mv.addObject("post", post);
return mv;
}
@GetMapping("/edit/{id}")
public ModelAndView edit(@PathVariable("id") Long id) {
return add(service.findOne(id));
}
@GetMapping("/delete/{id}")
public ModelAndView delete(@PathVariable("id") Long id) {
service.delete(id);
return findAll();
}
@PostMapping("/save")
public ModelAndView save(@Valid Post post, BindingResult result) {
if(result.hasErrors()) {
return add(post);
}
service.save(post);
return findAll();
}
}
Post.java (Model.):
package com.matera.blog.model;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.format.annotation.DateTimeFormat;
@Entity(name = "tbl_post")
public class Post implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name = "post_seq", sequenceName = "post_seq")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "post_seq")
private Long id;
@Column(nullable = false, length = 50)
@NotBlank(message = "Autor é uma informação obrigatória.")
private String autor;
@Column(nullable = false, length = 150)
@NotBlank(message = "Título é uma informação obrigatória.")
private String titulo;
@Column(nullable = false, length = 2000)
@NotBlank(message = "Texto é uma informação obrigatória.")
private String texto;
@Column(nullable = false)
@Temporal(TemporalType.DATE)
@DateTimeFormat(pattern = "yyyy-MM-dd")
@NotNull(message = "Data é uma informação obrigatória.")
private Date data;
}
Postrepository.java (Repository.):
package com.matera.blog.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.matera.blog.model.Post;
@Repository
public interface PostRepository extends JpaRepository<Post, Long> {
void delete(Long id);
}
Postservice.java (Service):
package com.matera.blog.service.;
import com.matera.blog.model.Post;
import com.matera.blog.repository.PostRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class PostService {
@Autowired
private PostRepository repository;
public List<Post> findAll() {
return repository.findAll();
}
public Post findOne(Long id) {
return repository.findOne(id);
}
public Post save(Post post) {
return repository.saveAndFlush(post);
}
public void delete(Long id) {
repository.delete(id);
}
}
depending on the version you are using I believe you should replace findOne with findById
– Lucas Miranda
findOne
was migrated forfindById
– renanvm
Thank you very much, guys!!
– user143767