Updating user's address - Spring restWhat exactly is Spring Framework for?How does autowiring work in Spring?Difference between applicationContext.xml and spring-servlet.xml in Spring FrameworkWhat's the difference between @Component, @Repository & @Service annotations in Spring?What is the difference between @Inject and @Autowired in Spring Framework? Which one to use under what condition?What is difference between CrudRepository and JpaRepository interfaces in Spring Data JPA?What in the world are Spring beans?Spring JPA Auditing empty createdByWhy is my Spring @Autowired field null?How to configure port for a Spring Boot application

Declining an unreasonable request from a superior

The Passive Wisdom (Perception) score of my character on D&D Beyond seems too high

What is the difference between nullifying your vote and not going to vote at all?

shutdown at specific date

Where did the “Vikings wear helmets with horn” stereotype come from and why?

Crossword gone overboard

How to properly maintain eye contact with people that have distinct facial features?

Can a wire having a 610-670 THz (frequency of blue light) AC frequency supply, generate blue light?

How many chess players are over 2500 Elo?

Were pen cap holes designed to prevent death by suffocation if swallowed?

Ticket sales for Queen at the Live Aid

How is character development a major role in the plot of a story

Is this story about US tax office reasonable?

Can non-English-speaking characters use wordplay specific to English?

What are the problems in teaching guitar via Skype?

Where can I find the list of all tendons in the human body?

Do firearms count as ranged weapons?

Which noble houses were destroyed during the Game of Thrones?

Is my router's IP address really public?

Infinitely many hats

The qvolume of an integer

What does it mean when you think without speaking?

Mother abusing my finances

Can the Help action be used to give advantage to a specific ally's attack (rather than just the next ally who attacks the target)?



Updating user's address - Spring rest


What exactly is Spring Framework for?How does autowiring work in Spring?Difference between applicationContext.xml and spring-servlet.xml in Spring FrameworkWhat's the difference between @Component, @Repository & @Service annotations in Spring?What is the difference between @Inject and @Autowired in Spring Framework? Which one to use under what condition?What is difference between CrudRepository and JpaRepository interfaces in Spring Data JPA?What in the world are Spring beans?Spring JPA Auditing empty createdByWhy is my Spring @Autowired field null?How to configure port for a Spring Boot application






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















So I created the following rest API using Spring 5.



http://localhost:8080/restful-webservices/users/DtvQLbP5uUXaJr4y5n2yxch3BODQCd




"userId": "DtvQLbP5uUXaJr4y5n2yxch3BODQCd",
"firstName": "T",
"lastName": "T",
"email": "email1@gmail.com",
"addresses": [

"id": 158,
"addressId": "yRK81aJUn2Sfh4JSaWU4svr2YtWgZj",
"city": "Larissa",
"country": "Greece",
"streetName": "Iasonos 33",
"postalCode": "41223",
"type": "billing"
,

"id": 157,
"addressId": "ajyPnC75jQ2G5aqfDY4sqBYk5BUE9X",
"city": "Larissa",
"country": "Greece",
"streetName": "Palaiologou 11",
"postalCode": "41223",
"type": "shipping"

]



I can update the user's details, but now I want to update the address's details as well. http://localhost:8080/restful-webservices/users/DtvQLbP5uUXaJr4y5n2yxch3BODQCd/addresses/yRK81aJUn2Sfh4JSaWU4svr2YtWgZj




"addressId": "yRK81aJUn2Sfh4JSaWU4svr2YtWgZj",
"city": "Larissa",
"country": "Greece",
"streetName": "Iasonos 33",
"postalCode": "41223",
"type": "billing"



However, I am getting this




"timestamp": "2019-03-24T08:53:27.986+0000",
"message": "Missing URI template variable 'id' for method parameter of type String"



Here is my AddressServiceImpl class



@Service
public class AddressServiceImpl implements AddressService

@Autowired
UserRepository userRepository;

@Autowired
AddressRepository addressRepository;

@Override
public List<AddressDTO> getAddresses(String userId)

List<AddressDTO> returnValue = new ArrayList<>();
ModelMapper modelMapper = new ModelMapper();

UserEntity userEntity = userRepository.findByUserId(userId);


if(userEntity == null) return returnValue;

Iterable<AddressEntity> addresses = addressRepository.findAllByUserDetails(userEntity);

for(AddressEntity addressEntity : addresses)
returnValue.add(modelMapper.map(addressEntity, AddressDTO.class));


return returnValue;


@Override
public AddressDTO getAddress(String addressId)

AddressDTO returnValue = null;

AddressEntity addressEntity = addressRepository.findByAddressId(addressId);

if(addressEntity != null)
returnValue = new ModelMapper().map(addressEntity, AddressDTO.class);


return returnValue;


@Override
public AddressDTO updateAddress(String addressId, AddressDTO address)

AddressDTO returnValue = new AddressDTO();
AddressEntity addressEntity = addressRepository.findByAddressId(addressId);

if(addressEntity == null)
throw new UserServiceException(ErrorMessages.NO_RECORD_FOUND.getErrorMessage());


addressEntity.setCity(address.getCity());
addressEntity.setCountry(address.getCountry());
addressEntity.setPostalCode(address.getPostalCode());
addressEntity.setStreetName(address.getType());

AddressEntity updatedUserEntity = addressRepository.save(addressEntity);
BeanUtils.copyProperties(updatedUserEntity,returnValue);

return returnValue;






and my controller.



@RestController
@RequestMapping("users")
public class UserController

@Autowired
UserService userService;

@Autowired
AddressService addressService;

@Autowired
AddressService addressesService;


@GetMapping(path = "/id",
produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE )
public UserRest getUser(@PathVariable String id)

UserRest returnValue = new UserRest();

UserDto userDto = userService.getUserByUserId(id);
BeanUtils.copyProperties(userDto, returnValue);

return returnValue;


@PostMapping(
consumes = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE)
public UserRest createUser(@RequestBody UsersDetailsRequestModel userDetails) throws Exception

UserRest returnValue = new UserRest();

if(userDetails.getFirstName().isEmpty()) throw new NullPointerException("The object is null");

// UserDto userDto = new UserDto();
// BeanUtils.copyProperties(userDetails,userDto);

ModelMapper modelMapper = new ModelMapper();
UserDto userDto = modelMapper.map(userDetails, UserDto.class);

UserDto createUser = userService.createUser(userDto);
// BeanUtils.copyProperties(createUser,returnValue);
returnValue = modelMapper.map(createUser, UserRest.class);

return returnValue;



@PutMapping(path = "/id",
consumes = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE)
public UserRest updateUser(@PathVariable String id, @RequestBody UsersDetailsRequestModel userDetails)

UserRest returnValue = new UserRest();

if(userDetails.getFirstName().isEmpty()) throw new NullPointerException("The object is null");

UserDto userDto = new UserDto();
BeanUtils.copyProperties(userDetails,userDto);

UserDto updatedUser = userService.updateUser(id, userDto);
BeanUtils.copyProperties(updatedUser,returnValue);

return returnValue;


@DeleteMapping(path = "/id", produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE )
public OperationStatusModel deleteUser(@PathVariable String id)

OperationStatusModel returnValue = new OperationStatusModel();
returnValue.setOperationName(RequestOperationName.DELETE.name());

userService.deleteUser(id);

returnValue.setOperationResult(RequestOperationStatus.SUCCESS.name());
return returnValue;



@GetMapping(produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE )
public List<UserRest> getUsers(@RequestParam(value="page", defaultValue = "0") int page,
@RequestParam(value="limit", defaultValue = "25") int limit)

List<UserRest> returnValue = new ArrayList<>();

List<UserDto> users = userService.getUsers(page,limit);

for(UserDto userDto : users)
UserRest userModel = new UserRest();
BeanUtils.copyProperties(userDto, userModel);
returnValue.add(userModel);


return returnValue;

//http://localhost:8080/restful-webservices/users/id/addresses
@GetMapping(path = "/id/addresses",
produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE )
public List<AddressesRest> getUserAddresses(@PathVariable String id)

List<AddressesRest> returnValue = new ArrayList<>();

List<AddressDTO> addressesDTO = addressesService.getAddresses(id);

if(addressesDTO != null && !addressesDTO.isEmpty())
Type listType = new TypeToken<List<AddressesRest>>().getType();
returnValue = new ModelMapper().map(addressesDTO, listType);


return returnValue;


@GetMapping(path="/userId/addresses/addressId",
produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE )
public AddressesRest getUserAddress(@PathVariable String addressId)

AddressDTO addressesDto = addressService.getAddress(addressId);

ModelMapper modelMapper = new ModelMapper();

return modelMapper.map(addressesDto, AddressesRest.class);


@PutMapping(path="/userId/addresses/addressId",
consumes = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE)
public AddressesRest updateAddress(@PathVariable String id, @RequestBody AddressesRequestModel addressDetails) addressDetails.getCountry().isEmpty()




This is my repo



How can this error be fixed? Any ideas?



Thanks,



Theo.










share|improve this question




























    0















    So I created the following rest API using Spring 5.



    http://localhost:8080/restful-webservices/users/DtvQLbP5uUXaJr4y5n2yxch3BODQCd




    "userId": "DtvQLbP5uUXaJr4y5n2yxch3BODQCd",
    "firstName": "T",
    "lastName": "T",
    "email": "email1@gmail.com",
    "addresses": [

    "id": 158,
    "addressId": "yRK81aJUn2Sfh4JSaWU4svr2YtWgZj",
    "city": "Larissa",
    "country": "Greece",
    "streetName": "Iasonos 33",
    "postalCode": "41223",
    "type": "billing"
    ,

    "id": 157,
    "addressId": "ajyPnC75jQ2G5aqfDY4sqBYk5BUE9X",
    "city": "Larissa",
    "country": "Greece",
    "streetName": "Palaiologou 11",
    "postalCode": "41223",
    "type": "shipping"

    ]



    I can update the user's details, but now I want to update the address's details as well. http://localhost:8080/restful-webservices/users/DtvQLbP5uUXaJr4y5n2yxch3BODQCd/addresses/yRK81aJUn2Sfh4JSaWU4svr2YtWgZj




    "addressId": "yRK81aJUn2Sfh4JSaWU4svr2YtWgZj",
    "city": "Larissa",
    "country": "Greece",
    "streetName": "Iasonos 33",
    "postalCode": "41223",
    "type": "billing"



    However, I am getting this




    "timestamp": "2019-03-24T08:53:27.986+0000",
    "message": "Missing URI template variable 'id' for method parameter of type String"



    Here is my AddressServiceImpl class



    @Service
    public class AddressServiceImpl implements AddressService

    @Autowired
    UserRepository userRepository;

    @Autowired
    AddressRepository addressRepository;

    @Override
    public List<AddressDTO> getAddresses(String userId)

    List<AddressDTO> returnValue = new ArrayList<>();
    ModelMapper modelMapper = new ModelMapper();

    UserEntity userEntity = userRepository.findByUserId(userId);


    if(userEntity == null) return returnValue;

    Iterable<AddressEntity> addresses = addressRepository.findAllByUserDetails(userEntity);

    for(AddressEntity addressEntity : addresses)
    returnValue.add(modelMapper.map(addressEntity, AddressDTO.class));


    return returnValue;


    @Override
    public AddressDTO getAddress(String addressId)

    AddressDTO returnValue = null;

    AddressEntity addressEntity = addressRepository.findByAddressId(addressId);

    if(addressEntity != null)
    returnValue = new ModelMapper().map(addressEntity, AddressDTO.class);


    return returnValue;


    @Override
    public AddressDTO updateAddress(String addressId, AddressDTO address)

    AddressDTO returnValue = new AddressDTO();
    AddressEntity addressEntity = addressRepository.findByAddressId(addressId);

    if(addressEntity == null)
    throw new UserServiceException(ErrorMessages.NO_RECORD_FOUND.getErrorMessage());


    addressEntity.setCity(address.getCity());
    addressEntity.setCountry(address.getCountry());
    addressEntity.setPostalCode(address.getPostalCode());
    addressEntity.setStreetName(address.getType());

    AddressEntity updatedUserEntity = addressRepository.save(addressEntity);
    BeanUtils.copyProperties(updatedUserEntity,returnValue);

    return returnValue;






    and my controller.



    @RestController
    @RequestMapping("users")
    public class UserController

    @Autowired
    UserService userService;

    @Autowired
    AddressService addressService;

    @Autowired
    AddressService addressesService;


    @GetMapping(path = "/id",
    produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE )
    public UserRest getUser(@PathVariable String id)

    UserRest returnValue = new UserRest();

    UserDto userDto = userService.getUserByUserId(id);
    BeanUtils.copyProperties(userDto, returnValue);

    return returnValue;


    @PostMapping(
    consumes = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE,
    produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE)
    public UserRest createUser(@RequestBody UsersDetailsRequestModel userDetails) throws Exception

    UserRest returnValue = new UserRest();

    if(userDetails.getFirstName().isEmpty()) throw new NullPointerException("The object is null");

    // UserDto userDto = new UserDto();
    // BeanUtils.copyProperties(userDetails,userDto);

    ModelMapper modelMapper = new ModelMapper();
    UserDto userDto = modelMapper.map(userDetails, UserDto.class);

    UserDto createUser = userService.createUser(userDto);
    // BeanUtils.copyProperties(createUser,returnValue);
    returnValue = modelMapper.map(createUser, UserRest.class);

    return returnValue;



    @PutMapping(path = "/id",
    consumes = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE,
    produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE)
    public UserRest updateUser(@PathVariable String id, @RequestBody UsersDetailsRequestModel userDetails)

    UserRest returnValue = new UserRest();

    if(userDetails.getFirstName().isEmpty()) throw new NullPointerException("The object is null");

    UserDto userDto = new UserDto();
    BeanUtils.copyProperties(userDetails,userDto);

    UserDto updatedUser = userService.updateUser(id, userDto);
    BeanUtils.copyProperties(updatedUser,returnValue);

    return returnValue;


    @DeleteMapping(path = "/id", produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE )
    public OperationStatusModel deleteUser(@PathVariable String id)

    OperationStatusModel returnValue = new OperationStatusModel();
    returnValue.setOperationName(RequestOperationName.DELETE.name());

    userService.deleteUser(id);

    returnValue.setOperationResult(RequestOperationStatus.SUCCESS.name());
    return returnValue;



    @GetMapping(produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE )
    public List<UserRest> getUsers(@RequestParam(value="page", defaultValue = "0") int page,
    @RequestParam(value="limit", defaultValue = "25") int limit)

    List<UserRest> returnValue = new ArrayList<>();

    List<UserDto> users = userService.getUsers(page,limit);

    for(UserDto userDto : users)
    UserRest userModel = new UserRest();
    BeanUtils.copyProperties(userDto, userModel);
    returnValue.add(userModel);


    return returnValue;

    //http://localhost:8080/restful-webservices/users/id/addresses
    @GetMapping(path = "/id/addresses",
    produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE )
    public List<AddressesRest> getUserAddresses(@PathVariable String id)

    List<AddressesRest> returnValue = new ArrayList<>();

    List<AddressDTO> addressesDTO = addressesService.getAddresses(id);

    if(addressesDTO != null && !addressesDTO.isEmpty())
    Type listType = new TypeToken<List<AddressesRest>>().getType();
    returnValue = new ModelMapper().map(addressesDTO, listType);


    return returnValue;


    @GetMapping(path="/userId/addresses/addressId",
    produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE )
    public AddressesRest getUserAddress(@PathVariable String addressId)

    AddressDTO addressesDto = addressService.getAddress(addressId);

    ModelMapper modelMapper = new ModelMapper();

    return modelMapper.map(addressesDto, AddressesRest.class);


    @PutMapping(path="/userId/addresses/addressId",
    consumes = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE,
    produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE)
    public AddressesRest updateAddress(@PathVariable String id, @RequestBody AddressesRequestModel addressDetails) addressDetails.getCountry().isEmpty()




    This is my repo



    How can this error be fixed? Any ideas?



    Thanks,



    Theo.










    share|improve this question
























      0












      0








      0








      So I created the following rest API using Spring 5.



      http://localhost:8080/restful-webservices/users/DtvQLbP5uUXaJr4y5n2yxch3BODQCd




      "userId": "DtvQLbP5uUXaJr4y5n2yxch3BODQCd",
      "firstName": "T",
      "lastName": "T",
      "email": "email1@gmail.com",
      "addresses": [

      "id": 158,
      "addressId": "yRK81aJUn2Sfh4JSaWU4svr2YtWgZj",
      "city": "Larissa",
      "country": "Greece",
      "streetName": "Iasonos 33",
      "postalCode": "41223",
      "type": "billing"
      ,

      "id": 157,
      "addressId": "ajyPnC75jQ2G5aqfDY4sqBYk5BUE9X",
      "city": "Larissa",
      "country": "Greece",
      "streetName": "Palaiologou 11",
      "postalCode": "41223",
      "type": "shipping"

      ]



      I can update the user's details, but now I want to update the address's details as well. http://localhost:8080/restful-webservices/users/DtvQLbP5uUXaJr4y5n2yxch3BODQCd/addresses/yRK81aJUn2Sfh4JSaWU4svr2YtWgZj




      "addressId": "yRK81aJUn2Sfh4JSaWU4svr2YtWgZj",
      "city": "Larissa",
      "country": "Greece",
      "streetName": "Iasonos 33",
      "postalCode": "41223",
      "type": "billing"



      However, I am getting this




      "timestamp": "2019-03-24T08:53:27.986+0000",
      "message": "Missing URI template variable 'id' for method parameter of type String"



      Here is my AddressServiceImpl class



      @Service
      public class AddressServiceImpl implements AddressService

      @Autowired
      UserRepository userRepository;

      @Autowired
      AddressRepository addressRepository;

      @Override
      public List<AddressDTO> getAddresses(String userId)

      List<AddressDTO> returnValue = new ArrayList<>();
      ModelMapper modelMapper = new ModelMapper();

      UserEntity userEntity = userRepository.findByUserId(userId);


      if(userEntity == null) return returnValue;

      Iterable<AddressEntity> addresses = addressRepository.findAllByUserDetails(userEntity);

      for(AddressEntity addressEntity : addresses)
      returnValue.add(modelMapper.map(addressEntity, AddressDTO.class));


      return returnValue;


      @Override
      public AddressDTO getAddress(String addressId)

      AddressDTO returnValue = null;

      AddressEntity addressEntity = addressRepository.findByAddressId(addressId);

      if(addressEntity != null)
      returnValue = new ModelMapper().map(addressEntity, AddressDTO.class);


      return returnValue;


      @Override
      public AddressDTO updateAddress(String addressId, AddressDTO address)

      AddressDTO returnValue = new AddressDTO();
      AddressEntity addressEntity = addressRepository.findByAddressId(addressId);

      if(addressEntity == null)
      throw new UserServiceException(ErrorMessages.NO_RECORD_FOUND.getErrorMessage());


      addressEntity.setCity(address.getCity());
      addressEntity.setCountry(address.getCountry());
      addressEntity.setPostalCode(address.getPostalCode());
      addressEntity.setStreetName(address.getType());

      AddressEntity updatedUserEntity = addressRepository.save(addressEntity);
      BeanUtils.copyProperties(updatedUserEntity,returnValue);

      return returnValue;






      and my controller.



      @RestController
      @RequestMapping("users")
      public class UserController

      @Autowired
      UserService userService;

      @Autowired
      AddressService addressService;

      @Autowired
      AddressService addressesService;


      @GetMapping(path = "/id",
      produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE )
      public UserRest getUser(@PathVariable String id)

      UserRest returnValue = new UserRest();

      UserDto userDto = userService.getUserByUserId(id);
      BeanUtils.copyProperties(userDto, returnValue);

      return returnValue;


      @PostMapping(
      consumes = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE,
      produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE)
      public UserRest createUser(@RequestBody UsersDetailsRequestModel userDetails) throws Exception

      UserRest returnValue = new UserRest();

      if(userDetails.getFirstName().isEmpty()) throw new NullPointerException("The object is null");

      // UserDto userDto = new UserDto();
      // BeanUtils.copyProperties(userDetails,userDto);

      ModelMapper modelMapper = new ModelMapper();
      UserDto userDto = modelMapper.map(userDetails, UserDto.class);

      UserDto createUser = userService.createUser(userDto);
      // BeanUtils.copyProperties(createUser,returnValue);
      returnValue = modelMapper.map(createUser, UserRest.class);

      return returnValue;



      @PutMapping(path = "/id",
      consumes = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE,
      produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE)
      public UserRest updateUser(@PathVariable String id, @RequestBody UsersDetailsRequestModel userDetails)

      UserRest returnValue = new UserRest();

      if(userDetails.getFirstName().isEmpty()) throw new NullPointerException("The object is null");

      UserDto userDto = new UserDto();
      BeanUtils.copyProperties(userDetails,userDto);

      UserDto updatedUser = userService.updateUser(id, userDto);
      BeanUtils.copyProperties(updatedUser,returnValue);

      return returnValue;


      @DeleteMapping(path = "/id", produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE )
      public OperationStatusModel deleteUser(@PathVariable String id)

      OperationStatusModel returnValue = new OperationStatusModel();
      returnValue.setOperationName(RequestOperationName.DELETE.name());

      userService.deleteUser(id);

      returnValue.setOperationResult(RequestOperationStatus.SUCCESS.name());
      return returnValue;



      @GetMapping(produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE )
      public List<UserRest> getUsers(@RequestParam(value="page", defaultValue = "0") int page,
      @RequestParam(value="limit", defaultValue = "25") int limit)

      List<UserRest> returnValue = new ArrayList<>();

      List<UserDto> users = userService.getUsers(page,limit);

      for(UserDto userDto : users)
      UserRest userModel = new UserRest();
      BeanUtils.copyProperties(userDto, userModel);
      returnValue.add(userModel);


      return returnValue;

      //http://localhost:8080/restful-webservices/users/id/addresses
      @GetMapping(path = "/id/addresses",
      produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE )
      public List<AddressesRest> getUserAddresses(@PathVariable String id)

      List<AddressesRest> returnValue = new ArrayList<>();

      List<AddressDTO> addressesDTO = addressesService.getAddresses(id);

      if(addressesDTO != null && !addressesDTO.isEmpty())
      Type listType = new TypeToken<List<AddressesRest>>().getType();
      returnValue = new ModelMapper().map(addressesDTO, listType);


      return returnValue;


      @GetMapping(path="/userId/addresses/addressId",
      produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE )
      public AddressesRest getUserAddress(@PathVariable String addressId)

      AddressDTO addressesDto = addressService.getAddress(addressId);

      ModelMapper modelMapper = new ModelMapper();

      return modelMapper.map(addressesDto, AddressesRest.class);


      @PutMapping(path="/userId/addresses/addressId",
      consumes = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE,
      produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE)
      public AddressesRest updateAddress(@PathVariable String id, @RequestBody AddressesRequestModel addressDetails) addressDetails.getCountry().isEmpty()




      This is my repo



      How can this error be fixed? Any ideas?



      Thanks,



      Theo.










      share|improve this question














      So I created the following rest API using Spring 5.



      http://localhost:8080/restful-webservices/users/DtvQLbP5uUXaJr4y5n2yxch3BODQCd




      "userId": "DtvQLbP5uUXaJr4y5n2yxch3BODQCd",
      "firstName": "T",
      "lastName": "T",
      "email": "email1@gmail.com",
      "addresses": [

      "id": 158,
      "addressId": "yRK81aJUn2Sfh4JSaWU4svr2YtWgZj",
      "city": "Larissa",
      "country": "Greece",
      "streetName": "Iasonos 33",
      "postalCode": "41223",
      "type": "billing"
      ,

      "id": 157,
      "addressId": "ajyPnC75jQ2G5aqfDY4sqBYk5BUE9X",
      "city": "Larissa",
      "country": "Greece",
      "streetName": "Palaiologou 11",
      "postalCode": "41223",
      "type": "shipping"

      ]



      I can update the user's details, but now I want to update the address's details as well. http://localhost:8080/restful-webservices/users/DtvQLbP5uUXaJr4y5n2yxch3BODQCd/addresses/yRK81aJUn2Sfh4JSaWU4svr2YtWgZj




      "addressId": "yRK81aJUn2Sfh4JSaWU4svr2YtWgZj",
      "city": "Larissa",
      "country": "Greece",
      "streetName": "Iasonos 33",
      "postalCode": "41223",
      "type": "billing"



      However, I am getting this




      "timestamp": "2019-03-24T08:53:27.986+0000",
      "message": "Missing URI template variable 'id' for method parameter of type String"



      Here is my AddressServiceImpl class



      @Service
      public class AddressServiceImpl implements AddressService

      @Autowired
      UserRepository userRepository;

      @Autowired
      AddressRepository addressRepository;

      @Override
      public List<AddressDTO> getAddresses(String userId)

      List<AddressDTO> returnValue = new ArrayList<>();
      ModelMapper modelMapper = new ModelMapper();

      UserEntity userEntity = userRepository.findByUserId(userId);


      if(userEntity == null) return returnValue;

      Iterable<AddressEntity> addresses = addressRepository.findAllByUserDetails(userEntity);

      for(AddressEntity addressEntity : addresses)
      returnValue.add(modelMapper.map(addressEntity, AddressDTO.class));


      return returnValue;


      @Override
      public AddressDTO getAddress(String addressId)

      AddressDTO returnValue = null;

      AddressEntity addressEntity = addressRepository.findByAddressId(addressId);

      if(addressEntity != null)
      returnValue = new ModelMapper().map(addressEntity, AddressDTO.class);


      return returnValue;


      @Override
      public AddressDTO updateAddress(String addressId, AddressDTO address)

      AddressDTO returnValue = new AddressDTO();
      AddressEntity addressEntity = addressRepository.findByAddressId(addressId);

      if(addressEntity == null)
      throw new UserServiceException(ErrorMessages.NO_RECORD_FOUND.getErrorMessage());


      addressEntity.setCity(address.getCity());
      addressEntity.setCountry(address.getCountry());
      addressEntity.setPostalCode(address.getPostalCode());
      addressEntity.setStreetName(address.getType());

      AddressEntity updatedUserEntity = addressRepository.save(addressEntity);
      BeanUtils.copyProperties(updatedUserEntity,returnValue);

      return returnValue;






      and my controller.



      @RestController
      @RequestMapping("users")
      public class UserController

      @Autowired
      UserService userService;

      @Autowired
      AddressService addressService;

      @Autowired
      AddressService addressesService;


      @GetMapping(path = "/id",
      produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE )
      public UserRest getUser(@PathVariable String id)

      UserRest returnValue = new UserRest();

      UserDto userDto = userService.getUserByUserId(id);
      BeanUtils.copyProperties(userDto, returnValue);

      return returnValue;


      @PostMapping(
      consumes = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE,
      produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE)
      public UserRest createUser(@RequestBody UsersDetailsRequestModel userDetails) throws Exception

      UserRest returnValue = new UserRest();

      if(userDetails.getFirstName().isEmpty()) throw new NullPointerException("The object is null");

      // UserDto userDto = new UserDto();
      // BeanUtils.copyProperties(userDetails,userDto);

      ModelMapper modelMapper = new ModelMapper();
      UserDto userDto = modelMapper.map(userDetails, UserDto.class);

      UserDto createUser = userService.createUser(userDto);
      // BeanUtils.copyProperties(createUser,returnValue);
      returnValue = modelMapper.map(createUser, UserRest.class);

      return returnValue;



      @PutMapping(path = "/id",
      consumes = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE,
      produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE)
      public UserRest updateUser(@PathVariable String id, @RequestBody UsersDetailsRequestModel userDetails)

      UserRest returnValue = new UserRest();

      if(userDetails.getFirstName().isEmpty()) throw new NullPointerException("The object is null");

      UserDto userDto = new UserDto();
      BeanUtils.copyProperties(userDetails,userDto);

      UserDto updatedUser = userService.updateUser(id, userDto);
      BeanUtils.copyProperties(updatedUser,returnValue);

      return returnValue;


      @DeleteMapping(path = "/id", produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE )
      public OperationStatusModel deleteUser(@PathVariable String id)

      OperationStatusModel returnValue = new OperationStatusModel();
      returnValue.setOperationName(RequestOperationName.DELETE.name());

      userService.deleteUser(id);

      returnValue.setOperationResult(RequestOperationStatus.SUCCESS.name());
      return returnValue;



      @GetMapping(produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE )
      public List<UserRest> getUsers(@RequestParam(value="page", defaultValue = "0") int page,
      @RequestParam(value="limit", defaultValue = "25") int limit)

      List<UserRest> returnValue = new ArrayList<>();

      List<UserDto> users = userService.getUsers(page,limit);

      for(UserDto userDto : users)
      UserRest userModel = new UserRest();
      BeanUtils.copyProperties(userDto, userModel);
      returnValue.add(userModel);


      return returnValue;

      //http://localhost:8080/restful-webservices/users/id/addresses
      @GetMapping(path = "/id/addresses",
      produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE )
      public List<AddressesRest> getUserAddresses(@PathVariable String id)

      List<AddressesRest> returnValue = new ArrayList<>();

      List<AddressDTO> addressesDTO = addressesService.getAddresses(id);

      if(addressesDTO != null && !addressesDTO.isEmpty())
      Type listType = new TypeToken<List<AddressesRest>>().getType();
      returnValue = new ModelMapper().map(addressesDTO, listType);


      return returnValue;


      @GetMapping(path="/userId/addresses/addressId",
      produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE )
      public AddressesRest getUserAddress(@PathVariable String addressId)

      AddressDTO addressesDto = addressService.getAddress(addressId);

      ModelMapper modelMapper = new ModelMapper();

      return modelMapper.map(addressesDto, AddressesRest.class);


      @PutMapping(path="/userId/addresses/addressId",
      consumes = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE,
      produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE)
      public AddressesRest updateAddress(@PathVariable String id, @RequestBody AddressesRequestModel addressDetails) addressDetails.getCountry().isEmpty()




      This is my repo



      How can this error be fixed? Any ideas?



      Thanks,



      Theo.







      java spring






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 24 at 9:00









      TheoTheo

      1,40232948




      1,40232948






















          3 Answers
          3






          active

          oldest

          votes


















          3















          Missing URI template variable 'id' for method parameter of type String




          The message is clear. It says you have defined a path variable named "id", but that there isn't any such template variable in the URI. And indeed:



          @PutMapping(path="/userId/addresses/addressId", // there is no id in the path
          ...)
          public AddressesRest updateAddress(@PathVariable String id, // there should be an id in the path
          ...





          share|improve this answer






























            2














            You've declared two path variables with names userId and addressId, but there is only one method argument with @PathVariable annotation named id. You could correct method arguments:



            @PutMapping(path="/userId/addresses/addressId",
            consumes = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE)
            public AddressesRest updateAddress(@PathVariable String userId, @PathVariable String addressId, @RequestBody AddressesRequestModel addressDetails)
            // ...



            If adressIds are unique then userId is redundant, so you could also change URI:



            @PutMapping(path="/address/id",
            consumes = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE)
            public AddressesRest updateAddress(@PathVariable String id, @RequestBody AddressesRequestModel addressDetails)
            // ...






            share|improve this answer






























              1














              In your updateAddress method, you are meant to pass the two parameter path variable.



               @PutMapping(path="/userId/addresses/addressId",
              consumes = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE,
              produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE)
              public AddressesRest updateAddress(@PathVariable String userId, @PathVariable String addressId, @RequestBody AddressesRequestModel addressDetails) addressDetails.getCountry().isEmpty()






              share|improve this answer























                Your Answer






                StackExchange.ifUsing("editor", function ()
                StackExchange.using("externalEditor", function ()
                StackExchange.using("snippets", function ()
                StackExchange.snippets.init();
                );
                );
                , "code-snippets");

                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "1"
                ;
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function()
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled)
                StackExchange.using("snippets", function()
                createEditor();
                );

                else
                createEditor();

                );

                function createEditor()
                StackExchange.prepareEditor(
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader:
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                ,
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );













                draft saved

                draft discarded


















                StackExchange.ready(
                function ()
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55322154%2fupdating-users-address-spring-rest%23new-answer', 'question_page');

                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                3















                Missing URI template variable 'id' for method parameter of type String




                The message is clear. It says you have defined a path variable named "id", but that there isn't any such template variable in the URI. And indeed:



                @PutMapping(path="/userId/addresses/addressId", // there is no id in the path
                ...)
                public AddressesRest updateAddress(@PathVariable String id, // there should be an id in the path
                ...





                share|improve this answer



























                  3















                  Missing URI template variable 'id' for method parameter of type String




                  The message is clear. It says you have defined a path variable named "id", but that there isn't any such template variable in the URI. And indeed:



                  @PutMapping(path="/userId/addresses/addressId", // there is no id in the path
                  ...)
                  public AddressesRest updateAddress(@PathVariable String id, // there should be an id in the path
                  ...





                  share|improve this answer

























                    3












                    3








                    3








                    Missing URI template variable 'id' for method parameter of type String




                    The message is clear. It says you have defined a path variable named "id", but that there isn't any such template variable in the URI. And indeed:



                    @PutMapping(path="/userId/addresses/addressId", // there is no id in the path
                    ...)
                    public AddressesRest updateAddress(@PathVariable String id, // there should be an id in the path
                    ...





                    share|improve this answer














                    Missing URI template variable 'id' for method parameter of type String




                    The message is clear. It says you have defined a path variable named "id", but that there isn't any such template variable in the URI. And indeed:



                    @PutMapping(path="/userId/addresses/addressId", // there is no id in the path
                    ...)
                    public AddressesRest updateAddress(@PathVariable String id, // there should be an id in the path
                    ...






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 24 at 9:07









                    JB NizetJB Nizet

                    555k629081031




                    555k629081031























                        2














                        You've declared two path variables with names userId and addressId, but there is only one method argument with @PathVariable annotation named id. You could correct method arguments:



                        @PutMapping(path="/userId/addresses/addressId",
                        consumes = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE,
                        produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE)
                        public AddressesRest updateAddress(@PathVariable String userId, @PathVariable String addressId, @RequestBody AddressesRequestModel addressDetails)
                        // ...



                        If adressIds are unique then userId is redundant, so you could also change URI:



                        @PutMapping(path="/address/id",
                        consumes = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE,
                        produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE)
                        public AddressesRest updateAddress(@PathVariable String id, @RequestBody AddressesRequestModel addressDetails)
                        // ...






                        share|improve this answer



























                          2














                          You've declared two path variables with names userId and addressId, but there is only one method argument with @PathVariable annotation named id. You could correct method arguments:



                          @PutMapping(path="/userId/addresses/addressId",
                          consumes = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE,
                          produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE)
                          public AddressesRest updateAddress(@PathVariable String userId, @PathVariable String addressId, @RequestBody AddressesRequestModel addressDetails)
                          // ...



                          If adressIds are unique then userId is redundant, so you could also change URI:



                          @PutMapping(path="/address/id",
                          consumes = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE,
                          produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE)
                          public AddressesRest updateAddress(@PathVariable String id, @RequestBody AddressesRequestModel addressDetails)
                          // ...






                          share|improve this answer

























                            2












                            2








                            2







                            You've declared two path variables with names userId and addressId, but there is only one method argument with @PathVariable annotation named id. You could correct method arguments:



                            @PutMapping(path="/userId/addresses/addressId",
                            consumes = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE,
                            produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE)
                            public AddressesRest updateAddress(@PathVariable String userId, @PathVariable String addressId, @RequestBody AddressesRequestModel addressDetails)
                            // ...



                            If adressIds are unique then userId is redundant, so you could also change URI:



                            @PutMapping(path="/address/id",
                            consumes = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE,
                            produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE)
                            public AddressesRest updateAddress(@PathVariable String id, @RequestBody AddressesRequestModel addressDetails)
                            // ...






                            share|improve this answer













                            You've declared two path variables with names userId and addressId, but there is only one method argument with @PathVariable annotation named id. You could correct method arguments:



                            @PutMapping(path="/userId/addresses/addressId",
                            consumes = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE,
                            produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE)
                            public AddressesRest updateAddress(@PathVariable String userId, @PathVariable String addressId, @RequestBody AddressesRequestModel addressDetails)
                            // ...



                            If adressIds are unique then userId is redundant, so you could also change URI:



                            @PutMapping(path="/address/id",
                            consumes = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE,
                            produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE)
                            public AddressesRest updateAddress(@PathVariable String id, @RequestBody AddressesRequestModel addressDetails)
                            // ...







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 24 at 9:11









                            Mikhail IlinykhMikhail Ilinykh

                            633310




                            633310





















                                1














                                In your updateAddress method, you are meant to pass the two parameter path variable.



                                 @PutMapping(path="/userId/addresses/addressId",
                                consumes = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE,
                                produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE)
                                public AddressesRest updateAddress(@PathVariable String userId, @PathVariable String addressId, @RequestBody AddressesRequestModel addressDetails) addressDetails.getCountry().isEmpty()






                                share|improve this answer



























                                  1














                                  In your updateAddress method, you are meant to pass the two parameter path variable.



                                   @PutMapping(path="/userId/addresses/addressId",
                                  consumes = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE,
                                  produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE)
                                  public AddressesRest updateAddress(@PathVariable String userId, @PathVariable String addressId, @RequestBody AddressesRequestModel addressDetails) addressDetails.getCountry().isEmpty()






                                  share|improve this answer

























                                    1












                                    1








                                    1







                                    In your updateAddress method, you are meant to pass the two parameter path variable.



                                     @PutMapping(path="/userId/addresses/addressId",
                                    consumes = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE,
                                    produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE)
                                    public AddressesRest updateAddress(@PathVariable String userId, @PathVariable String addressId, @RequestBody AddressesRequestModel addressDetails) addressDetails.getCountry().isEmpty()






                                    share|improve this answer













                                    In your updateAddress method, you are meant to pass the two parameter path variable.



                                     @PutMapping(path="/userId/addresses/addressId",
                                    consumes = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE,
                                    produces = MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE)
                                    public AddressesRest updateAddress(@PathVariable String userId, @PathVariable String addressId, @RequestBody AddressesRequestModel addressDetails) addressDetails.getCountry().isEmpty()







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Mar 24 at 9:27









                                    Kabiru AhmedKabiru Ahmed

                                    315




                                    315



























                                        draft saved

                                        draft discarded
















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid


                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.

                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55322154%2fupdating-users-address-spring-rest%23new-answer', 'question_page');

                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                                        SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                                        은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현