Я все еще новичок в использовании весны. Я получаю выше ошибки, когда пытался внедрить свой интерфейс Mapper в свой сервисный уровень. Не уверен, где это могло быть неправильно, с тех пор, как я аннотировал свой интерфейс Mapper с одним: < /p>
a) Mapper (componentmodel = "Spring")
b) Компонент
c) ComponentsCan
Однако кажется, что эта ошибка. com.example.demoforlearning; < /p>
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication
@EntityScan("Entity")
@ComponentScan({"Controller","Service","Repository"})
@EnableJpaRepositories("Repository")
@EnableAutoConfiguration
public class DemoForLEarningApplication {
public static void main(String[] args) {
SpringApplication.run(DemoForLEarningApplication.class, args);
}
}
< /code>
Contoller Layer: < /p>
package Controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import DTO.UserDTO;
import Entity.UserEntity;
import Repository.UserRepository;
import Service.UserServices;
@RestController
@RequestMapping("/api/user")
public class UserController {
private final UserServices userServices;
private final UserRepository userRepository;
@Autowired
public UserController(UserServices userServices, UserRepository userRepository){
this.userServices = userServices;
this.userRepository = userRepository;
}
@GetMapping("/viewAllUsers")
public ResponseEntity viewAllUsers(){
List allUsers= userServices.viewAllUsers();
return new ResponseEntity(allUsers,HttpStatus.OK);
}
@PostMapping("/addNewUser")
public ResponseEntity createNewUser(@RequestBody UserDTO userDTO)
{
if(userRepository.existsByEmail(userDTO.getEmail()))
{
return new ResponseEntity(HttpStatus.LOCKED); //semak balik
}
UserEntity newUserCreated = userServices.createNewUser(userDTO);
return new ResponseEntity(newUserCreated, HttpStatus.OK);
}
@DeleteMapping("/deleteByEmail")
public ResponseEntity deleteByEmail(@RequestParam String email)
{
userServices.deleteuserbyEmail(email);
return new ResponseEntity(HttpStatus.OK);
}
@PutMapping("/update")
public ResponseEntity updateUserEmail(@RequestParam String currentEmail, @RequestParam String newEmail){
UserEntity updatedUserEmail = userServices.updateUserEmail(currentEmail, newEmail);
return new ResponseEntity(updatedUserEmail,HttpStatus.OK);
}
}
< /code>
Service Layer: < /p>
package Service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import DTO.UserDTO;
import DataForMap.UserMappingOperation;
import Entity.UserEntity;
import Repository.UserRepository;
@Service
public class UserServices {
@Autowired
UserMappingOperation userMappingOperation;
private final UserRepository userRepository;
@Autowired
public UserServices(UserRepository userRepository) { //constructor injection
this.userRepository=userRepository;
}
public List viewAllUsers(){
/*
* List retrieveAllUsers = userRepository.findAll(); return
* userMapper.toDTOList(retrieveAllUsers);
*/
return null;
}
public UserEntity createNewUser(UserDTO userDTO) {
/*
* UserEntity newUserCreated =
* userRepository.save(userMapper.toEntity(userDTO)); return newUserCreated;
*/
return null;
}
@Transactional
public void deleteuserbyEmail(String email) {
userRepository.deleteByEmail(email);
}
public UserEntity updateUserEmail(String currentEmail, String newEmail) {
/*
* Optional targetUser = userRepository.findByEmail(currentEmail);
* if(targetUser.isPresent()) { UserEntity userToUpdate = targetUser.get();
* userToUpdate.setEmail(newEmail); return userRepository.save(userToUpdate);
* }else return null;//create error message here
} */
return null;
}
}
< /code>
mapper: < /p>
package DataForMap;
import java.util.List;
import org.mapstruct.Mapper;
import org.springframework.stereotype.Component;
import DTO.UserDTO;
import Entity.UserEntity;
@Mapper(componentModel="spring")
public interface UserMappingOperation {
UserEntity toEntity(UserDTO userDTO);
UserDTO toDTO(UserEntity userEntity);
List toDTOList(List userEntity);
}
< /code>
Пользователь DTO: < /p>
package DTO;
import java.util.List;
import org.springframework.stereotype.Component;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Data
public class UserDTO {
private Long id;
private String firstname;
private String lastname;
private String email;
private String password;
private List roomDTO;
}
Entity layer:
package Entity;
import java.util.ArrayList;
import java.util.List;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Entity
@Table(name="User_Table")
@NoArgsConstructor
@AllArgsConstructor
@Data
public class UserEntity{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String firstname;
private String lastname;
private String email;
private String password;
@OneToMany(mappedBy="userEntity", cascade = CascadeType.ALL,fetch = FetchType.EAGER)
private List roomentity = new ArrayList();
public Long getId() {return id;}
public void setId(Long id) {this.id = id;}
public String getfirstname() {return firstname;}
public void setfirstname(String firstname) {this.firstname = firstname;}
public String getlastname() {return lastname;}
public void setlastname(String lastname) {this.lastname = lastname;}
public String getEmail() {return email;}
public void setEmail(String email) {this.email = email;}
public String getpassword() {return password;}
public void setpassword(String password) { this.password = password;}
public List getRoomentity() {return roomentity;}
public void setRoomentity(List roomentity) {this.roomentity = roomentity;}
}
Подробнее здесь: https://stackoverflow.com/questions/797 ... configurat