0

I want to search by email but always get "error": "Not Acceptable",

@RestController
@RequestMapping("api/users")
public class UserController {

    private final UserService userService;
    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping(value = "/{name:.+}")
    public User getUser(@PathVariable String name)  {
        return userService.getUserByEmail(name);
    }
@Service
public class UserServiceImpl implements UserService {
    private final UserRepository userRepository;


    public UserServiceImpl(UserRepository userRepository) {
        this.userRepository = userRepository;

    }
    @Override
    public User getUserByEmail(String email){
        User user = userRepository.findByEmail(email).get();
        return user;
    }
@Repository
public interface UserRepository extends JpaRepository<User,Long> {
    Optional<User> findByEmail(@Param("email") String email);
}

even It can fetch from database but when want to return throw errorfetch from db

but throw error enter image description here

add header application/json header but don't work.

another thing that I can get byId and firstName ,this work correctly

1 Answers1

1

Try adding, value in pathVariable in the controller:

The content in bracket is a regex so it should work.

@GetMapping("/statusByEmail/{email:.+}/")
public String statusByEmail(@PathVariable(value = "email") String email){
  //code
}

And from the postman/rest-client

Get http://mywebhook.com/statusByEmail/abc.test@gmail.com/

If this doesn't work try to give the email in URLEncoded format: The problem might be due to the multiple . in the request Eg: alireza.ca%40gmail.com

OR

You can set Content-Type: application/x-www-form-urlencoded to automatically do the encoding of the url

Hopefully, this should work.

Lavish
  • 642
  • 7
  • 12
  • 1
    thanks .just add slash (/) end of {email:.+}/ and my problem solved ,but i don't understand why must add slash end of it. – alireza rayani May 31 '20 at 08:57