Java 更新用户';s地址-春季休息

Java 更新用户';s地址-春季休息,java,spring,Java,Spring,因此,我使用Spring5创建了以下RESTAPI { "userId": "DtvQLbP5uUXaJr4y5n2yxch3BODQCd", "firstName": "T", "lastName": "T", "email": "email1@gmail.com", "addresses": [ { "id": 158, "addressId": "yRK81aJUn2Sfh4JSaWU4svr2YtWgZj", "city": "L

因此,我使用Spring5创建了以下RESTAPI

{
"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"
    }
]
}
{
    "addressId": "yRK81aJUn2Sfh4JSaWU4svr2YtWgZj",
    "city": "Larissa",
    "country": "Greece",
    "streetName": "Iasonos 33",
    "postalCode": "41223",
    "type": "billing"
}
我可以更新用户的详细信息,但现在我也想更新地址的详细信息

{
"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"
    }
]
}
{
    "addressId": "yRK81aJUn2Sfh4JSaWU4svr2YtWgZj",
    "city": "Larissa",
    "country": "Greece",
    "streetName": "Iasonos 33",
    "postalCode": "41223",
    "type": "billing"
}
然而,我得到了这个

{
    "timestamp": "2019-03-24T08:53:27.986+0000",
    "message": "Missing URI template variable 'id' for method parameter of type String"
}
这是我的AddressServiceImpl课程

@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;
    }


}
@服务
公共类AddressServiceImpl实现AddressService{
@自动连线
用户存储库用户存储库;
@自动连线
地址库地址库;
@凌驾
公开名单

如何修复这个错误?有什么想法吗

谢谢

西奥

字符串类型的方法参数缺少URI模板变量“id”

消息很清楚。它说您已经定义了一个名为“id”的路径变量,但URI中没有任何这样的模板变量。事实上:

@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
                                       ...

您已经声明了两个名为
userId
addressId
的路径变量,但是只有一个名为
@PathVariable
注释的方法参数名为
id
。您可以更正方法参数:

@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){
// ...
}
如果ADRESSID是唯一的,则userId是冗余的,因此您还可以更改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){
// ...
}

在updateAddress方法中,要传递两个参数的path变量

 @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){

          AddressesRest returnValue = new AddressesRest();

          if(addressDetails.getCity().isEmpty()
                  || addressDetails.getCountry().isEmpty()
                  || addressDetails.getPostalCode().isEmpty()
                  || addressDetails.getStreetName().isEmpty()
                  || addressDetails.getType().isEmpty()) throw new NullPointerException("The object is null");

          AddressDTO addressDto = new AddressDTO();
          BeanUtils.copyProperties(addressDetails, addressDto);

          AddressDTO updatedAddress = addressService.updateAddress(addressId, addressDto);
          BeanUtils.copyProperties(updatedAddress,returnValue);

          return returnValue;
    }