休息删除错误请求

时间:2016-04-28 10:44:20

标签: java rest extjs http-delete

你可以解释一下为什么DELETE方法(Edit.js中的store.remove())会抛出400 Bad请求。其他方法效果很好。在标头请求网址似乎没问题" http://localhost:8080/Diary/rest/notes/22?_dc=1461837327580"。

我知道问题出在DELETE方法的有效负载中,store.remove()包含ID作为有效负载。如何禁用它并发送没有正文的DELETE方法,因为ID已经在URL中

休息服务

@Path("/notes")
public class NoteRestService {
@Context
private UriInfo uriInfo;
@Context
private HttpServletRequest request;



private NoteDaoImpl noteDao = new NoteDaoImpl();
@GET
@Produces("application/json")
public String getNotes(){
    String login = request.getSession(true).getAttribute("login").toString();
    List<Note> notes = noteDao.getUserNotes(login);
    return new Gson().toJson(notes);
}

@POST
@Consumes("application/json")
public Response postNote(Note note){
    String login = request.getSession(true).getAttribute("login").toString();
    note.setUser(login);
    noteDao.persist(note);
    URI noteUri = uriInfo.getAbsolutePathBuilder().path(Long.toString(note.getId())).build();
    return Response.created(noteUri).build();
}

@PUT
@Path("{id}")
@Consumes("application/json")
public Response updateNote(@PathParam("id") String id,Note note){
    String login = request.getSession(true).getAttribute("login").toString();
    Note editNote = noteDao.getNote(Long.parseLong(id));
    note.setCreated(editNote.getCreated());
    note.setUser(login);
    noteDao.update(note);
    return Response.ok().build();
}

@DELETE
@Path("{id}")
public Response deleteNote(@PathParam("id") String id){
    Note note = noteDao.getNote(Long.valueOf(id));
    if (note==null){
        throw new NotFoundException();
    }
    noteDao.delete(Long.parseLong(id));
    return Response.noContent().build();
}
}

EditController.js

Ext.define('MVC.controller.Edit', {
extend: 'Ext.app.Controller',


init: function () {
    this.control({
        'editForm > button#SaveRecord': {
            click: this.onSaveButtonClick
        },
        'editForm > button#DeleteButton': {
            click: this.onDeleteButtonClick
        }
    });
},

onSaveButtonClick: function (btn) {
    //get reference to the form
    var detailView = btn.up('editForm');

    //get the form inputs
    var data = detailView.getValues();

    //see if the record exists
    var store = Ext.getStore('TestStore');
    console.log(data.id);
    var record = store.getById(data.id);

    if (!record) {
        record = Ext.create('MVC.model.Note', {
            title: data.title,
            created: new Date(),
            updated: new Date(),
            text: data.text
        });
        Ext.MessageBox.alert('Created', data.title);

        store.insert(0, record);
        store.sync();
        return;
    }

    record.set(data);

    store.sync();
    //manually update the record
    detailView.updateRecord();
},

onDeleteButtonClick: function (btn) {

    //get reference to the form
    var detailView = btn.up('editForm');

    //get the form inputs
    var data = detailView.getValues();

    var store = Ext.getStore('TestStore');
    var record = store.getById(data.id);
    store.remove(record);
    store.sync();
}
});

UPD:商店

Ext.define('MVC.store.TestStore', {
extend: 'Ext.data.Store',

requires: [
    'MVC.model.Note'
],

storeId: 'TestStore',
model: 'MVC.model.Note',
autoLoad: false,
proxy: {
    type: 'rest',
    url: 'rest/notes',
    actionMethods: {
        create: 'POST',
        read: 'GET',
        update: 'PUT',
        destroy:' DELETE'
    },
    reader: {
        type: 'json',
        rootProperty: 'data'
    },
    writer: {
        type: 'json',
        writeAllFields: true
    }
}
});

2 个答案:

答案 0 :(得分:2)

你身上没有 HttpMethod.DELETE

这在RFC中没有明确说明,但如果您在 delete 方法中有一个代理服务器将拒绝该主体。 Spring会降低标准,并会使用错误请求拒绝您的查询。

删除正文以及解决问题的答案。

检查以获取更多信息: Is an entity body allowed for an HTTP DELETE request?

答案 1 :(得分:0)

如果TestStore是你正在使用的商店,我猜你的问题就在这里:

actionMethods: {
    create: 'POST',
    read: 'GET',
    update: 'PUT',
    destroy: 'GET'
},

我不认识@DELETE注释,所以我不是100%肯定,但如果你的控制器期待DELETE,而你发送GET,那可以解释400错误。

相关问题