首页 文章

Spring boot post方法不起作用

提问于
浏览
0

对于下面的控制器我的“GET”方法正在工作,但我的post方法不起作用 . 错误是“不支持POST方法”

我的create方法工作正常,但更新无效 .

这是我测试的curl命令:

curl -H“Content-Type:application / json”-X POST -d'[{id:2,title:“我的第一个音符 Headers 更新”,注意:“My First Note update”,createTime:1461737231000,lastUpdateTime:1461737231000 }'' - u“a @aa”: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] oaccC [Tomcat] . [localhost] . [/]:初始化Spring FrameworkServlet'dispatcherServlet'2016-04-27 13:15:47.298 INFO 14755 --- [nio-8080-exec- 1] osweb.servlet.DispatcherServlet:FrameworkServlet'dispatcherServlet':初始化已启动2016-04-27 13:15:47.336 INFO 14755 --- [nio-8080-exec-1] osweb.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;
    }

1 回答

  • 2

    只需删除

    headers = "Content-type: application/*"
    

    如果要接受特定内容类型,请使用RequestMapping注释的consumes属性 .

    或者您想要使用headers属性语法是:

    headers = "Content-type=application/*"
    

相关问题