使用 PutRequest 仅更改 1 个参数? [英] Change only 1 Parameter with PutRequest?

查看:62
本文介绍了使用 PutRequest 仅更改 1 个参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题.是否可以只更改 PutRequest 中的一个参数?我在互联网上没有找到任何关于此的信息.

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);

    }

这是我的 GetRequest,我希望只有参数模板可以更改.

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

推荐答案

正如 Tom 已经提到的,如果您只想部分更新现有实体,那么您必须使用 PATCH HTTP 动词.您可以在此答案中找到更多信息.

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

这篇关于使用 PutRequest 仅更改 1 个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆