1
Good evening, I am creating an API with spring based on a course. However, it has some methods that are possibly depreciated in the version I am using(2.2.6) course version(1.5.10).
I need to apply the update to the following:
Type Mismatch: cannot Convert from Optional to User
The constructor Pagerequest(int, int) is Undefined
Follow my class Userserviceimpl:
package com.br.helpdesk.helpdesk.api.service.impl;
import com.br.helpdesk.helpdesk.api.entity.User;
import com.br.helpdesk.helpdesk.api.repository.UserRepository;
import com.br.helpdesk.helpdesk.api.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
@Override
public User findByEmail(String email) {
return this.userRepository.findByEmail(email);
}
@Override
public User createOrUpdate(User user) {
return this.userRepository.save(user);
}
@Override
public User findById(String id) {
return this.userRepository.findById(id);
}
@Override
public void delete(String id) {
this.userRepository.deleteById(id);
}
@Override
public Page<User> findAll(int page, int count) {
Pageable pages = new PageRequest(page, count)
return null;
}
}
Class User:
package com.br.helpdesk.helpdesk.api.entity;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import com.br.helpdesk.helpdesk.api.enums.ProfileEnum;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
@Document
public class User {
@Id
private String Id;
@Indexed(unique = true)
@NotBlank(message = "Email required")
@Email(message = "Email invalido")
private String email;
@NotBlank(message = "Passwor required")
@Size(min = 6)
private String password;
private ProfileEnum profile;
public String getId() {
return this.Id;
}
public void setId(String Id) {
this.Id = Id;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
public ProfileEnum getProfile() {
return this.profile;
}
public void setProfile(ProfileEnum profile) {
this.profile = profile;
}
}
Lastly Userservice:
package com.br.helpdesk.helpdesk.api.service;
import com.br.helpdesk.helpdesk.api.entity.User;
import org.springframework.data.domain.Page;
public interface UserService {
User findByEmail(String email);
User createOrUpdate(User user);
User findById(String id);
void delete(String id);
Page<User> findAll(int page, int count);
}
Someone’s been there and can help me?
Your question seems kind of cool. But I don’t understand exactly what it is you’re asking. As for the
findById
would only change the return of the method toOptional<User>
. As to thePageRequest
, would usePageRequest.of(page, count)
.– Victor Stafusa