Getting “java.net.ProtocolException: Server redirected too many times” Error

后端 未结 4 2245
误落风尘
误落风尘 2020-12-08 19:20

I\'m making a simple URL request with code like this:

URL url = new URL(webpage);
URLConnection urlConnection = url.openConnection();
InputStream is = urlCon         


        
4条回答
  •  死守一世寂寞
    2020-12-08 20:13

    I had faced the same problem and it took considerable amount of time to understand the problem. So to summarize the problem was in mismatch of headers.

    Consider below being my Resource

      @GET
      @Path("booksMasterData")
      @Produces(Array(core.MediaType.APPLICATION_JSON))
      def booksMasterData(@QueryParam("stockStatus") stockStatus : String): Response = {
     // some logic here to get the books and send it back
        }
    

    And here is client code, which was trying to connect to my above resource

    ClientResponse clientResponse = restClient.resource("http://localhost:8080/booksService").path("rest").path("catalogue").path("booksMasterData").accept("application/boks-master-data+json").get(ClientResponse.class);
    

    And the error was coming on exactly above line.

    What was the problem?

    My Resource was using

    "application/json"

    in

    @Produces annotation

    and my client was using

    accept("application/boks-master-data+json") and this was the problem.

    It took me long to find out this as the error was no where related. Break through was when I tried to access my resource in postman with

    Accept-> "application/json" header

    it worked fine, however with

    Accept-> "application/boks-master-data+json" header it doesnt.

    And again, even Postman was not giving me proper error. The error was too generic. Please see the below image for reference.

提交回复
热议问题