我试图从响应实体获取正确的 statusCode,但是当我使用 insomnia 发出请求时,
ResponseEntity
显示为对象,但实际的 statusCode 始终为 200
,即使 y 手动将其设置为 400
。
这是我的代码:
@GET
@Path("/tipoArchivos/{id}")
public ResponseEntity<?> getTipoArchivos(@PathParam("id")String idProceso) throws Exception{
Optional<List<TipoArchivoDTO>> respuesta = Optional.ofNullable(tramiteInt.getAttachmentTypes(idProceso));
if (!respuesta.isPresent()) {
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("#status#", "400");
ResponseEntity<String> respuesta1 = new ResponseEntity<String>("El codigo del proceso solo debe contener tres caracteres en mayuscula", responseHeaders, HttpStatus.BAD_REQUEST);
return respuesta1;
}
return new ResponseEntity<List<TipoArchivoDTO>>(respuesta.get(), HttpStatus.OK);
}
我不认为 ResponseEntity 可以与 Dropwizard 一起使用(我假设这就是您基于注释所使用的),因为它来自 Spring -
org.springframework.http.ResponseEntity
。
对于 Dropwizard,您应该使用:javax.ws.rs.core.Response