Change only 1 Parameter with PutRequest?

半世苍凉 提交于 2020-01-26 04:44:06

问题


I have a question. Is it possible to be able to change only one parameter in a PutRequest? I didn't find anything on the internet about this.

    @GetMapping("/templates/{user_name}/{template_id}")
    public Template retrieveTemplate(@PathVariable("user_name") String user_name,@PathVariable("template_id") int template_id)
    {
        return templateRepository.findByTemplateIdAndUserName(template_id, user_name);

    }

This is my GetRequest and I want that only the parameter template can be changed.


回答1:


As Tom has already mentioned, in case you'd like only partially update your existing entity then you must be using PATCH HTTP verb. More information you can find in this answer.

Also here is a small guide telling the difference between these two methods.

Finally, the code snippet that could help you will be looking:

@PatchMapping("/templates/{user_name}/{template_id}")
public Template updateTemplate(@PathVariable("user_name") String user_name, 
                               @PathVariable("template_id") int template_id, 
                               @RequestBody Template template)  {
    return templateService.updateTemplate(template_id, template);
}

@Service
public static class TemplateService {

    @Autowired
    private TemplateRepository templateRepository;

    @Transactional
    public Template updateTemplate(int id, Template updateTemplate) {
        Template foundTemplate = templateRepository.findById(id);
        foundTemplate.setTemplate(updateTemplate.getTemplate());
        return foundTemplate;
    }
}


来源:https://stackoverflow.com/questions/59628434/change-only-1-parameter-with-putrequest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!