Java 是否仅使用PutRequest更改1个参数?

Java 是否仅使用PutRequest更改1个参数?,java,microservices,Java,Microservices,我有个问题。是否可以在PUT请求中仅更改一个参数? 我在网上找不到这方面的任何信息 @GetMapping("/templates/{user_name}/{template_id}") public Template retrieveTemplate(@PathVariable("user_name") String user_name,@PathVariable("template_id") int template_id) { return temp

我有个问题。是否可以在PUT请求中仅更改一个参数? 我在网上找不到这方面的任何信息

    @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,我希望只有参数模板可以更改。

正如Tom已经提到的,如果您只想部分更新现有实体,那么您必须使用补丁。您可以在中找到更多信息

这里还要说明这两种方法之间的区别

最后,可以帮助您查找的代码片段如下:

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

你能改进你的问题吗?我不确定你到底想做什么。好吧,对不起。它有一个叫做模板的数据库。在这个数据库中有参数id、用户名、模板id和字符串模板。启动PutRequest时,不应允许我更改id模板id和用户名。只能更改模板。您需要的是HTTP中的补丁。@Doncarlito87,我很高兴它对您有所帮助!请你把我的答案标为正确答案好吗?我很想,但我的声誉不超过15个。所以不要让我这么做。啊,没问题:)编码快乐!