【发布时间】:2016-04-27 07:49:10
【问题描述】:
对于下面的控制器,我的“GET”方法有效,但我的 post 方法无效。错误是“不支持 POST 方法”
我的创建方法工作正常,但更新不起作用。
这是我测试的 curl 命令:
curl -H "Content-Type: application/json" -X POST -d '[{ 编号:2, title: "我的第一条笔记标题更新", 注意:“我的第一个笔记更新”, 创建时间:1461737231000, 上次更新时间:1461737231000 } ]' -u "a@a.a":a localhost:8080/update
@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);
}
}
这是尝试从 curl 热 POST 方法时的日志
2016-04-27 13:15:36.927 INFO 14755 --- [main] com.gotprint.GotprintApplication:在 18.481 秒内启动 GotprintApplication(JVM 运行时间为 23.399) 2016-04-27 13:15:47.298 INFO 14755 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]:初始化 Spring FrameworkServlet 'dispatcherServlet' 2016-04-27 13:15:47.298 INFO 14755 --- [nio-8080-exec-1] os.web.servlet.DispatcherServlet:FrameworkServlet 'dispatcherServlet':初始化开始 2016-04-27 13:15:47.336 INFO 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] os.web.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;
}
【问题讨论】:
-
将 headers 属性更改为
headers = "Content-type=application/*" -
如果不限制内容类型,或者限制为 "application/json" 怎么办?您可以使用
consumes = "application/json"代替headers = ... -
我使用了 consumes = "application/json",然后我得到了无法读取文档:无法反序列化 com.gotprint.entity.Notes 实例的 START_ARRAY 令牌
-
这是另一个问题:你需要使用双引号而不是单引号和escape the inner quotes。
标签: spring rest curl spring-boot