首页 > 解决方案 > 更新用户地址 - Spring rest

问题描述

所以我使用 Spring 5 创建了以下 REST API。

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"
    }
]
}

我可以更新用户的详细信息,但现在我也想更新地址的详细信息。http://localhost:8080/restful-webservices/users/DtvQLbP5uUXaJr4y5n2yxch3BODQCd/addresses/yRK81aJUn2Sfh4JSaWU4svr2YtWgZj

{
    "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;
    }


}

和我的控制器。

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

          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(id, addressDto);
          BeanUtils.copyProperties(updatedAddress,returnValue);

          return returnValue;
    }
}

这是我的回购

如何修复此错误?有任何想法吗?

谢谢,

西奥。

标签: javaspring

解决方案


缺少字符串类型的方法参数的 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
                                       ...

推荐阅读