I have a Restcontroller with 4 methods, but I can't use the calls, I always get the error: "message": "No static resource api/corp.",
I need method with string like:
- localhost:8090/api/corp?name=PETA
- localhost:8090/api/corp?cnpj=PETA
and PathVariable methods too
Corp.java
package com.priserp.springapipeta.model;
import jakarta.persistence.*;
import java.util.Objects;
@Entity(name = "corp")
public class Corp {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "corp_id")
private long id;
@Column(name = "corp_name")
private String corpName;
@Column(name = "corp_fantasy_name")
private String corpFantasy;
@Column(name = "corp_cnpj")
private String corpCnpj;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getCorpName() {
return corpName;
}
public void setCorpName(String corpName) {
this.corpName = corpName;
}
public String getCorpFantasy() {
return corpFantasy;
}
public void setCorpFantasy(String corpFantasy) {
this.corpFantasy = corpFantasy;
}
public String getCorpCnpj() {
return corpCnpj;
}
public void setCorpCnpj(String corpCnpj) {
this.corpCnpj = corpCnpj;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Corp corp = (Corp) o;
return id == corp.id;
}
@Override
public int hashCode() {
return Objects.hashCode(id);
}
public Corp(){}
}
CorpRepository.java
@Repository
public interface CorpRepository extends JpaRepository<Corp, Long> {
@Query("select c from corp c where c.corpName = :name")
Corp findByCorpName(@Param("name") String corpName);
Corp findById(long id);
Corp findByCorpCnpj(String corpCnpj);
}
CorpController.java
@RestController
@RequestMapping("/api/corp")
public class CorpController {
private final CorpRepository corpRepository;
@Autowired
public CorpController(CorpRepository repository) {
this.corpRepository = repository;
}
@GetMapping("/corps")
public ResponseEntity<List<Corp>> getAllCorps() {
System.out.println("listando aqui " +corpRepository.findAll() );
List<Corp> corps = corpRepository.findAll();
if (corps.isEmpty()) {
return ResponseEntity.noContent().build();
}
return ResponseEntity.ok(corps);
}
@GetMapping("/{id}")
public ResponseEntity<Corp> getCorpById(@PathVariable Long id) {
System.out.println("by id " + corpRepository.findById(id).get() );
Optional<Corp> corp = corpRepository.findById(id);
return corp.map(ResponseEntity::ok)
.orElseGet(() -> ResponseEntity.notFound().build());
}
@GetMapping(value = "/{name}")
public ResponseEntity<Corp> getCorpByName(@RequestParam(name = "name") String corpName) {
System.out.println("corpname " + corpName);
Corp corp = corpRepository.findByCorpName(corpName);
return ResponseEntity.ok(corp);
}
@GetMapping("/{cnpj}")
public ResponseEntity<Corp> getCorpByCnpj(@RequestParam(name = "cnpj") String corpCnpj) {
System.out.println("corpCnpj " + corpCnpj);
Corp corp = corpRepository.findByCorpCnpj(corpCnpj);
return ResponseEntity.ok(corp);
}
}
@GetMapping(value = "/byname/{name}")and@GetMapping("/bycnpj/{cnpj}")or define a single endpoint that receives a parameter and inside the method you call one or the other based on an IF condition./bycnpj/1234change the endpoint and test it like that.