Spring boot post方法不起作用

时间:2016-04-27 07:49:10

标签: spring rest curl spring-boot

对于以下控制器我的" GET"方法正在工作,但我的post方法不起作用。错误是"不支持POST方法"

我的创建方法工作正常,但更新无效。

这是我测试的curl命令:

卷曲-H"内容类型:application / json" -X POST -d' [{ id:2, 标题:"我的第一个音符标题更新", 注意:" My First Note更新", createTime:1461737231000, lastUpdateTime:1461737231000 } ]' -u" 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&#39; dispatcherServlet&#39; 2016-04-27 13:15:47.298 INFO 14755 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet:FrameworkServlet&#39; dispatcherServlet&#39 ;:初始化已启动 2016-04-27 13:15:47.336 INFO 14755 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet:FrameworkServlet&#39; dispatcherServlet&#39 ;:初始化完成时间为38毫秒 2016-04-27 13:15:47.433 WARN 14755 --- [nio-8080-exec-1] o.s.web.servlet.PageNotFound:请求方法&#39; POST&#39;不支持

这是我的实体类:

@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 个答案:

答案 0 :(得分:2)

只需删除

即可
headers = "Content-type: application/*"

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

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

headers = "Content-type=application/*"