#java #spring #spring-boot #api #rest
Вопрос:
У меня есть эта ошибка:
/Users/mj/Documents/Deploy/Heroku/Gym/src/main/java/app/gym/v1/Model/Domain/Gym.java:51:40 java: incompatible types: cannot infer type arguments for java.util.ArrayListlt;gt; reason: no instance(s) of type variable(s) E exist so that java.util.ArrayListlt;Egt; conforms to java.util.Setlt;app.gym.v1.Model.Domain.Sportgt;
Я не знаю, почему или что не так, я проверяю все дважды каждую строку кода, но чего-то не хватает.
это модель:
package app.gym.v1.Model.Domain; import app.gym.v1.Model.User; import com.fasterxml.jackson.annotation.JsonProperty; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import javax.persistence.*; import java.io.Serializable; import java.util.ArrayList; import java.util.Set; @Entity @Table(name = "gyms") @NoArgsConstructor @AllArgsConstructor @Data public class Gym implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(nullable = false, updatable = false) @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) private Long id; @OneToOne(cascade = CascadeType.ALL) @JoinColumn(name = "user_id", referencedColumnName = "id") private User user; @Column(name = "gym_name") private String gymName; @Column(name = "gym_phone") private String gymPhone; @Column(name = "gym_address") private String gymAddress; @Column(name = "logo", columnDefinition="LONGTEXT") private String logo; @OneToMany(mappedBy="gym") private Setlt;Productgt; products; @OneToMany(mappedBy="gym") private Setlt;Sportgt; sports; public void addSportForGym(Sport sport) { if(getSports()==null) { this.sports = new ArrayListlt;gt;(); } getSports().add(sport); sport.setGym(this); } }
это реализация сервиса:
package app.gym.v1.Utility.Impl; import app.gym.v1.DAO.SportRepo; import app.gym.v1.Model.Domain.Gym; import app.gym.v1.Model.Domain.Sport; import app.gym.v1.Service.SportService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.transaction.Transactional; import java.util.List; @Service @Transactional public class SportServiceImpl implements SportService { private SportRepo sportRepo; @Autowired public SportServiceImpl(SportRepo sportRepo) { this.sportRepo = sportRepo; } @Override public Sport findSportById(Long id) { return null; } @Override public Listlt;Sportgt; findSportForPlayer(Long idPlayer) { return null; } @Override public Listlt;Sportgt; findSportForCaptain(Long idCaptain) { return null; } @Override public Listlt;Sportgt; findSportForGym(Long idGym) { return null; } @Override public Sport addSportForGym(Sport sport, Gym gym) { gym.addSportForGym(sport); return sportRepo.save(sport); } @Override public void addSportForPlayer(Long idSport, Long idPlayer) { } @Override public Sport editSport(Sport sport, Sport existSport) { existSport.setSpName(sport.getSpName()); existSport.setDescription(sport.getDescription()); existSport.setSpLogo(sport.getSpLogo()); return sportRepo.save(existSport); } @Override public void deleteSport(Long id) { Sport existsSport = sportRepo.findById(id).orElse(null); sportRepo.delete(existsSport); } }
this is the service:
package app.gym.v1.Service; import app.gym.v1.Model.Domain.Gym; import app.gym.v1.Model.Domain.Sport; import org.springframework.stereotype.Service; import java.util.List; @Service public interface SportService { Sport findSportById(Long id); Listlt;Sportgt; findSportForPlayer(Long idPlayer); Listlt;Sportgt; findSportForCaptain(Long idCaptain); Listlt;Sportgt; findSportForGym(Long idGym); Sport addSportForGym(Sport sport, Gym gym); void addSportForPlayer(Long idSport, Long idPlayer); Sport editSport(Sport sport, Sport existSport); void deleteSport(Long id); }
и это контроль:
package app.gym.v1.Resource.API; import app.gym.v1.Model.Domain.Gym; import app.gym.v1.Model.Domain.Sport; import app.gym.v1.Service.SportService; import app.gym.v1.Utility.Constant.SwaggerConstant; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping(path = "/sport") @Api(tags = {SwaggerConstant.API_TAG8}) @CrossOrigin(origins = "*") public class SportControl { @Autowired private SportService sportService; @ApiOperation(value = "Finding Sports with id in the gym", notes = "Looking for sport gym with id", response = Sport.class) @ApiResponses({@ApiResponse(responseCode = "200", description = "Looking for specific sport's type in gym with id"), @ApiResponse(responseCode = "500", description = "Successfully register new user"), @ApiResponse(responseCode = "400", description = "The request is malformed or invalid"), @ApiResponse(responseCode = "404", description = "The resource URL was not found on the server"), @ApiResponse(responseCode = "403", description = "You are not authorized. Please authenticate and try again"), @ApiResponse(responseCode = "401", description = "You don't have permission to this resource") }) @GetMapping("/find_sp_by_Id/{id}") Sport findSportById(@PathVariable Long id) { return sportService.findSportById(id); } @ApiOperation(value = "Looking for player in the gym", notes = "Looking for player", response = Sport.class) @ApiResponses({@ApiResponse(responseCode = "200", description = "Looking for specific sport's player in gym"), @ApiResponse(responseCode = "500", description = "Successfully player was founded"), @ApiResponse(responseCode = "400", description = "The request is malformed or invalid"), @ApiResponse(responseCode = "404", description = "The resource URL was not found on the server"), @ApiResponse(responseCode = "403", description = "You are not authorized. Please authenticate and try again"), @ApiResponse(responseCode = "401", description = "You don't have permission to this resource") }) @GetMapping("/find_sp_for_player/{idPlayer}") Listlt;Sportgt; findSportForPlayer(@PathVariable Long idPlayer) { return sportService.findSportForPlayer(idPlayer); } @ApiOperation(value = "Finding Sport in the gym", notes = "Looking for sport gym", response = Sport.class) @ApiResponses({@ApiResponse(responseCode = "200", description = "Looking for specific sport's type in gym"), @ApiResponse(responseCode = "500", description = "Successfully sport founded"), @ApiResponse(responseCode = "400", description = "The request is malformed or invalid"), @ApiResponse(responseCode = "404", description = "The resource URL was not found on the server"), @ApiResponse(responseCode = "403", description = "You are not authorized. Please authenticate and try again"), @ApiResponse(responseCode = "401", description = "You don't have permission to this resource") }) @GetMapping("/find_sp_in_gym/{idGym}") Listlt;Sportgt; findSportForGym(@PathVariable Long idGym) { return sportService.findSportForGym(idGym); } @ApiOperation(value = "Adding sport to the gym", notes = "Adding new sport type to the gym", response = Sport.class) @ApiResponses({@ApiResponse(responseCode = "200", description = "Adding sport type to the gym"), @ApiResponse(responseCode = "500", description = "Successfully sports added to the gym"), @ApiResponse(responseCode = "400", description = "The request is malformed or invalid"), @ApiResponse(responseCode = "404", description = "The resource URL was not found on the server"), @ApiResponse(responseCode = "403", description = "You are not authorized. Please authenticate and try again"), @ApiResponse(responseCode = "401", description = "You don't have permission to this resource") }) @PostMapping("/add_sp_gym/{idGym}") Sport addSportForGym(@RequestBody Sport sport, @RequestBody Gym gym) { return sportService.addSportForGym(sport, gym); } @ApiOperation(value = "Adding Sport for the player", notes = "Adding player to the sport", response = Sport.class) @ApiResponses({@ApiResponse(responseCode = "200", description = "Adding player to the sport"), @ApiResponse(responseCode = "500", description = "Successfully player added"), @ApiResponse(responseCode = "400", description = "The request is malformed or invalid"), @ApiResponse(responseCode = "404", description = "The resource URL was not found on the server"), @ApiResponse(responseCode = "403", description = "You are not authorized. Please authenticate and try again"), @ApiResponse(responseCode = "401", description = "You don't have permission to this resource") }) @PostMapping("/add_to_plauer/{sport_id}/{player_id}") void addSportForPlayer(@PathVariable Long idSport, @PathVariable Long idPlayer) { sportService.addSportForPlayer(idSport, idPlayer); } @ApiOperation(value = "Editing Sport in the gym", notes = "Editing sport in the gym", response = Sport.class) @ApiResponses({@ApiResponse(responseCode = "200", description = "Editing sport in the gym"), @ApiResponse(responseCode = "500", description = "Successfully Editing sport course"), @ApiResponse(responseCode = "400", description = "The request is malformed or invalid"), @ApiResponse(responseCode = "404", description = "The resource URL was not found on the server"), @ApiResponse(responseCode = "403", description = "You are not authorized. Please authenticate and try again"), @ApiResponse(responseCode = "401", description = "You don't have permission to this resource") }) @PutMapping("/edit_sp/{id}") Sport editSport(@RequestBody Sport sport, @RequestBody Sport existSport) { return sportService.editSport(sport, existSport); } @ApiOperation(value = "Deleting Sport in the gym", notes = "Deleting sport in the gym", response = Sport.class) @ApiResponses({@ApiResponse(responseCode = "200", description = "Deleting sport in the gym"), @ApiResponse(responseCode = "500", description = "Successfully Deleted sport course"), @ApiResponse(responseCode = "400", description = "The request is malformed or invalid"), @ApiResponse(responseCode = "404", description = "The resource URL was not found on the server"), @ApiResponse(responseCode = "403", description = "You are not authorized. Please authenticate and try again"), @ApiResponse(responseCode = "401", description = "You don't have permission to this resource") }) @DeleteMapping("/del_sp/{id}") void deleteSport(@PathVariable Long id) { sportService.deleteSport(id); } }
проблема с addSportForGym все проблемы здесь не такие, как в моем коде. Это все время говорит мне, что нет никакого примера
Комментарии:
1. Вы объявляете поле
Setlt;Sportgt; sports
какSetlt;Sportgt;
, но 4 строками ниже пытаетесь инициализировать егоthis.sports = new ArrayListlt;gt;();
. Поскольку классArrayList
не реализуетSet
интерфейс, эта строка не компилируется. Замените его наthis.sports = new HashSetlt;gt;();