alireza rayani
alireza rayani

Reputation: 31

HTTP Status 406 in rest api spring boot when getByEmail

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

Upvotes: 0

Views: 622

Answers (1)

Lavish
Lavish

Reputation: 720

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/[email protected]/

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.

Upvotes: 1

Related Questions