问题
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