Spring: Returning empty HTTP Responses with ResponseEntity doesn't work

前端 未结 6 1049
梦如初夏
梦如初夏 2020-12-05 09:47

We are implementing a REST API with Spring (4.1.1.). For certain HTTP requests, we would like to return a head with no body as a response. However, using ResponseEntit

6条回答
  •  甜味超标
    2020-12-05 10:11

    Your method implementation is ambiguous, try the following , edited your code a little bit and used HttpStatus.NO_CONTENT i.e 204 No Content as in place of HttpStatus.OK

    The server has fulfilled the request but does not need to return an entity-body, and might want to return updated metainformation. The response MAY include new or updated metainformation in the form of entity-headers, which if present SHOULD be associated with the requested variant.

    Any value of T will be ignored for 204, but not for 404

      public ResponseEntity taxonomyPackageExists( @PathVariable final String key ) {
                LOG.debug( "taxonomyPackageExists queried with key: {0}", key ); //$NON-NLS-1$
                final TaxonomyKey taxonomyKey = TaxonomyKey.fromString( key );
                LOG.debug( "Taxonomy key created: {0}", taxonomyKey ); //$NON-NLS-1$
    
                if ( this.xbrlInstanceValidator.taxonomyPackageExists( taxonomyKey ) ) {
                    LOG.debug( "Taxonomy package with key: {0} exists.", taxonomyKey ); //$NON-NLS-1$
                     return new ResponseEntity(HttpStatus.NO_CONTENT);
                } else {
                   LOG.debug( "Taxonomy package with key: {0} does NOT exist.", taxonomyKey ); //$NON-NLS-1$
                    return new ResponseEntity( HttpStatus.NOT_FOUND );
                }
    
        }
    

提交回复
热议问题