-2

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.

  • Could you improve your question? I'm not sure to understand what you are trying to do exactly. – D. Lawrence Jan 07 '20 at 12:29
  • Ok I'm sorry. It's got a database called Templates. And in this database there are the parameters id, user_name, template_id and a string template. When I start a PutRequest I shouldn't be allowed to change id template_id and user_name. Only template should be able to be changed. – Doncarlito87 Jan 07 '20 at 12:43
  • What you require is called a PATCH in HTTP. – tom redfern Jan 07 '20 at 13:30

1 Answers1

0

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;
    }
}
Stepan Tsybulski
  • 839
  • 6
  • 16