2015-05-18 17 views
5

Ho trovato uno strano comportamento di Spring MVC.Spring MVC return HTTP 406 su URL con punto

ho controller con metodo:

@RequestMapping (value = "/delete/{id:.*}", method = RequestMethod.DELETE) 
public ResponseEntity<Response> delete(@PathVariable (value = "id") final String id) { 
    HttpStatus httpStatus = HttpStatus.OK; 
    final Response responseState = new Response(ResponseConstants.STATUS_SUCCESS); 
    try { 
     POJO pojo = mediaFileDao.findById(id); 
     if (pojo != null) { 
      delete(pojo); 
     } else { 
      httpStatus = HttpStatus.NOT_FOUND; 
      responseState.setError("NOT_FOUND"); 
     } 
    } catch (Exception e) { 
     httpStatus = HttpStatus.INTERNAL_SERVER_ERROR; 
     responseState.setError(e.getMessage()); 
    } 
    return new ResponseEntity<>(responseState, httpStatus); 
} 

Quindi, problema è quando id contiene dot (ex "my_file.wav".) Molla riporta HTTP 406, in ogni caso, ma se id non contiene dot , Spring restituisce responseState (come json) mentre espeto. Ho provato a risolverlo in un modo diverso (aggiungi @ResponseBody, cambia la versione di jackson, esegui il downgrade di Spring a 4.0) ma senza alcun risultato.

Qualcuno può aiutarmi?

UPDATE abilito i registri per la primavera MVN e ho visto questo

ID contiene dot:

DEBUG org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver - Resolving exception from handler [public org.springframework.http.ResponseEntity<my.package.response.Response> my.package.Controller.deleteMediaFile(java.lang.String) throws java.lang.Exception]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation 
DEBUG org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver - Resolving exception from handler [public org.springframework.http.ResponseEntity<my.package.response.Response> my.package.Controller.deleteMediaFile(java.lang.String) throws java.lang.Exception]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation 
DEBUG org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Resolving exception from handler [public org.springframework.http.ResponseEntity<my.package.response.Response> my.package.Controller.deleteMediaFile(java.lang.String) throws java.lang.Exception]: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation 

ID non contiene dot:

DEBUG org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdviceChain - Invoking ResponseBodyAdvice chain for [email protected] 
DEBUG org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdviceChain - After ResponseBodyAdvice chain [email protected] 

SOLUZIONE

Spring does not ignore file extension

SpringMVC: Inconsistent mapping behavior depending on url extension

risposta

2

Nella tua xml servlet, spegnere il suffisso corrispondente Primavera:

<mvc:annotation-driven> 
    <mvc:path-matching registered-suffixes-only="true"/> 
</mvc:annotation-driven> 

questa è una caratteristica che permette ai chiamanti di specificare come vogliono loro contenuto restituito da attaccare come suffisso alla fine dell'URL:

GET /user/bob.json 
GET /use/bob.jsp 

Ma 99 su 100 progetti non utilizzano questa funzione. E causa solo problemi quando ci sono dei punti alla fine dell'URL.

+0

La tua risposta è corretto, ma non mi aiuta. Spring ha ancora inviato HTTP 406. – Vartlok

+0

ora prova a cambiare il requestmapping per essere semplicemente '@RequestMapping (value ="/delete/{id} ", method = RequestMethod.DELETE)' –

+0

E ancora HTTP 406 – Vartlok

0

È necessario disporre di un servizio di negoziazione gestore di contenuti personalizzato definito in questo modo:

<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" /> 

<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> 
    <property name="favorPathExtension" value="false" /> 
</bean> 

è venuto da questo article

Problemi correlati