用户模型
@Entity
@Table(name="users")
public class Users implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private Integer id;
@Column(name="name")
private String name;
@Column(name="surname")
private String surname;
@Column(name="username")
private String username;
@Column(name="password")
private String password;
@Column(name="is_super_user")
private Boolean isSuperUser;
@Column(name="email")
private String email;
@Column(name="created_at")
private Date created_at;
@Column(name="updated_at")
private Date updated_at;
@PrePersist
protected void onCreate() {
created_at = new Date();
}
@PreUpdate
protected void onUpdate() {
updated_at = new Date();
}//and getter&setter
userDAO的
@Repository
public class UsersDao {
@PersistenceContext
private EntityManager entityManager;
public void save(Users users){
entityManager.persist(users);
}
}
UserDto喜欢没有这个构造函数的模型
public UsersDto(){
}
public UsersDto(Users users){
this.id = users.getId();
this.name = users.getName();
this.surname = users.getSurname();
this.email = users.getEmail();
this.isSuperUser = users.getSuperUser();
this.username = users.getUsername();
this.password = users.getPassword();
}
UserResources
@Component
@Path("/users")
public class UsersResources {
@Autowired
UsersService usersService;
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response saveCity(UsersDto usersDto){
Users users;
try{
users = usersService.saveUsers(usersDto);
}catch (Exception e){
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
return Response.ok(users).build();
}
}
UserServices
@Service
public class UsersService {
@Autowired
private UsersDao usersDao;
@Transactional
public Users saveUsers(UsersDto usersDto){
Users users = new Users();
users.setName(usersDto.getName());
users.setEmail(usersDto.getEmail());
users.setSuperUser(usersDto.getSuperUser());
users.setSurname(usersDto.getSurname());
users.setPassword(usersDto.getPassword());
usersDao.save(users);
return users;
}
}
错误:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javax/transaction/SystemException
at
... 27 common frames omitted
Caused by: java.lang.NoClassDefFoundError: javax/transaction/SystemException
at java.base/java.lang.Class.forName0(Native Method) ~[na:na]
at java.base/java.lang.Class.forName(Class.java:375) ~[na:na]
at org.jboss.logging.Logger$1.run(Logger.java:2554) ~[jboss-logging-3.3.2.Final.jar:3.3.2.Final]
我的pom.xml有spring boot starter(web security jersey jpa mysql)和javax.transaction
那我该如何解决?
答案 0 :(得分:0)
您在javax:javaee-web-api:6.0
文件中将provided
作为pom.xml
范围依赖关系。这说明您希望它在运行时由容器提供。 NoClassDefFoundException
似乎表明它没有在运行时提供(也许你使用的是Tomcat而不是TomEE?)。
尝试从<scope>
中的javaee-web-api
依赖项中删除pom.xml
元素 - 您可能只是在运行时遇到不同的错误(例如,如果您没有可用的实现) ,但这会让你超越这个特定的错误。