Helidon:由于 io.helidon.webclient.WebClientException 引发:请求失败,代码为 302。

huangapple go评论92阅读模式
英文:

Helidon : Caused by: io.helidon.webclient.WebClientException: Request failed with code 302

问题

我正在编写一个Helidon web客户端,遇到以下问题:

由于:io.helidon.webclient.WebClientException: 请求失败,状态码为 302
import java.util.concurrent.ExecutionException;
import io.helidon.media.jsonp.JsonpSupport;
import io.helidon.webclient.WebClient;
import jakarta.enterprise.context.RequestScoped;
import jakarta.json.JsonArray;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;

@Path("/consumer")
@RequestScoped
public class ConsumerController {
	
	private String producerUrl="http://localhost:8080/books-db/";
	
	@GET
	public JsonArray getBookData(){
		System.out.println("producerUrl :: "+producerUrl);
		try {
		WebClient client = WebClient.builder().baseUri(producerUrl)
				.addMediaSupport(JsonpSupport.create()).addHeader("Accept", "application/json").build();
				return client.method("GET").request(JsonArray.class).get();
		}
		catch(InterruptedException | ExecutionException e) {
			e.printStackTrace();
			return null;
		}
	}
}

完整的堆栈跟踪如下:

2023.08.09 12:17:27 INFO io.helidon.common.LogConfig Thread[main,5,main]: 使用类路径配置初始化的日志记录: /logging.properties
2023.08.09 12:17:27 INFO io.helidon.microprofile.server.ServerCdiExtension Thread[main,5,main]: 注册 JAX-RS 应用程序: HelidonMP
2023.08.09 12:17:28 INFO io.helidon.webserver.NettyWebServer Thread[nioEventLoopGroup-2-1,10,main]: 通道 '@default' 已启动:[id: 0xeba066a3, L:/[0:0:0:0:0:0:0:0]:9090]
2023.08.09 12:17:28 INFO io.helidon.microprofile.server.ServerCdiExtension Thread[main,5,main]: 服务器在 1019 毫秒内启动,位于 http://localhost:9090(以及所有其他主机地址)。
2023.08.09 12:17:28 INFO io.helidon.common.HelidonFeatures Thread[features-thread,5,main]: Helidon MP 3.2.2 特性:[CDI, Config, Health, JAX-RS, Metrics, Open API, Server]
producerUrl :: http://localhost:8080/books-db/
java.util.concurrent.ExecutionException: io.helidon.webclient.WebClientException: 请求失败,状态码为 302
  ...

Helidon:由于 io.helidon.webclient.WebClientException 引发:请求失败,代码为 302。

英文:

I'm writing a helidon web-client, I'm getting

Caused by: io.helidon.webclient.WebClientException: Request failed with code 302
import java.util.concurrent.ExecutionException;
import io.helidon.media.jsonp.JsonpSupport;
import io.helidon.webclient.WebClient;
import jakarta.enterprise.context.RequestScoped;
import jakarta.json.JsonArray;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
@Path("/consumer")
@RequestScoped
public class ConsumerController {
	
	private String producerUrl="http://localhost:8080/books-db/";
	
	@GET
	public JsonArray getBookData(){
		System.out.println("producerUrl :: "+producerUrl);
		try {
		WebClient client = WebClient.builder().baseUri(producerUrl)
				.addMediaSupport(JsonpSupport.create()).addHeader("Accept", "application/json").build();
				return client.method("GET").request(JsonArray.class).get();
		}
		catch(InterruptedException | ExecutionException e) {
			e.printStackTrace();
			return null;
		}
	
	}
}

The entire stack trace is:

2023.08.09 12:17:27 INFO io.helidon.common.LogConfig Thread[main,5,main]: Logging at initialization configured using classpath: /logging.properties
2023.08.09 12:17:27 INFO io.helidon.microprofile.server.ServerCdiExtension Thread[main,5,main]: Registering JAX-RS Application: HelidonMP
2023.08.09 12:17:28 INFO io.helidon.webserver.NettyWebServer Thread[nioEventLoopGroup-2-1,10,main]: Channel '@default' started: [id: 0xeba066a3, L:/[0:0:0:0:0:0:0:0]:9090]
2023.08.09 12:17:28 INFO io.helidon.microprofile.server.ServerCdiExtension Thread[main,5,main]: Server started on http://localhost:9090 (and all other host addresses) in 1019 milliseconds (since JVM startup).
2023.08.09 12:17:28 INFO io.helidon.common.HelidonFeatures Thread[features-thread,5,main]: Helidon MP 3.2.2 features: [CDI, Config, Health, JAX-RS, Metrics, Open API, Server]
producerUrl :: http://localhost:8080/books-db/
java.util.concurrent.ExecutionException: io.helidon.webclient.WebClientException: Request failed with code 302
at java.base/java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:396)
at java.base/java.util.concurrent.CompletableFuture.get(CompletableFuture.java:2073)
at io.helidon.common.reactive.Single.get(Single.java:730)
at com.oracle.controller.ConsumerController.getBookData(ConsumerController.java:25)
at com.oracle.controller.ConsumerController$Proxy$_$$_WeldClientProxy.getBookData(Unknown Source)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static$0(ResourceMethodInvocationHandlerFactory.java:52)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:134)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:177)
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:219)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:81)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:478)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:400)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:81)
at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:256)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:248)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:244)
at org.glassfish.jersey.internal.Errors.process(Errors.java:292)
at org.glassfish.jersey.internal.Errors.process(Errors.java:274)
at org.glassfish.jersey.internal.Errors.process(Errors.java:244)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:235)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:684)
at io.helidon.webserver.jersey.JerseySupport$JerseyHandler.lambda$doAccept$6(JerseySupport.java:339)
at io.helidon.common.context.Contexts.runInContext(Contexts.java:117)
at io.helidon.common.context.ContextAwareExecutorImpl.lambda$wrap$7(ContextAwareExecutorImpl.java:154)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: io.helidon.webclient.WebClientException: Request failed with code 302
at io.helidon.webclient.WebClientRequestBuilderImpl.getContentFromClientResponse(WebClientRequestBuilderImpl.java:682)
at io.helidon.common.reactive.MultiMapperPublisher$MapperSubscriber.onNext(MultiMapperPublisher.java:73)
at io.helidon.common.reactive.SingleFromPublisher$SingleSubscriber.onComplete(SingleFromPublisher.java:86)
at io.helidon.common.context.Contexts.runInContext(Contexts.java:117)
at io.helidon.webclient.WebClientRequestBuilderImpl.runInContext(WebClientRequestBuilderImpl.java:641)
at io.helidon.webclient.WebClientRequestBuilderImpl$1.onComplete(WebClientRequestBuilderImpl.java:674)
at io.helidon.common.reactive.DeferredScalarSubscription.complete(DeferredScalarSubscription.java:92)
at io.helidon.common.reactive.MultiFromCompletionStage$CompletionStageSubscription.accept(MultiFromCompletionStage.java:72)
at io.helidon.common.reactive.MultiFromCompletionStage$CompletionStageSubscription.accept(MultiFromCompletionStage.java:52)
at io.helidon.common.reactive.MultiFromCompletionStage$AtomicBiConsumer.accept(MultiFromCompletionStage.java:95)
at io.helidon.common.reactive.MultiFromCompletionStage$AtomicBiConsumer.accept(MultiFromCompletionStage.java:88)
at java.base/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863)
at java.base/java.util.concurrent.CompletableFuture$UniWhenComplete.tryFire(CompletableFuture.java:841)
at java.base/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:510)
at java.base/java.util.concurrent.CompletableFuture.complete(CompletableFuture.java:2147)
at io.helidon.webclient.NettyClientHandler.lambda$channelRead0$7(NettyClientHandler.java:190)
at java.base/java.util.concurrent.CompletableFuture.uniRunNow(CompletableFuture.java:819)
at java.base/java.util.concurrent.CompletableFuture.uniRunStage(CompletableFuture.java:803)
at java.base/java.util.concurrent.CompletableFuture.thenRun(CompletableFuture.java:2195)
at io.helidon.webclient.NettyClientHandler.lambda$channelRead0$9(NettyClientHandler.java:183)
at java.base/java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:863)
at java.base/java.util.concurrent.CompletableFuture.uniWhenCompleteStage(CompletableFuture.java:887)
at java.base/java.util.concurrent.CompletableFuture.whenComplete(CompletableFuture.java:2325)
at java.base/java.util.concurrent.CompletableFuture.whenComplete(CompletableFuture.java:144)
at io.helidon.webclient.NettyClientHandler.channelRead0(NettyClientHandler.java:176)
at io.helidon.webclient.NettyClientHandler.channelRead0(NettyClientHandler.java:61)
at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:99)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:444)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412)
at io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:286)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:442)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412)
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:444)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412)
at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:436)
at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:346)
at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:333)
at io.netty.handler.codec.ByteToMessageDecoder.callDecode(ByteToMessageDecoder.java:454)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:290)
at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:251)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:442)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412)
at io.netty.handler.logging.LoggingHandler.channelRead(LoggingHandler.java:280)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:442)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412)
at io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:286)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:442)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:412)
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:440)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:420)
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919)
at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:166)
at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:788)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:724)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:650)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:562)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
... 5 more

Helidon:由于 io.helidon.webclient.WebClientException 引发:请求失败,代码为 302。

答案1

得分: 1

[302] 用于重定向。

Postman可能默认情况下会跟随重定向,但Helidon WebClient不会。您需要在WebClient.Builder或请求实例上使用.followRedirects(true)

英文:

302 is used for redirects.

Postman probably follows redirects by default, however Helidon WebClient does not. You need to use .followRedirects(true) on either WebClient.Builder or request instance.

huangapple
  • 本文由 发表于 2023年8月9日 15:11:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76865400-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定