Spring boot post方法不起作用
对于下面的控制器,我的“GET”方法有效,但我的post方法无效。错误为“不支持POST方法” 我的创建方法工作正常,但更新不工作 这是我测试的curl命令: curl-H“内容类型:application/json”-X POST-d”[{ id:2, 标题:“我的第一张便笺标题更新”, 备注:“我的第一个备注更新”, 创建时间:1461737231000, 最新更新时间:1461737231000 } ]“-u”a@a.a“:本地主机:8080/更新Spring boot post方法不起作用,spring,rest,curl,spring-boot,Spring,Rest,Curl,Spring Boot,对于下面的控制器,我的“GET”方法有效,但我的post方法无效。错误为“不支持POST方法” 我的创建方法工作正常,但更新不工作 这是我测试的curl命令: curl-H“内容类型:application/json”-X POST-d”[{ id:2, 标题:“我的第一张便笺标题更新”, 备注:“我的第一个备注更新”, 创建时间:1461737231000, 最新更新时间:1461737231000 } ]“-u”a@a.a“:本地主机:8080/更新 @RestController publ
@RestController
public class GotPrintController {
@Autowired
private NotesRepository notesRepository;
@Autowired
private UserRepository userRepository;
@RequestMapping("/createnotes")
@Transactional
public Notes create() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
System.out.println("auth.getName::"+auth.getName());
Notes notes = new Notes();
notes.setCreateTime(new Date());
notes.setLastUpdateTime(new Date());
notes.setNote("My First Note");
notes.setTitle("My first note title");
notesRepository.save(notes);
return notes;
}
@RequestMapping("/readnotes")
@Transactional
public List<Notes> read(){
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
System.out.println("auth.getName::"+auth.getName());
User users = userRepository.findByEmailId(auth.getName());
List<Notes> notes = (List<Notes>) users.getNotes();
return notes;
}
@RequestMapping(value="/delete/{id}/", method=RequestMethod.POST)
@Transactional
public String delete(@PathVariable Integer id){
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
System.out.println("auth.getName::"+auth.getName());
User users = userRepository.findByEmailId(auth.getName());
List<Notes> notes = (List<Notes>) users.getNotes();
for (Notes notes2 : notes) {
if(notes2.getId() == id){
notesRepository.delete(id);
return "success";
}
}
return "failure";
}
@RequestMapping(value="/update", method=RequestMethod.POST,headers = "Content-type: application/*")
@Transactional
public ResponseEntity<?> update(@RequestBody Notes note){
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
System.out.println("auth.getName::"+auth.getName());
User users = userRepository.findByEmailId(auth.getName());
List<Notes> notes = (List<Notes>) users.getNotes();
for (Notes notes2 : notes) {
if(note.getId() == notes2.getId()){
// note belongs to user update it
notesRepository.save(note);
return new ResponseEntity<>(note, HttpStatus.OK);
}
}
return new ResponseEntity<>("", HttpStatus.NO_CONTENT);
}
}
只需移除
headers = "Content-type: application/*"
如果要接受特定的内容类型,请使用RequestMapping注释的consumes属性
或者,如果要使用headers属性,则语法为:
headers = "Content-type=application/*"
只需移除
headers = "Content-type: application/*"
如果要接受特定的内容类型,请使用RequestMapping注释的consumes属性
或者,如果要使用headers属性,则语法为:
headers = "Content-type=application/*"
将headers属性更改为
headers=“Content type=application/*”
如果不限制内容类型,或将其限制为“application/json”,该怎么办?您可以使用consumes=“application/json”
而不是headers=…
@Edd是正确的:我使用了consumes=“application/json”然后我得到了无法读取文档:无法反序列化com.gotprint.entity.Notes的实例脱离START\u数组标记这是另一个问题:您需要使用双引号而不是单引号和。将headers属性更改为headers=“Content type=application/*”
如果不限制内容类型,或者将其限制为“application/json”?您可以使用consumes=“application/json”
而不是headers=…
@Edd是正确的:我使用了consumes=“application/json”然后我发现无法读取文档:无法反序列化START_数组标记之外的com.gotprint.entity.Notes实例这是另一个问题:您需要使用双引号而不是单引号和。