是的,Java的ResponseEntity支持自定义头部。在创建ResponseEntity对象时,你可以通过HttpHeaders类来设置自定义的HTTP头部。HttpHeaders类允许你添加、修改或删除HTTP头部。以下是一个简单的示例:
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpHeaders;
public class CustomResponseEntity {
public static void main(String[] args) {
HttpHeaders headers = new HttpHeaders();
headers.add("Custom-Header", "CustomValue");
ResponseEntity<String> responseEntity = new ResponseEntity<>("Hello, World!", headers, HttpStatus.OK);
System.out.println("Response Status: " + responseEntity.getStatusCode());
System.out.println("Custom Header: " + responseEntity.getHeaders().getFirst("Custom-Header"));
}
}
在这个示例中,我们创建了一个包含自定义头部(“Custom-Header”)的ResponseEntity对象。然后,我们分别输出响应的状态码和自定义头部的值。