Pergunta

I'm trying to configure a small aplication based on extjs asfrontend and Spring as backend. So to communicate with the server from extjs we need to configure proxy writer at store. My proxy configuration looks as follows:

proxy : {

    type: 'rest',

    reader: {
        type: 'json',
        idProperty: 'id',

        root: 'data',
        totalProperty: 'total',
        successProperty: 'success',
        messageProperty: 'message'
    },

    writer: {
        type: 'json',
        allowSingle: true,
        writeAllFields: true,
        root: 'data'
    },

    api: {
        read:       'countries',
        create:     'country/add',
        update:     'country/update',
        destroy:    'country/delete'
    }
}

And here is the @RequestMapping in a Spring Controller:

@ResponseStatus(value = HttpStatus.OK)
@RequestMapping(value = COUNTRY_PATH + DELETE_PATH + SLASH + DELETE_ID_PARAM, method = RequestMethod.DELETE)
public void delete(@PathVariable(DELETE_ID_PARAM) Integer deleteId, @RequestParam Object data)  {
    System.out.println(data.toString());
    countryService.deleteCountry(deleteId);
}

Tomcat answers everytime "400 Bad request" with description "The request sent by the client was syntactically incorrect."

So, but if I change proxy type to ajax and requestMapping to getting POST requests everything works fine.

Foi útil?

Solução

It looks like you have incorrect syntax for the path variable:

It should be something like

@ResponseStatus(value = HttpStatus.OK)
@RequestMapping(value = COUNTRY_PATH + DELETE_PATH + SLASH + "{" + DELETE_ID_PARAM + "}", method = RequestMethod.DELETE)
public void delete(@PathVariable(DELETE_ID_PARAM) Integer deleteId, @RequestParam  Object data)  {
    System.out.println(data.toString());
    countryService.deleteCountry(deleteId);
}

Also make sure that in the webapplication startup you see a line like

INFO   [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (MSC service thread 1-4) Mapped "{[/myapp/country/delete],methods=[DELETE],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto   public void com.myorg.web.MyController.delete(@PathVariable(DELETE_ID_PARAM) Integer deleteId, @RequestParam  Object data)

The line confirms that the method is registered to handle DELETE methods for this url.

Outras dicas

Look at your Tomcat/conf/web.xml You'll see an entry like this:

- <!--    readonly            Is this context "read only", so HTTP           --> 
- <!--                        commands like PUT and DELETE are               --> 
- <!--                        rejected?  [true]        

Essentially it's typically bad practice to allow put and delete commands on your server since this gives anyone the ability to do malicious things. Tomcat by default protects you by turnning off those.

For restful services it is fairly common practice to map puts/deletes to Post

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top