有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (1) 个答案

  1. # 1 楼答案

    根据您的问题,我不确定您是否希望强制客户端向您发送这些自定义头,或者在服务器的响应中设置这些自定义头。 如果使用JAX-RS(Jersey)的Java实现使用RESTAPI,那么从请求中获取头或在响应中设置头是相当简单的。 下面的示例显示了这两种情况

    @GET
    public Response getClientMessage(@Context HttpContext context) {
        // fetch the header values into a multi valued map
        MultivaluedMap<String, String> headerMap = context.getRequest().getRequestHeaders();
    
        // If the required headers are not found throw a bad request
        //    throw new WebApplicationException(Status.BAD_REQUEST);
    
        // Setting the headers on the response
        return Response.ok()
            .header("X-AppEngine-Country", "US")
            .header("X-AppEngine-City", "California").build();
    }
    

    希望这有帮助