I have rephrased the question and will explain it in more detail. I'm sorry and thank you. This is how the system is in spring boot java. The java I'm using is the 11. As for the system, it goes up. It communicates with the database. However, I can't make the system work. It gives me the answer from the undefined method and I don't understand what I'm getting wrong. Here you have many answers about the undefined method, but none that I have looked for is similar to my error.
My resource:
@RestController
@RequestMapping("/api/cidade")
public class CidadeResource {
@Autowired
private CidadeRepository repository;
@Autowired
private EstadoRepository repositoryEstado;
@Autowired
private CidadeResourceMapper mapper;
@GetMapping(produces = { MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<PageDto<CidadeDTO>> getPageWithQuery(@QuerydslPredicate(root = Cidade.class) Predicate predicate, Pageable pageble) {
return ResponseEntity.ok(convertToPageDto(predicate, pageble));
}
@PostMapping
public ResponseEntity <CidadeDTO> add(@Valid @RequestBody CidadeWriteDTO dto) throws Exception {
Cidade entity = this.mapper.fromDto(dto);
entity = repository.save(entity);
ResponseEntity <CidadeDTO> re = ResponseEntity.ok(this.convertToDto(entity));
return re;
}
@GetMapping(value = "/{id}")
public ResponseEntity <CidadeDTO> get(@PathVariable Integer id) {
return ResponseEntity.ok(convertToDto(repository.findById(id).get()));
}
@PutMapping(value = "/{id}")
public ResponseEntity <CidadeDTO> update(@Valid @RequestBody CidadeWriteDTO dto, @PathVariable Integer id) throws AppException {
Optional <Cidade> entity = repository.findById(id);
if (entity.isPresent()) {
Double qtdPopulacao = 0 D;
if (entity.get().getEstado().getPopulacao() == null)
qtdPopulacao = entity.get().getPopulacao();
else {
// Remove a população da cidade (antes da alteração)
qtdPopulacao = entity.get().getEstado().getPopulacao() - entity.get().getPopulacao();
}
this.mapper.merge(entity.get(), dto);
// Adiciona a população da cidade
qtdPopulacao += dto.getPopulacao();
// Atualiza os dados no estado
entity.get().getEstado().setPopulacao(qtdPopulacao);
// Salva a entidade no BD
repositoryEstado.save(entity.get().getEstado());
if (dto.getPopulacao() < 0)
throw new AppException("Valor inválido da quantidade da população");
Cidade mergedEntity = repository.save(entity.get());
ResponseEntity < CidadeDTO > re = ResponseEntity.ok(this.convertToDto(mergedEntity));
return re;
} else {
throw new RuntimeException();
}
}
@DeleteMapping(value = "/{id}")
public void delete(@PathVariable Integer id) throws Exception {
repository.deleteById(id);
}
private PageDto <CidadeDTO> convertToPageDto(Predicate predicate, Pageable pageble) {
Page <Cidade> pageEntity = repository.findAll(predicate, pageble);
return new PageDto<>(StreamSupport.stream(pageEntity.spliterator(), false)
.map(this::convertToDto)
.collect(Collectors.toList()), pageEntity.getTotalElements());
}
private CidadeDTO convertToDto(Cidade entity) {
return mapper.toDto(entity);
}
}
Thanks for wanting to help me, sorry for the delay in answering, but come on! In the STS console, it appears as "started", and yes, it works on sending the data to the bank and so on and these errors appear:
Mapped "{[/api/cidade],methods=[GET],produces=[application/json]}" onto public org.springframework.http.ResponseEntity<br.com.inteligate.entrevista.teste.base.page.dto.PageDto<br.com.inteligate.entrevista.teste.rs.cidade.CidadeDTO>> br.com.inteligate.entrevista.teste.rs.cidade.CidadeResource.getPageWithQuery(com.querydsl.core.types.Predicate,org.springframework.data.domain.Pageable)
Mapped "{[/api/cidade],methods=[POST]}" onto public org.springframework.http.ResponseEntity<br.com.inteligate.entrevista.teste.rs.cidade.CidadeDTO>br.com.inteligate.entrevista.teste.rs.cidade.CidadeResource.add(br.com.inteligate.entrevista.teste.rs.cidade.CidadeWriteDTO) throws java.lang.Exception
Mapped "{[/api/cidade/{id}],methods=[GET]}" onto public org.springframework.http.ResponseEntity<br.com.inteligate.entrevista.teste.rs.cidade.CidadeDTO> br.com.inteligate.entrevista.teste.rs.cidade.CidadeResource.get(java.lang.Integer)
Mapped "{[/api/cidade/{id}],methods=[PUT]}" onto public org.springframework.http.ResponseEntity<br.com.inteligate.entrevista.teste.rs.cidade.CidadeDTO> br.com.inteligate.entrevista.teste.rs.cidade.CidadeResource.update(br.com.inteligate.entrevista.teste.rs.cidade.CidadeWriteDTO,java.lang.Integer) throws br.com.inteligate.entrevista.teste.exceptions.AppException
Mapped "{[/api/cidade/{id}],methods=[DELETE]}" onto public void br.com.inteligate.entrevista.teste.rs.cidade.CidadeResource.delete(java.lang.Integer) throws java.lang.Exception
Mapped "{[/api/estado],methods=[GET],produces=[application/json]}" onto public org.springframework.http.ResponseEntity<br.com.inteligate.entrevista.teste.base.page.dto.PageDto<br.com.inteligate.entrevista.teste.rs.estado.EstadoDTO>> br.com.inteligate.entrevista.teste.rs.estado.EstadoResource.getPageWithQuery(com.querydsl.core.types.Predicate,org.springframework.data.domain.Pageable)
Mapped "{[/api/estado],methods=[POST]}" onto public org.springframework.http.ResponseEntity<br.com.inteligate.entrevista.teste.rs.estado.EstadoDTO> br.com.inteligate.entrevista.teste.rs.estado.EstadoResource.add(br.com.inteligate.entrevista.teste.rs.estado.EstadoDTO) throws java.lang.Exception
Mapped "{[/api/estado/{id}],methods=[GET]}" onto public org.springframework.http.ResponseEntity<br.com.inteligate.entrevista.teste.rs.estado.EstadoDTO> br.com.inteligate.entrevista.teste.rs.estado.EstadoResource.get(java.lang.Integer)
Mapped "{[/api/estado/{id}],methods=[PUT]}" onto public org.springframework.http.ResponseEntity<br.com.inteligate.entrevista.teste.rs.estado.EstadoDTO> br.com.inteligate.entrevista.teste.rs.estado.EstadoResource.update(br.com.inteligate.entrevista.teste.rs.estado.EstadoDTO,java.lang.Integer) throws java.lang.Exception
Mapped "{[/api/estado/{id}],methods=[DELETE]}" onto public void br.com.inteligate.entrevista.teste.rs.estado.EstadoResource.delete(java.lang.Integer) throws java.lang.Exception
Mapped "{[/api/pais],methods=[GET],produces=[application/json]}" onto public org.springframework.http.ResponseEntity<br.com.inteligate.entrevista.teste.base.page.dto.PageDto<br.com.inteligate.entrevista.teste.rs.pais.PaisDTO>> br.com.inteligate.entrevista.teste.rs.pais.PaisResource.getPageWithQuery(com.querydsl.core.types.Predicate,org.springframework.data.domain.Pageable)
Mapped "{[/api/pais],methods=[POST]}" onto public org.springframework.http.ResponseEntity<br.com.inteligate.entrevista.teste.rs.pais.PaisDTO> br.com.inteligate.entrevista.teste.rs.pais.PaisResource.add(br.com.inteligate.entrevista.teste.rs.pais.PaisDTO) throws java.lang.Exception
Mapped "{[/api/pais/{id}],methods=[GET]}" onto public org.springframework.http.ResponseEntity<br.com.inteligate.entrevista.teste.rs.pais.PaisDTO> br.com.inteligate.entrevista.teste.rs.pais.PaisResource.get(java.lang.Integer)
Mapped "{[/api/pais/{id}],methods=[PUT]}" onto public org.springframework.http.ResponseEntity<br.com.inteligate.entrevista.teste.rs.pais.PaisDTO> br.com.inteligate.entrevista.teste.rs.pais.PaisResource.update(br.com.inteligate.entrevista.teste.rs.pais.PaisDTO,java.lang.Integer) throws java.lang.Exception
Mapped "{[/api/pais/{id}],methods=[DELETE]}" onto public void br.com.inteligate.entrevista.teste.rs.pais.PaisResource.delete(java.lang.Integer) throws java.lang.Exception
Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletRespons)
Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
[36ms.w.s.m.m.a.RequestMappingHandlerAdapter Looking for
@ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@7902b05f: startup date [Fri Nov 27 10:57:12 BRT 2020]; root of context hierarchy[36mo.j.s.b.sql.JaversSqlAutoConfiguration detected Hibernate dialect: MySQL5Dialect
When I try to do the front, be it in the city, state or country, in addition to these errors above, they appear in the console of the Sts:
1st error
Cannot infer type arguments for PageDto<>
] with root cause
java.lang.Error: Unresolved compilation problem:
Cannot infer type arguments for PageDto<>
2nd error:
2020-11-27 11:05:14.849 ERROR 14582 --- [nio-8081-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.Error: Unresolved compilation problem:
Cannot infer type arguments for PageDto<>
] with root cause
java.lang.Error: Unresolved compilation problem:
Cannot infer type arguments for PageDto<>
3rd error:
2020-11-27 11:05:21.319e[0;39m e[31mERRORe[0;39m e[35m14582e[0;39m e[2m---e[0;39m e[2m[nio-8081-exec-3]e[0;39m e[36mo.a.c.c.C.[.[.[/].[dispatcherServlet] e[0;39m e[2m:e[0;39m Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.Error: Unresolved compilation problems:
The method getId() is undefined for the type PaisDTO
The method getNome() is undefined for the type PaisDTO
The method getSigla() is undefined for the type PaisDTO
The method getEstados() is undefined for the type PaisDTO
] with root cause
java.lang.Error: Unresolved compilation problems:
The method getId() is undefined for the type PaisDTO
The method getNome() is undefined for the type PaisDTO
The method getSigla() is undefined for the type PaisDTO
The method getEstados() is undefined for the type PaisDTO
And in the front part I'm using Angular. in version 7, and the errors in the console and in the network of my browser appear like this and also in postman:
{timestamp: 1606485361742, status: 500, error: "Internal Server Error",…}
error: "Internal Server Error"
message: "Unresolved compilation problem: ↵ Cannot infer type arguments for PageDto<>↵"
path: "/api/cidade/"
status: 500
timestamp: 1606485361742
Of course, I understand that the server is not communicating with the front. But, initially, as for the verse, that's what I'm not seeing. And I've been looking for a lot of explanations and I haven't seen anything similar. That's why I'm here asking for help. Please don't misjudge me. I'm learning how to use this tool and I want to become a helper like you one day. And I want to make it clear that I'm not being lazy, I'm looking for answers in various places and by various means. I am very grateful to all those who can help me.
Brothers, these are the classes that are not letting my system run in full steam:
EstadoResource.java:
@RestController
@RequestMapping("/api/estado")
public class EstadoResource {
@Autowired
private EstadoRepository repository;
@Autowired
private EstadoResourceMapper mapper;
@Autowired
private EstadoService service;
@GetMapping(produces = { MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<PageDto<EstadoDTO>> getPageWithQuery(@QuerydslPredicate(root = Estado.class ) Predicate predicate, Pageable pageble) {
return ResponseEntity.ok(convertToPageDto(predicate, pageble));
}
@GetMapping(value = "/{id}")
public ResponseEntity<EstadoDTO> get(@PathVariable Integer id) {
return ResponseEntity.ok(convertToDto(repository.findById(id).get()));
}
@PostMapping
public ResponseEntity<EstadoDTO> add(@Valid @RequestBody EstadoDTO dto) throws Exception {
Estado entity = this.mapper.fromDto(dto);
service.validateAdd(dto, entity);
entity = repository.save(entity);
ResponseEntity<EstadoDTO> re = ResponseEntity.ok(this.convertToDto(entity));
return re;
}
@PutMapping(value = "/{id}")
public ResponseEntity<EstadoDTO> update(@Valid @RequestBody EstadoDTO dto, @PathVariable Integer id) throws Exception {
Optional<Estado> entity = repository.findById(id);
if (entity.isPresent()) {
this.mapper.merge(entity.get(), dto);
service.validateUpdate(dto, entity.get());
Estado mergedEntity = repository.save(entity.get());
ResponseEntity<EstadoDTO> re = ResponseEntity.ok(this.convertToDto(mergedEntity));
return re;
} else {
throw new RuntimeException();
}
}
@DeleteMapping(value = "/{id}")
public void delete(@PathVariable Integer id) throws Exception {
service.validateDelete(id);
repository.deleteById(id);
}
private PageDto<EstadoDTO> convertToPageDto(Predicate predicate, Pageable pageble) {
Page<Estado> pageEntity = repository.findAll(predicate, pageble);
return new PageDto<>(StreamSupport.stream(pageEntity.spliterator(), false).map(this::convertToDto)
.collect(Collectors.toList()), pageEntity.getTotalElements());
}
private EstadoDTO convertToDto(Estado entity) {
return mapper.toDto(entity);
}
}
the error is: Cannot infer type arguments for PageDto<> And is highlighted in: return new PageDto<>(StreamSupport.stream(pageEntity.spliterator(), false).map(this::convertToDto) .collect(Collectors.toList()), pageEntity.getTotalElements());
The second is in class CidadeResourceMapperImp1.java:
@Mapper(componentModel = "spring")
public abstract class CidadeResourceMapperImp1 extends CidadeResourceMapper {
@Autowired
private EstadoRepository repository;
@Mapping(target = "estado", expression = "java(getEstado(dto.getEstado(), entity.getEstado()))")
abstract void merge(@MappingTarget Cidade entity, CidadeWriteDTO dto);
public Estado getEstado(EstadoWirteDTO estadoWirteDTO, Estado mappingTarget) {
return repository.findById(estadoWirteDTO.getId()).get();
}
}
the error is: The method getId() is undefined for the type EstadoWirteDTO And is highlighted in:
public Estado getEstado(EstadoWirteDTO estadoWirteDTO, Estado mappingTarget) {
return repository.findById(estadoWirteDTO.getId()).get();
Now in the package estado:
EstadoResource.java:
@RestController
@RequestMapping("/api/estado")
public class EstadoResource {
@Autowired
private EstadoRepository repository;
@Autowired
private EstadoResourceMapper mapper;
@Autowired
private EstadoService service;
@GetMapping(produces = { MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<PageDto<EstadoDTO>> getPageWithQuery(@QuerydslPredicate(root = Estado.class ) Predicate predicate, Pageable pageble) {
return ResponseEntity.ok(convertToPageDto(predicate, pageble));
}
@GetMapping(value = "/{id}")
public ResponseEntity<EstadoDTO> get(@PathVariable Integer id) {
return ResponseEntity.ok(convertToDto(repository.findById(id).get()));
}
@PostMapping
public ResponseEntity<EstadoDTO> add(@Valid @RequestBody EstadoDTO dto) throws Exception {
Estado entity = this.mapper.fromDto(dto);
service.validateAdd(dto, entity);
entity = repository.save(entity);
ResponseEntity<EstadoDTO> re = ResponseEntity.ok(this.convertToDto(entity));
return re;
}
@PutMapping(value = "/{id}")
public ResponseEntity<EstadoDTO> update(@Valid @RequestBody EstadoDTO dto, @PathVariable Integer id) throws Exception {
Optional<Estado> entity = repository.findById(id);
if (entity.isPresent()) {
this.mapper.merge(entity.get(), dto);
service.validateUpdate(dto, entity.get());
Estado mergedEntity = repository.save(entity.get());
ResponseEntity<EstadoDTO> re = ResponseEntity.ok(this.convertToDto(mergedEntity));
return re;
} else {
throw new RuntimeException();
}
}
@DeleteMapping(value = "/{id}")
public void delete(@PathVariable Integer id) throws Exception {
service.validateDelete(id);
repository.deleteById(id);
}
private PageDto<EstadoDTO> convertToPageDto(Predicate predicate, Pageable pageble) {
Page<Estado> pageEntity = repository.findAll(predicate, pageble);
return new PageDto<>(StreamSupport.stream(pageEntity.spliterator(), false).map(this::convertToDto)
.collect(Collectors.toList()), pageEntity.getTotalElements());
}
private EstadoDTO convertToDto(Estado entity) {
return mapper.toDto(entity);
}
}
the error is: Cannot infer type arguments for PageDto<> And is highlighted in: return new PageDto<>(StreamSupport.stream(pageEntity.spliterator(), false).map(this::convertToDto) .collect(Collectors.toList()), pageEntity.getTotalElements()); }
Now in the package pais na classe:
PaisResource.java:
@RestController
@RequestMapping("/api/pais")
public class PaisResource {
@Autowired
private PaisRepository repository;
@Autowired
private PaisResourceMapper mapper;
@Autowired
private PaisService service;
@GetMapping(produces = { MediaType.APPLICATION_JSON_VALUE })
public ResponseEntity<PageDto<PaisDTO>> getPageWithQuery(@QuerydslPredicate(root = Pais.class ) Predicate predicate, Pageable pageble) {
return ResponseEntity.ok(convertToPageDto(predicate, pageble));
}
@PostMapping
public ResponseEntity<PaisDTO> add(@Valid @RequestBody PaisDTO dto) throws Exception {
Pais entity = this.mapper.fromDto(dto);
service.validateAdd(dto, entity);
entity = repository.save(entity);
ResponseEntity<PaisDTO> re = ResponseEntity.ok(this.convertToDto(entity));
return re;
}
@GetMapping(value = "/{id}")
public ResponseEntity<PaisDTO> get(@PathVariable Integer id) {
return ResponseEntity.ok(convertToDto(repository.findById(id).get()));
}
@PutMapping(value = "/{id}")
public ResponseEntity<PaisDTO> update(@Valid @RequestBody PaisDTO dto, @PathVariable Integer id) throws Exception {
Optional<Pais> entity = repository.findById(id);
if (entity.isPresent()) {
this.mapper.merge(entity.get(), dto);
service.validateUpdate(dto, entity.get());
Pais mergedEntity = repository.save(entity.get());
ResponseEntity<PaisDTO> re = ResponseEntity.ok(this.convertToDto(mergedEntity));
return re;
} else {
throw new RuntimeException();
}
}
@DeleteMapping(value = "/{id}")
public void delete(@PathVariable Integer id) throws Exception {
service.validateDelete(id);
repository.deleteById(id);
}
private PageDto<PaisDTO> convertToPageDto(Predicate predicate, Pageable pageble) {
Page<Pais> pageEntity = repository.findAll(predicate, pageble);
return new PageDto<>(StreamSupport.stream(pageEntity.spliterator(), false).map(this::convertToDto)
.collect(Collectors.toList()), pageEntity.getTotalElements());
}
private PaisDTO convertToDto(Pais entity) {
return mapper.toDto(entity);
}
}
the error is: Cannot infer type arguments for PageDto<> And is highlighted in: return new PageDto<>(StreamSupport.stream(pageEntity.spliterator(), false).map(this::convertToDto) .collect(Collectors.toList()), pageEntity.getTotalElements()); }
And finally, in the package services:
the error is in PaisServiceImp.java:
@Service
public class PaisServiceImp implements PaisService {
@Autowired
private PaisRepository repository;
@Override
public void validateDelete(Integer id) throws AppException {
Optional<Pais> pais = repository.findById(id);
if (!pais.isPresent())
throw new AppException("País não encontratro");
Long qtdEstados = repository.getQtdEstados(id);
if (qtdEstados > 0)
throw new AppException("Esse país possui um estado vinculado, favor desvincular primeiro");
}
@Override
public void validateUpdate(@Valid PaisDTO dto, Pais entity) throws AppException {
Optional<Pais> pais = repository.findByNome(dto.getNome());
if (pais.isPresent()) {
if (!entity.getId().equals(pais.get().getId()))
throw new AppException("Já existe um país com esse nome");
}
}
@Override
public void validateAdd(@Valid PaisDTO dto, Pais entity) throws AppException {
Optional<Pais> pais = repository.findByNome(dto.getNome());
if (pais.isPresent())
throw new AppException("Já existe um país com esse nome");
}
}
if you can help me thank you.
I uninstalled Lombok and reinstalled. Then I switched to the version you are using in the POM and also added as annotations Getter and Setter in the classes in question and it worked!