I am new to Spring boot. I am having a problem with the below method to which support to get the raw by the name:
@RestController
@RequiredArgsConstructor
@RequestMapping("/api")
public class AccountsResource {
private final AccountsService service;
@GetMapping("/accounts/{name}")
public ResponseEntity<?> getByName(@PathVariable String name){
return ResponseEntity.ok(service.getByUsername(name));
}
}
This is my AccountsService class:
public Accounts getByUsername(String username) {
Optional<Accounts> byUsername = repository.findByUsername(username);
return byUsername.orElseThrow(() -> new RuntimeException("User not found"));
}
and this is my Repository class for Acccounts:
@Repository
public interface AccountsRepository extends JpaRepository<Accounts, Long> {
@Query(value = "SELECT * FROM accounts where name = ?", nativeQuery = true)
Optional<Accounts> findByUsername(String username);
}
This is my Entity class:
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "accounts")
public class Accounts extends BaseEntity {
private String name;
private String website;
private String pContact;
@ManyToOne
@JoinColumn(name="sales_rep_id", referencedColumnName = "id")
private SalesRep sales;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "accounts")
private List<Orders> orders;
@OneToMany(mappedBy = "account", cascade = CascadeType.ALL)
private List<WebEvents> webEvents;
}
when I try to send the GET request with http://localhost:8081/api/accounts/1, I am getting:
{
"timestamp": "2021-02-09T12:36:02.943+00:00",
"status": 500,
"error": "Internal Server Error",
"message": "",
"path": "/api/accounts/1"
}
With the IlligalStateException as follows:
java.lang.IllegalStateException: Ambiguous handler methods mapped for '/api/accounts/1': {public org.springframework.http.ResponseEntity com.example.demo.web.rest.AccountsResource.getById(java.lang.Long), public org.springframework.http.ResponseEntity com.example.demo.web.rest.AccountsResource.getByName(java.lang.String)}
I hope to get any suggestions on this regard. As I am new to this I appreciate any valid suggestions and links.