2016-04-28 13 views
0

DELETEメソッド(Edit.jsのstore.remove())が400の不正リクエストをスローする理由を説明できますか?他の方法もうまくいきます。ヘッダーリクエストでは、URLは「http://localhost:8080/Diary/rest/notes/22?_dc=1461837327580」のようです。Rest DELETE Bad Request

私はDELETEメソッドのペイロードに問題があることを知っています。store.remove()にはペイロードとしてIDが含まれています。 IDがURLにすでにあるので、どのように私は、それを無効にして、ボディなしDELETEメソッドを送信することができ

RESTサービスの

@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 
    } 
} 
}); 
+0

あなたが投稿できますお店やプロキシ設定? – lagnat

答えて

0

TestStoreが使用しているストアであれば、あなたの問題ではここにある:

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

私は@DELETE注釈を認識しないので、私は100%確実ではないですが、あなたのコントローラがDELETE期待されている場合、あなたはGETを送っています、 400エラーを説明することができます。

+0

いいえ、その問題のためにDELETEをGETに変更しました。 GETではうまくいきますが、DELETEがある場合はそうなりません。 – shagi

+0

writeAllFieldsのバグかもしれません。その問題を修正したかどうか確認するために削除しようとしましたか? – lagnat

2

ボディには、HttpMethod.DELETEを使用することはできません。

RFCに明示的には記載されていませんが、の削除メソッドにあるものがある場合は、一部のプロキシサーバーがその本体を拒否します。 Springは標準を低下させ、Bad Requestでクエリを拒否します。

問題を解決するために本文と回答を削除してください。

チェックの詳細については、この: Is an entity body allowed for an HTTP DELETE request?