gpt4 book ai didi

java - 休息删除错误请求

转载 作者:行者123 更新时间:2023-11-29 04:46:30 25 4
gpt4 key购买 nike

你能解释一下为什么 DELETE 方法(Edit.js 中的 store.remove())抛出 400 错误请求吗?其他方法效果很好。在标题请求 url 中似乎没问题“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
}
}
});

最佳答案

HttpMethod.DELETE 不能有正文。

这在 RFC 中没有明确说明,但是如果您在 delete 方法中有一个代理服务器,一些代理服务器将拒绝主体。 Spring 降低了标准,并会以Bad Request 拒绝您的查询。

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

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

关于java - 休息删除错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36912331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com