泽西岛客户端:太多的Conent-Type标头值

时间:2018-08-01 14:03:44

标签: java json rest header jersey

在我的Web应用程序中,我调用这样的服务:

library(tidyverse)
library(shiny)

# Data
tibble(values = c(15500,16300,18200,28300,23500,23700,
                  31500,35800,34700,36900,40000,44700,
                  53300,55800,69800,89500,1.13E+5,
                  1.53E+5,1.77E+5,1.83E+5,1.99E+5), 
                  year = seq(1990, 2010, 1)) %>% 
  mutate(year = lubridate::ymd(year, truncated = 2L)) ->
  data1

# Shiny app
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
    sliderInput("period", "Year:", 
                min = 1990, max = 2010, 
                value = c(1990, 2010), sep = "")),
mainPanel(plotOutput("ggplot2"))))

server <- function(input, output) {

  data1_subset <- reactive({
      filter(data1,
             year >= lubridate::ymd(input$period[1], truncated = 2L),
             year <= lubridate::ymd(input$period[2], truncated = 2L))
  })


  output$ggplot2 <- renderPlot({
        ggplot(data = data1_subset(), aes(x = year, y = values)) +
          geom_line(color = "red") +
          xlab("Year") +
          theme_minimal()

  })
}

shinyApp(ui = ui, server = server)

服务正确响应,但是当我尝试反序列化响应时,会抛出此异常:

  

org.glassfish.jersey.message.internal.HeaderValueException:太多   “ Content-Type”标头值:“ [application / json; charset = utf-8,   application / json]“

我使用:

Response response = null;
Builder builder = webTarget.request().accept(MediaType.APPLICATION_JSON_TYPE);

if (this.headers != null) { 
    builder.headers(this.headers); // headers is empty in this case
}

response = builder.method(methodName.toString(), Entity.entity(multiPart, multiPart.getMediaType()), Response.class);

if (typeOfT.equals(Response.class)) {
    return (T) response;

} else {
    handleException(response);
    return gson.fromJson(response.readEntity(String.class), typeOfT);
}

1 个答案:

答案 0 :(得分:1)

当响应具有多个Content-Type响应头时,您将收到此错误。您可以通过id response.getHeaders().putSingle()将标头重置为仅使用标头。这将覆盖双头,只剩下一个。

response.getHeaders().putSingle(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
return gson.fromJson(response.readEntity(String.class), typeOfT);