對於下面的控制器,我的「GET」方法正在工作,但是我的post方法不起作用。 Th錯誤是「不支持POST方法」Spring Boot Post方法不起作用
我的創建方法工作正常,但更新不起作用。
,這是我的測試curl命令:
捲曲-H 「內容類型:應用程序/ JSON」 -X POST -d「[{ ID:2, 標題:「我的第一個音符標題更新」, 注: 「我的第一個注意更新」, createTime:1461737231000, lastUpdateTime:1461737231000 } ]」 -u 「一@ AA」:本地主機:8080 /更新
@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);
}
}
這在嘗試熱POST方法時記錄日誌米捲曲
2016年4月27日13:15:36.927 INFO 14755 --- [主要] com.gotprint.GotprintApplication:在18.481秒(JVM運行23.399) 2016年4月27日13:15開始GotprintApplication :47.298 INFO 14755 --- [nio-8080-exec-1] oaccC [Tomcat]。[localhost]。[/]:初始化Spring FrameworkServlet'dispatcherServlet' 2016-04-27 13:15:47.298信息14755 - - [nio-8080-exec-1] osweb.servlet.DispatcherServlet:FrameworkServlet'dispatcherServlet':開始初始化 2016-04-27 13:15:47.336信息14755 --- [nio-8080-exec-1] os web.servlet.DispatcherServlet:FrameworkServlet'dispatcherServlet':初始化在38毫秒內完成 2016-04-27 13:15:47.433 WARN 14755 --- [nio-8080-exec-1] osweb.servlet.PageNotFound:請求方法'POST'不支持
這裏是我的實體類:
@Entity
public class Notes implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "title")
private String title;
@Column(name = "note")
private String note;
@Column(name = "createTime")
private Date createTime;
@Column(name = "lastUpdateTime")
private Date lastUpdateTime;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getLastUpdateTime() {
return lastUpdateTime;
}
public void setLastUpdateTime(Date lastUpdateTime) {
this.lastUpdateTime = lastUpdateTime;
}
更改標題屬性'頭=「內容類型=應用程序/ *」' – Edd
如果你不限制內容類型,還是將其限制爲「application/json」?你可以使用'consumes =「application/json」'而不是'headers = ...' –
@Edd是正確的:https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework /web/bind/annotation/RequestMapping.html#headers-- –