将 postman form-data post 请求转换为 java 请求
Convert a postman form-data post request to a java request
我想将 postman 中的以下表单数据请求“转换”为 java 代码(在 java 调用者中)。更准确地说,我正在尝试实现下面显示的 api 的调用者,以便我可以通过 jar 访问端点。问题是我有 2 个字符串数组 to 和 cc 以及 2 个字符串 content 和subject(显然 api 发送电子邮件/暂时忽略附件字段)我不知道如何将这些参数设置为 [=36] 中的表单数据=].
这里是我在 Main post 中的方法 class
public static void myPostRequest() throws IOException {
URL object = new URL(POST_ENDPOINT);
HttpURLConnection postConnection = (HttpURLConnection) object.openConnection();
postConnection.setRequestMethod("POST");
postConnection.setRequestProperty("to", "alice@example.com");
postConnection.setRequestProperty("to", "bob@anotherex.com");
postConnection.setRequestProperty("cc", "alice@example.com");
postConnection.setRequestProperty("cc", "bob@anotherex.com");
postConnection.setRequestProperty("subject", "randomsubject");
postConnection.setRequestProperty("content", "randomContent");
// postConnection.setRequestProperty("contentType", "text/html");
// postConnection.setRequestProperty("Content-Type", "application/json");
postConnection.setDoOutput(true);
OutputStream os = postConnection.getOutputStream();
os.flush();
os.close();
int responseCode = postConnection.getResponseCode();
System.out.println("POST response code " + responseCode);
System.out.println("POST response message" + postConnection.getResponseMessage());
if (responseCode == HttpURLConnection.HTTP_OK) { //success
BufferedReader in = new BufferedReader(new InputStreamReader(postConnection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println("RESPONSE: " + response.toString());
} else {
System.out.println("POST DIDN`T WORK");
}
}
以及其他应用程序中的控制器方法(在 postman 中有效):
@ApiOperation("Send notification email")
@RequestMapping(path = "/email", method = RequestMethod.POST, consumes = {"multipart/form-data"})
private String sendNotificationEmail(@RequestParam("to") String[] to,
@RequestParam("cc") String[] cc,
@RequestParam("subject") String subject,
@RequestParam("content") String content) String contentType,
@RequestParam(name = "attachments") MultipartFile[] attachements
) {
EmailSender.sendMail(new EmailSettings(to, cc, subject, content, null), attachements);
return "Email sent";
}
日志:
"C:\Program Files\Java\jdk1.8.0_211\bin\java.exe" "-javaagent:C:\Program Files\Java\jdk1.8.0_211\jre\lib\rt.jar;C:\GIT\ocademo\target\classes;C:\Users\MARIUS1.POP\.m2\repository\org\springframework\spring-web.3.2\spring-web-5.3.2.jar;C:\Users\MARIUS1.POP\.m2\repository\org\springframework\spring-beans.3.2\spring-beans-5.3.2.jar;C:\Users\MARIUS1.POP\.m2\repository\org\springframework\spring-core.3.2\spring-core-5.3.2.jar;C:\Users\MARIUS1.POP\.m2\repository\org\springframework\spring-jcl.3.2\spring-jcl-5.3.2.jar;C:\Users\MARIUS1.POP\.m2\repository\org\apache\httpcomponents\httpclient.5.2\httpclient-4.5.2.jar;C:\Users\MARIUS1.POP\.m2\repository\org\apache\httpcomponents\httpcore.4.14\httpcore-4.4.14.jar;C:\Users\MARIUS1.POP\.m2\repository\commons-codec\commons-codec.15\commons-codec-1.15.jar;C:\Users\MARIUS1.POP\.m2\repository\org\springframework\spring-webflux.3.2\spring-webflux-5.3.2.jar;C:\Users\MARIUS1.POP\.m2\repository\io\projectreactor\reactor-core.4.1\reactor-core-3.4.1.jar;C:\Users\MARIUS1.POP\.m2\repository\org\reactivestreams\reactive-streams.0.3\reactive-streams-1.0.3.jar;C:\Users\MARIUS1.POP\.m2\repository\io\projectreactor\netty\reactor-netty.0.2\reactor-netty-1.0.2.jar;C:\Users\MARIUS1.POP\.m2\repository\io\projectreactor\netty\reactor-netty-core.0.2\reactor-netty-core-1.0.2.jar;C:\Users\MARIUS1.POP\.m2\repository\io\netty\netty-handler.1.55.Final\netty-handler-4.1.55.Final.jar;C:\Users\MARIUS1.POP\.m2\repository\io\netty\netty-common.1.55.Final\netty-common-4.1.55.Final.jar;C:\Users\MARIUS1.POP\.m2\repository\io\netty\netty-resolver.1.55.Final\netty-resolver-4.1.55.Final.jar;C:\Users\MARIUS1.POP\.m2\repository\io\netty\netty-buffer.1.55.Final\netty-buffer-4.1.55.Final.jar;C:\Users\MARIUS1.POP\.m2\repository\io\netty\netty-transport.1.55.Final\netty-transport-4.1.55.Final.jar;C:\Users\MARIUS1.POP\.m2\repository\io\netty\netty-codec.1.55.Final\netty-codec-4.1.55.Final.jar;C:\Users\MARIUS1.POP\.m2\repository\io\netty\netty-handler-proxy.1.55.Final\netty-handler-proxy-4.1.55.Final.jar;C:\Users\MARIUS1.POP\.m2\repository\io\netty\netty-codec-socks.1.55.Final\netty-codec-socks-4.1.55.Final.jar;C:\Users\MARIUS1.POP\.m2\repository\io\netty\netty-resolver-dns.1.55.Final\netty-resolver-dns-4.1.55.Final.jar;C:\Users\MARIUS1.POP\.m2\repository\io\netty\netty-codec-dns.1.55.Final\netty-codec-dns-4.1.55.Final.jar;C:\Users\MARIUS1.POP\.m2\repository\io\netty\netty-transport-native-epoll.1.55.Final\netty-transport-native-epoll-4.1.55.Final-linux-x86_64.jar;C:\Users\MARIUS1.POP\.m2\repository\io\netty\netty-transport-native-unix-common.1.55.Final\netty-transport-native-unix-common-4.1.55.Final.jar;C:\Users\MARIUS1.POP\.m2\repository\io\projectreactor\netty\reactor-netty-http.0.2\reactor-netty-http-1.0.2.jar;C:\Users\MARIUS1.POP\.m2\repository\io\netty\netty-codec-http.1.55.Final\netty-codec-http-4.1.55.Final.jar;C:\Users\MARIUS1.POP\.m2\repository\io\netty\netty-codec-http2.1.55.Final\netty-codec-http2-4.1.55.Final.jar;C:\Users\MARIUS1.POP\.m2\repository\io\projectreactor\netty\reactor-netty-http-brave.0.2\reactor-netty-http-brave-1.0.2.jar;C:\Users\MARIUS1.POP\.m2\repository\io\zipkin\brave\brave-instrumentation-http.13.2\brave-instrumentation-http-5.13.2.jar;C:\Users\MARIUS1.POP\.m2\repository\io\zipkin\brave\brave.13.2\brave-5.13.2.jar;C:\Users\MARIUS1.POP\.m2\repository\io\zipkin\reporter2\zipkin-reporter-brave.16.1\zipkin-reporter-brave-2.16.1.jar;C:\Users\MARIUS1.POP\.m2\repository\io\zipkin\reporter2\zipkin-reporter.16.1\zipkin-reporter-2.16.1.jar;C:\Users\MARIUS1.POP\.m2\repository\io\zipkin\zipkin2\zipkin.23.0\zipkin-2.23.0.jar;C:\Users\MARIUS1.POP\.m2\repository\com\github\Cloudmersive\Cloudmersive.APIClient.Java\v3.62\Cloudmersive.APIClient.Java-v3.62.jar;C:\Users\MARIUS1.POP\.m2\repository\org\threeten\threetenbp.3.5\threetenbp-1.3.5.jar;C:\Users\MARIUS1.POP\.m2\repository\com\squareup\okhttp\okhttp.7.5\okhttp-2.7.5.jar;C:\Users\MARIUS1.POP\.m2\repository\com\squareup\okio\okio.6.0\okio-1.6.0.jar;C:\Users\MARIUS1.POP\.m2\repository\com\google\code\gson\gson.8.6\gson-2.8.6.jar;C:\Users\MARIUS1.POP\.m2\repository\io\gsonfire\gson-fire.8.0\gson-fire-1.8.0.jar;C:\Users\MARIUS1.POP\.m2\repository\com\squareup\okhttp\logging-interceptor.7.5\logging-interceptor-2.7.5.jar;C:\Users\MARIUS1.POP\.m2\repository\io\swagger\swagger-annotations.5.15\swagger-annotations-1.5.15.jar;C:\Users\MARIUS1.POP\.m2\repository\org\springframework\boot\spring-boot-starter.4.1\spring-boot-starter-2.4.1.jar;C:\Users\MARIUS1.POP\.m2\repository\org\springframework\boot\spring-boot.4.1\spring-boot-2.4.1.jar;C:\Users\MARIUS1.POP\.m2\repository\org\springframework\spring-context.3.2\spring-context-5.3.2.jar;C:\Users\MARIUS1.POP\.m2\repository\org\springframework\spring-aop.3.2\spring-aop-5.3.2.jar;C:\Users\MARIUS1.POP\.m2\repository\org\springframework\spring-expression.3.2\spring-expression-5.3.2.jar;C:\Users\MARIUS1.POP\.m2\repository\org\springframework\boot\spring-boot-autoconfigure.4.1\spring-boot-autoconfigure-2.4.1.jar;C:\Users\MARIUS1.POP\.m2\repository\org\springframework\boot\spring-boot-starter-logging.4.1\spring-boot-starter-logging-2.4.1.jar;C:\Users\MARIUS1.POP\.m2\repository\ch\qos\logback\logback-classic.2.3\logback-classic-1.2.3.jar;C:\Users\MARIUS1.POP\.m2\repository\ch\qos\logback\logback-core.2.3\logback-core-1.2.3.jar;C:\Users\MARIUS1.POP\.m2\repository\org\apache\logging\log4j\log4j-to-slf4j.13.3\log4j-to-slf4j-2.13.3.jar;C:\Users\MARIUS1.POP\.m2\repository\org\apache\logging\log4j\log4j-api.13.3\log4j-api-2.13.3.jar;C:\Users\MARIUS1.POP\.m2\repository\org\slf4j\jul-to-slf4j.7.30\jul-to-slf4j-1.7.30.jar;C:\Users\MARIUS1.POP\.m2\repository\jakarta\annotation\jakarta.annotation-api.3.5\jakarta.annotation-api-1.3.5.jar;C:\Users\MARIUS1.POP\.m2\repository\org\yaml\snakeyaml.27\snakeyaml-1.27.jar;C:\Users\MARIUS1.POP\.m2\repository\org\slf4j\slf4j-api.7.30\slf4j-api-1.7.30.jar" oca.demo.ocademo.Main
14:37:03.760 [main] DEBUG reactor.util.Loggers - Using Slf4j logging framework
14:37:03.789 [main] DEBUG io.netty.util.internal.logging.InternalLoggerFactory - Using SLF4J as the default logging framework
14:37:03.789 [main] DEBUG io.netty.util.internal.PlatformDependent - Platform: Windows
14:37:03.791 [main] DEBUG io.netty.util.internal.PlatformDependent0 - -Dio.netty.noUnsafe: false
14:37:03.791 [main] DEBUG io.netty.util.internal.PlatformDependent0 - Java version: 8
14:37:03.792 [main] DEBUG io.netty.util.internal.PlatformDependent0 - sun.misc.Unsafe.theUnsafe: available
14:37:03.793 [main] DEBUG io.netty.util.internal.PlatformDependent0 - sun.misc.Unsafe.copyMemory: available
14:37:03.793 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.Buffer.address: available
14:37:03.793 [main] DEBUG io.netty.util.internal.PlatformDependent0 - direct buffer constructor: available
14:37:03.794 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.Bits.unaligned: available, true
14:37:03.794 [main] DEBUG io.netty.util.internal.PlatformDependent0 - jdk.internal.misc.Unsafe.allocateUninitializedArray(int): unavailable prior to Java9
14:37:03.794 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.DirectByteBuffer.<init>(long, int): available
14:37:03.794 [main] DEBUG io.netty.util.internal.PlatformDependent - sun.misc.Unsafe: available
14:37:03.794 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.tmpdir: C:\Users\MARIUS1.POP\AppData\Local\Temp (java.io.tmpdir)
14:37:03.794 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.bitMode: 64 (sun.arch.data.model)
14:37:03.795 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.maxDirectMemory: 3791650816 bytes
14:37:03.795 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.uninitializedArrayAllocationThreshold: -1
14:37:03.796 [main] DEBUG io.netty.util.internal.CleanerJava6 - java.nio.ByteBuffer.cleaner(): available
14:37:03.796 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.noPreferDirect: false
14:37:03.809 [main] DEBUG reactor.netty.tcp.TcpResources - [http] resources will use the default LoopResources: DefaultLoopResources {prefix=reactor-http, daemon=true, selectCount=12, workerCount=12}
14:37:03.809 [main] DEBUG reactor.netty.tcp.TcpResources - [http] resources will use the default ConnectionProvider: reactor.netty.resources.DefaultPooledConnectionProvider@614c5515
14:37:03.843 [main] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetection.level: simple
14:37:03.843 [main] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetection.targetRecords: 4
14:37:04.070 [main] DEBUG org.springframework.web.reactive.function.client.ExchangeFunctions - [7e0b85f9] HTTP POST http://mdl07cj001.bt.wan:7003/MIS/Notification/Email/api/notification/email
14:37:04.135 [main] DEBUG reactor.netty.resources.DefaultLoopEpoll - Default Epoll support : false
14:37:04.136 [main] DEBUG reactor.netty.resources.DefaultLoopKQueue - Default KQueue support : false
14:37:04.139 [main] DEBUG io.netty.channel.MultithreadEventLoopGroup - -Dio.netty.eventLoopThreads: 24
14:37:04.152 [main] DEBUG io.netty.util.internal.InternalThreadLocalMap - -Dio.netty.threadLocalMap.stringBuilder.initialSize: 1024
14:37:04.152 [main] DEBUG io.netty.util.internal.InternalThreadLocalMap - -Dio.netty.threadLocalMap.stringBuilder.maxSize: 4096
14:37:04.156 [main] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.noKeySetOptimization: false
14:37:04.157 [main] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.selectorAutoRebuildThreshold: 512
14:37:04.162 [main] DEBUG io.netty.util.internal.PlatformDependent - org.jctools-core.MpscChunkedArrayQueue: available
14:37:04.194 [main] DEBUG io.netty.util.NetUtil - -Djava.net.preferIPv4Stack: false
14:37:04.194 [main] DEBUG io.netty.util.NetUtil - -Djava.net.preferIPv6Addresses: false
14:37:04.228 [main] DEBUG io.netty.util.NetUtilInitializations - Loopback interface: lo (Software Loopback Interface 1, 127.0.0.1)
14:37:04.229 [main] DEBUG io.netty.util.NetUtil - Failed to get SOMAXCONN from sysctl and file \proc\sys\net\core\somaxconn. Default: 200
14:37:04.278 [main] DEBUG io.netty.resolver.dns.DefaultDnsServerAddressStreamProvider - Default DNS servers: [/10.100.101.50:53, /10.100.101.51:53] (sun.net.dns.ResolverConfiguration)
14:37:04.283 [main] DEBUG reactor.netty.resources.PooledConnectionProvider - Creating a new [http] client pool [PoolFactory{evictionInterval=PT0S, leasingStrategy=fifo, maxConnections=500, maxIdleTime=-1, maxLifeTime=-1, metricsEnabled=false, pendingAcquireMaxCount=1000, pendingAcquireTimeout=45000}] for [mdl07cj001.bt.wan:7003]
14:37:04.317 [main] DEBUG io.netty.channel.DefaultChannelId - -Dio.netty.processId: 15532 (auto-detected)
14:37:04.356 [main] DEBUG io.netty.channel.DefaultChannelId - -Dio.netty.machineId: f4:93:9f:ff:fe:f6:06:8a (auto-detected)
14:37:04.373 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.numHeapArenas: 24
14:37:04.373 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.numDirectArenas: 24
14:37:04.373 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.pageSize: 8192
14:37:04.373 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.maxOrder: 11
14:37:04.373 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.chunkSize: 16777216
14:37:04.373 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.smallCacheSize: 256
14:37:04.373 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.normalCacheSize: 64
14:37:04.373 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.maxCachedBufferCapacity: 32768
14:37:04.373 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.cacheTrimInterval: 8192
14:37:04.373 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.cacheTrimIntervalMillis: 0
14:37:04.373 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.useCacheForAllThreads: true
14:37:04.373 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.maxCachedByteBuffersPerChunk: 1023
14:37:04.379 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.allocator.type: pooled
14:37:04.379 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.threadLocalDirectBufferSize: 0
14:37:04.379 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.maxThreadLocalCharBufferSize: 16384
14:37:04.392 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [id: 0xacaa8747] Created a new pooled channel, now 1 active connections and 0 inactive connections
14:37:04.409 [reactor-http-nio-2] DEBUG io.netty.buffer.AbstractByteBuf - -Dio.netty.buffer.checkAccessible: true
14:37:04.409 [reactor-http-nio-2] DEBUG io.netty.buffer.AbstractByteBuf - -Dio.netty.buffer.checkBounds: true
14:37:04.410 [reactor-http-nio-2] DEBUG io.netty.util.ResourceLeakDetectorFactory - Loaded default ResourceLeakDetector: io.netty.util.ResourceLeakDetector@22f23501
14:37:04.413 [reactor-http-nio-2] DEBUG reactor.netty.transport.TransportConfig - [id: 0xacaa8747] Initialized pipeline DefaultChannelPipeline{(reactor.left.httpCodec = io.netty.handler.codec.http.HttpClientCodec), (reactor.left.httpDecompressor = io.netty.handler.codec.http.HttpContentDecompressor), (reactor.right.reactiveBridge = reactor.netty.channel.ChannelOperationsHandler)}
14:37:04.438 [reactor-http-nio-1] DEBUG io.netty.util.ResourceLeakDetectorFactory - Loaded default ResourceLeakDetector: io.netty.util.ResourceLeakDetector@492aff7f
14:37:04.439 [reactor-http-nio-1] DEBUG io.netty.resolver.dns.DnsQueryContext - [id: 0xa3a7d02f] WRITE: UDP, [23067: /10.100.101.50:53], DefaultDnsQuestion(mdl07cj001.bt.wan. IN A)
14:37:04.443 [reactor-http-nio-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxCapacityPerThread: 4096
14:37:04.443 [reactor-http-nio-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxSharedCapacityFactor: 2
14:37:04.443 [reactor-http-nio-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.linkCapacity: 16
14:37:04.443 [reactor-http-nio-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.ratio: 8
14:37:04.443 [reactor-http-nio-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.delayedQueue.ratio: 8
14:37:04.456 [reactor-http-nio-1] DEBUG io.netty.resolver.dns.DnsNameResolver - [id: 0xa3a7d02f] RECEIVED: UDP [23067: /10.100.101.50:53], DatagramDnsResponse(from: /10.100.101.50:53, to: /0:0:0:0:0:0:0:0:55442, 23067, QUERY(0), NoError(0), RD AA RA)
DefaultDnsQuestion(mdl07cj001.bt.wan. IN A)
DefaultDnsRawRecord(mdl07cj001.bt.wan. 3600 IN A 4B)
DefaultDnsRawRecord(OPT flags:0 udp:4000 0B)
14:37:04.460 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003] Registering pool release on close event for channel
14:37:04.460 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003] Channel connected, now 1 active connections and 0 inactive connections
14:37:04.461 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003] onStateChange(PooledConnection{channel=[id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003]}, [connected])
14:37:04.467 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003] onStateChange(GET{uri=/, connection=PooledConnection{channel=[id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003]}}, [configured])
14:37:04.468 [reactor-http-nio-2] DEBUG reactor.netty.http.client.HttpClientConnect - [id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003] Handler is being applied: {uri=http://mdl07cj001.bt.wan:7003/MIS/Notification/Email/api/notification/email, method=POST}
14:37:04.470 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003] onStateChange(POST{uri=/MIS/Notification/Email/api/notification/email, connection=PooledConnection{channel=[id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003]}}, [request_prepared])
14:37:04.474 [reactor-http-nio-2] DEBUG org.springframework.http.codec.FormHttpMessageWriter - [7e0b85f9] Writing form fields [to, cc, subject, content, contentType] (content masked)
14:37:04.488 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003] onStateChange(POST{uri=/MIS/Notification/Email/api/notification/email, connection=PooledConnection{channel=[id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003]}}, [request_sent])
14:37:04.517 [reactor-http-nio-2] DEBUG reactor.netty.http.client.HttpClientOperations - [id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003] Received response (auto-read:false) : [Cache-Control=no-cache, no-store, max-age=0, must-revalidate, Date=Mon, 18 Jan 2021 12:37:04 GMT, Pragma=no-cache, Transfer-Encoding=chunked, Expires=0, X-ORACLE-DMS-ECID=57445be9-bcb8-496b-bd25-4986e33f3e23-00000135, X-ORACLE-DMS-RID=0, X-Frame-Options=DENY, Accept=multipart/form-data, X-Content-Type-Options=nosniff, X-XSS-Protection=1; mode=block]
14:37:04.517 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003] onStateChange(POST{uri=/MIS/Notification/Email/api/notification/email, connection=PooledConnection{channel=[id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003]}}, [response_received])
14:37:04.519 [reactor-http-nio-2] DEBUG org.springframework.web.reactive.function.client.ExchangeFunctions - [7e0b85f9] Response 415 UNSUPPORTED_MEDIA_TYPE
14:37:04.530 [reactor-http-nio-2] DEBUG reactor.netty.channel.FluxReceive - [id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003] FluxReceive{pending=0, cancelled=false, inboundDone=false, inboundError=null}: subscribing inbound receiver
14:37:04.531 [reactor-http-nio-2] DEBUG reactor.netty.http.client.HttpClientOperations - [id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003] Received last HTTP packet
14:37:04.540 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003] onStateChange(POST{uri=/MIS/Notification/Email/api/notification/email, connection=PooledConnection{channel=[id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003]}}, [response_completed])
Exception in thread "main" org.springframework.web.reactive.function.client.WebClientResponseException$UnsupportedMediaType: 415 Unsupported Media Type from POST http://mdl07cj001.bt.wan:7003/MIS/Notification/Email/api/notification/email
at org.springframework.web.reactive.function.client.WebClientResponseException.create(WebClientResponseException.java:195)
14:37:04.541 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003] onStateChange(POST{uri=/MIS/Notification/Email/api/notification/email, connection=PooledConnection{channel=[id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003]}}, [disconnecting])
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
14:37:04.541 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003] Releasing channel
|_ checkpoint ⇢ 415 from POST http://mdl07cj001.bt.wan:7003/MIS/Notification/Email/api/notification/email [DefaultWebClient]
Stack trace:
at org.springframework.web.reactive.function.client.WebClientResponseException.create(WebClientResponseException.java:195)
at org.springframework.web.reactive.function.client.DefaultClientResponse.lambda$createException(DefaultClientResponse.java:216)
at reactor.core.publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:106)
at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onNext(FluxOnErrorResume.java:79)
at reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1784)
at reactor.core.publisher.FluxDefaultIfEmpty$DefaultIfEmptySubscriber.onComplete(FluxDefaultIfEmpty.java:107)
at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onComplete(FluxMapFuseable.java:150)
at reactor.core.publisher.FluxContextWrite$ContextWriteSubscriber.onComplete(FluxContextWrite.java:126)
at reactor.core.publisher.FluxMapFuseable$MapFuseableConditionalSubscriber.onComplete(FluxMapFuseable.java:344)
at reactor.core.publisher.FluxFilterFuseable$FilterFuseableConditionalSubscriber.onComplete(FluxFilterFuseable.java:391)
at reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1785)
at reactor.core.publisher.MonoCollect$CollectSubscriber.onComplete(MonoCollect.java:159)
at reactor.core.publisher.FluxMap$MapSubscriber.onComplete(FluxMap.java:142)
at reactor.core.publisher.FluxPeek$PeekSubscriber.onComplete(FluxPeek.java:259)
at reactor.core.publisher.FluxMap$MapSubscriber.onComplete(FluxMap.java:142)
at reactor.netty.channel.FluxReceive.onInboundComplete(FluxReceive.java:383)
at reactor.netty.channel.ChannelOperations.onInboundComplete(ChannelOperations.java:396)
at reactor.netty.channel.ChannelOperations.terminate(ChannelOperations.java:452)
at reactor.netty.http.client.HttpClientOperations.onInboundNext(HttpClientOperations.java:664)
at reactor.netty.channel.ChannelOperationsHandler.channelRead(ChannelOperationsHandler.java:94)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:436)
at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:324)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:296)
at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:251)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
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:719)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
at io.netty.util.concurrent.SingleThreadEventExecutor.run(SingleThreadEventExecutor.java:989)
at io.netty.util.internal.ThreadExecutorMap.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:748)
Suppressed: java.lang.Exception: #block terminated with an error
at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:99)
at reactor.core.publisher.Mono.block(Mono.java:1703)
at oca.demo.ocademo.GetAndPost.newPostMethod(GetAndPost.java:120)
at oca.demo.ocademo.Main.main(Main.java:8)
Process finished with exit code 1
如果您正在使用 spring,正如我在 RequestMapping 注释中看到的那样,您为什么不使用 restTemplate 或 webclient? (取决于您使用的 spring 版本)。会容易很多。
Spring 4 及以下的 Rest 模板,命令式
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> map= new LinkedMultiValueMap<>();
map.add("to", "alice@example.com");
map.add("to", "bob@anotherex.com");
map.add("cc", "alice@example.com");
map.add("cc", "bob@anotherex.com");
map.add("subject", "randomsubject");
map.add("content", "randomContent");
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
RestTemplate restTemplate= new RestTemplateBuilder().rootUri("http://localhost:8080").build();
restTemplate.postForLocation( "url", request);
WebClient for Spring 5+,反应式需要 WebFlux
WebClient client = WebClient.builder()
.baseUrl("http://localhost:8080")
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE)
.build();
client.post()
.uri("/api/notification/email...")
.body(BodyInserters.fromFormData("to", "alice@example.com")
.with("to", "bob@anotherex.com")
.with("cc", "alice@example.com")
.with("cc", "bob@anotherex.com")
.with("subject", "randomsubject")
.with("content", "randomContent"))
.retrieve()
.toBodilessEntity()
.block();
在这两种情况下都在内部使用多值映射,因此您可以使用相同的键添加各种值,例如 to 和 cc
Postman 允许将请求转换为其他语言,只需单击请求下方的代码 link url。
我想将 postman 中的以下表单数据请求“转换”为 java 代码(在 java 调用者中)。更准确地说,我正在尝试实现下面显示的 api 的调用者,以便我可以通过 jar 访问端点。问题是我有 2 个字符串数组 to 和 cc 以及 2 个字符串 content 和subject(显然 api 发送电子邮件/暂时忽略附件字段)我不知道如何将这些参数设置为 [=36] 中的表单数据=].
这里是我在 Main post 中的方法 class
public static void myPostRequest() throws IOException {
URL object = new URL(POST_ENDPOINT);
HttpURLConnection postConnection = (HttpURLConnection) object.openConnection();
postConnection.setRequestMethod("POST");
postConnection.setRequestProperty("to", "alice@example.com");
postConnection.setRequestProperty("to", "bob@anotherex.com");
postConnection.setRequestProperty("cc", "alice@example.com");
postConnection.setRequestProperty("cc", "bob@anotherex.com");
postConnection.setRequestProperty("subject", "randomsubject");
postConnection.setRequestProperty("content", "randomContent");
// postConnection.setRequestProperty("contentType", "text/html");
// postConnection.setRequestProperty("Content-Type", "application/json");
postConnection.setDoOutput(true);
OutputStream os = postConnection.getOutputStream();
os.flush();
os.close();
int responseCode = postConnection.getResponseCode();
System.out.println("POST response code " + responseCode);
System.out.println("POST response message" + postConnection.getResponseMessage());
if (responseCode == HttpURLConnection.HTTP_OK) { //success
BufferedReader in = new BufferedReader(new InputStreamReader(postConnection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println("RESPONSE: " + response.toString());
} else {
System.out.println("POST DIDN`T WORK");
}
}
以及其他应用程序中的控制器方法(在 postman 中有效):
@ApiOperation("Send notification email")
@RequestMapping(path = "/email", method = RequestMethod.POST, consumes = {"multipart/form-data"})
private String sendNotificationEmail(@RequestParam("to") String[] to,
@RequestParam("cc") String[] cc,
@RequestParam("subject") String subject,
@RequestParam("content") String content) String contentType,
@RequestParam(name = "attachments") MultipartFile[] attachements
) {
EmailSender.sendMail(new EmailSettings(to, cc, subject, content, null), attachements);
return "Email sent";
}
日志:
"C:\Program Files\Java\jdk1.8.0_211\bin\java.exe" "-javaagent:C:\Program Files\Java\jdk1.8.0_211\jre\lib\rt.jar;C:\GIT\ocademo\target\classes;C:\Users\MARIUS1.POP\.m2\repository\org\springframework\spring-web.3.2\spring-web-5.3.2.jar;C:\Users\MARIUS1.POP\.m2\repository\org\springframework\spring-beans.3.2\spring-beans-5.3.2.jar;C:\Users\MARIUS1.POP\.m2\repository\org\springframework\spring-core.3.2\spring-core-5.3.2.jar;C:\Users\MARIUS1.POP\.m2\repository\org\springframework\spring-jcl.3.2\spring-jcl-5.3.2.jar;C:\Users\MARIUS1.POP\.m2\repository\org\apache\httpcomponents\httpclient.5.2\httpclient-4.5.2.jar;C:\Users\MARIUS1.POP\.m2\repository\org\apache\httpcomponents\httpcore.4.14\httpcore-4.4.14.jar;C:\Users\MARIUS1.POP\.m2\repository\commons-codec\commons-codec.15\commons-codec-1.15.jar;C:\Users\MARIUS1.POP\.m2\repository\org\springframework\spring-webflux.3.2\spring-webflux-5.3.2.jar;C:\Users\MARIUS1.POP\.m2\repository\io\projectreactor\reactor-core.4.1\reactor-core-3.4.1.jar;C:\Users\MARIUS1.POP\.m2\repository\org\reactivestreams\reactive-streams.0.3\reactive-streams-1.0.3.jar;C:\Users\MARIUS1.POP\.m2\repository\io\projectreactor\netty\reactor-netty.0.2\reactor-netty-1.0.2.jar;C:\Users\MARIUS1.POP\.m2\repository\io\projectreactor\netty\reactor-netty-core.0.2\reactor-netty-core-1.0.2.jar;C:\Users\MARIUS1.POP\.m2\repository\io\netty\netty-handler.1.55.Final\netty-handler-4.1.55.Final.jar;C:\Users\MARIUS1.POP\.m2\repository\io\netty\netty-common.1.55.Final\netty-common-4.1.55.Final.jar;C:\Users\MARIUS1.POP\.m2\repository\io\netty\netty-resolver.1.55.Final\netty-resolver-4.1.55.Final.jar;C:\Users\MARIUS1.POP\.m2\repository\io\netty\netty-buffer.1.55.Final\netty-buffer-4.1.55.Final.jar;C:\Users\MARIUS1.POP\.m2\repository\io\netty\netty-transport.1.55.Final\netty-transport-4.1.55.Final.jar;C:\Users\MARIUS1.POP\.m2\repository\io\netty\netty-codec.1.55.Final\netty-codec-4.1.55.Final.jar;C:\Users\MARIUS1.POP\.m2\repository\io\netty\netty-handler-proxy.1.55.Final\netty-handler-proxy-4.1.55.Final.jar;C:\Users\MARIUS1.POP\.m2\repository\io\netty\netty-codec-socks.1.55.Final\netty-codec-socks-4.1.55.Final.jar;C:\Users\MARIUS1.POP\.m2\repository\io\netty\netty-resolver-dns.1.55.Final\netty-resolver-dns-4.1.55.Final.jar;C:\Users\MARIUS1.POP\.m2\repository\io\netty\netty-codec-dns.1.55.Final\netty-codec-dns-4.1.55.Final.jar;C:\Users\MARIUS1.POP\.m2\repository\io\netty\netty-transport-native-epoll.1.55.Final\netty-transport-native-epoll-4.1.55.Final-linux-x86_64.jar;C:\Users\MARIUS1.POP\.m2\repository\io\netty\netty-transport-native-unix-common.1.55.Final\netty-transport-native-unix-common-4.1.55.Final.jar;C:\Users\MARIUS1.POP\.m2\repository\io\projectreactor\netty\reactor-netty-http.0.2\reactor-netty-http-1.0.2.jar;C:\Users\MARIUS1.POP\.m2\repository\io\netty\netty-codec-http.1.55.Final\netty-codec-http-4.1.55.Final.jar;C:\Users\MARIUS1.POP\.m2\repository\io\netty\netty-codec-http2.1.55.Final\netty-codec-http2-4.1.55.Final.jar;C:\Users\MARIUS1.POP\.m2\repository\io\projectreactor\netty\reactor-netty-http-brave.0.2\reactor-netty-http-brave-1.0.2.jar;C:\Users\MARIUS1.POP\.m2\repository\io\zipkin\brave\brave-instrumentation-http.13.2\brave-instrumentation-http-5.13.2.jar;C:\Users\MARIUS1.POP\.m2\repository\io\zipkin\brave\brave.13.2\brave-5.13.2.jar;C:\Users\MARIUS1.POP\.m2\repository\io\zipkin\reporter2\zipkin-reporter-brave.16.1\zipkin-reporter-brave-2.16.1.jar;C:\Users\MARIUS1.POP\.m2\repository\io\zipkin\reporter2\zipkin-reporter.16.1\zipkin-reporter-2.16.1.jar;C:\Users\MARIUS1.POP\.m2\repository\io\zipkin\zipkin2\zipkin.23.0\zipkin-2.23.0.jar;C:\Users\MARIUS1.POP\.m2\repository\com\github\Cloudmersive\Cloudmersive.APIClient.Java\v3.62\Cloudmersive.APIClient.Java-v3.62.jar;C:\Users\MARIUS1.POP\.m2\repository\org\threeten\threetenbp.3.5\threetenbp-1.3.5.jar;C:\Users\MARIUS1.POP\.m2\repository\com\squareup\okhttp\okhttp.7.5\okhttp-2.7.5.jar;C:\Users\MARIUS1.POP\.m2\repository\com\squareup\okio\okio.6.0\okio-1.6.0.jar;C:\Users\MARIUS1.POP\.m2\repository\com\google\code\gson\gson.8.6\gson-2.8.6.jar;C:\Users\MARIUS1.POP\.m2\repository\io\gsonfire\gson-fire.8.0\gson-fire-1.8.0.jar;C:\Users\MARIUS1.POP\.m2\repository\com\squareup\okhttp\logging-interceptor.7.5\logging-interceptor-2.7.5.jar;C:\Users\MARIUS1.POP\.m2\repository\io\swagger\swagger-annotations.5.15\swagger-annotations-1.5.15.jar;C:\Users\MARIUS1.POP\.m2\repository\org\springframework\boot\spring-boot-starter.4.1\spring-boot-starter-2.4.1.jar;C:\Users\MARIUS1.POP\.m2\repository\org\springframework\boot\spring-boot.4.1\spring-boot-2.4.1.jar;C:\Users\MARIUS1.POP\.m2\repository\org\springframework\spring-context.3.2\spring-context-5.3.2.jar;C:\Users\MARIUS1.POP\.m2\repository\org\springframework\spring-aop.3.2\spring-aop-5.3.2.jar;C:\Users\MARIUS1.POP\.m2\repository\org\springframework\spring-expression.3.2\spring-expression-5.3.2.jar;C:\Users\MARIUS1.POP\.m2\repository\org\springframework\boot\spring-boot-autoconfigure.4.1\spring-boot-autoconfigure-2.4.1.jar;C:\Users\MARIUS1.POP\.m2\repository\org\springframework\boot\spring-boot-starter-logging.4.1\spring-boot-starter-logging-2.4.1.jar;C:\Users\MARIUS1.POP\.m2\repository\ch\qos\logback\logback-classic.2.3\logback-classic-1.2.3.jar;C:\Users\MARIUS1.POP\.m2\repository\ch\qos\logback\logback-core.2.3\logback-core-1.2.3.jar;C:\Users\MARIUS1.POP\.m2\repository\org\apache\logging\log4j\log4j-to-slf4j.13.3\log4j-to-slf4j-2.13.3.jar;C:\Users\MARIUS1.POP\.m2\repository\org\apache\logging\log4j\log4j-api.13.3\log4j-api-2.13.3.jar;C:\Users\MARIUS1.POP\.m2\repository\org\slf4j\jul-to-slf4j.7.30\jul-to-slf4j-1.7.30.jar;C:\Users\MARIUS1.POP\.m2\repository\jakarta\annotation\jakarta.annotation-api.3.5\jakarta.annotation-api-1.3.5.jar;C:\Users\MARIUS1.POP\.m2\repository\org\yaml\snakeyaml.27\snakeyaml-1.27.jar;C:\Users\MARIUS1.POP\.m2\repository\org\slf4j\slf4j-api.7.30\slf4j-api-1.7.30.jar" oca.demo.ocademo.Main
14:37:03.760 [main] DEBUG reactor.util.Loggers - Using Slf4j logging framework
14:37:03.789 [main] DEBUG io.netty.util.internal.logging.InternalLoggerFactory - Using SLF4J as the default logging framework
14:37:03.789 [main] DEBUG io.netty.util.internal.PlatformDependent - Platform: Windows
14:37:03.791 [main] DEBUG io.netty.util.internal.PlatformDependent0 - -Dio.netty.noUnsafe: false
14:37:03.791 [main] DEBUG io.netty.util.internal.PlatformDependent0 - Java version: 8
14:37:03.792 [main] DEBUG io.netty.util.internal.PlatformDependent0 - sun.misc.Unsafe.theUnsafe: available
14:37:03.793 [main] DEBUG io.netty.util.internal.PlatformDependent0 - sun.misc.Unsafe.copyMemory: available
14:37:03.793 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.Buffer.address: available
14:37:03.793 [main] DEBUG io.netty.util.internal.PlatformDependent0 - direct buffer constructor: available
14:37:03.794 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.Bits.unaligned: available, true
14:37:03.794 [main] DEBUG io.netty.util.internal.PlatformDependent0 - jdk.internal.misc.Unsafe.allocateUninitializedArray(int): unavailable prior to Java9
14:37:03.794 [main] DEBUG io.netty.util.internal.PlatformDependent0 - java.nio.DirectByteBuffer.<init>(long, int): available
14:37:03.794 [main] DEBUG io.netty.util.internal.PlatformDependent - sun.misc.Unsafe: available
14:37:03.794 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.tmpdir: C:\Users\MARIUS1.POP\AppData\Local\Temp (java.io.tmpdir)
14:37:03.794 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.bitMode: 64 (sun.arch.data.model)
14:37:03.795 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.maxDirectMemory: 3791650816 bytes
14:37:03.795 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.uninitializedArrayAllocationThreshold: -1
14:37:03.796 [main] DEBUG io.netty.util.internal.CleanerJava6 - java.nio.ByteBuffer.cleaner(): available
14:37:03.796 [main] DEBUG io.netty.util.internal.PlatformDependent - -Dio.netty.noPreferDirect: false
14:37:03.809 [main] DEBUG reactor.netty.tcp.TcpResources - [http] resources will use the default LoopResources: DefaultLoopResources {prefix=reactor-http, daemon=true, selectCount=12, workerCount=12}
14:37:03.809 [main] DEBUG reactor.netty.tcp.TcpResources - [http] resources will use the default ConnectionProvider: reactor.netty.resources.DefaultPooledConnectionProvider@614c5515
14:37:03.843 [main] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetection.level: simple
14:37:03.843 [main] DEBUG io.netty.util.ResourceLeakDetector - -Dio.netty.leakDetection.targetRecords: 4
14:37:04.070 [main] DEBUG org.springframework.web.reactive.function.client.ExchangeFunctions - [7e0b85f9] HTTP POST http://mdl07cj001.bt.wan:7003/MIS/Notification/Email/api/notification/email
14:37:04.135 [main] DEBUG reactor.netty.resources.DefaultLoopEpoll - Default Epoll support : false
14:37:04.136 [main] DEBUG reactor.netty.resources.DefaultLoopKQueue - Default KQueue support : false
14:37:04.139 [main] DEBUG io.netty.channel.MultithreadEventLoopGroup - -Dio.netty.eventLoopThreads: 24
14:37:04.152 [main] DEBUG io.netty.util.internal.InternalThreadLocalMap - -Dio.netty.threadLocalMap.stringBuilder.initialSize: 1024
14:37:04.152 [main] DEBUG io.netty.util.internal.InternalThreadLocalMap - -Dio.netty.threadLocalMap.stringBuilder.maxSize: 4096
14:37:04.156 [main] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.noKeySetOptimization: false
14:37:04.157 [main] DEBUG io.netty.channel.nio.NioEventLoop - -Dio.netty.selectorAutoRebuildThreshold: 512
14:37:04.162 [main] DEBUG io.netty.util.internal.PlatformDependent - org.jctools-core.MpscChunkedArrayQueue: available
14:37:04.194 [main] DEBUG io.netty.util.NetUtil - -Djava.net.preferIPv4Stack: false
14:37:04.194 [main] DEBUG io.netty.util.NetUtil - -Djava.net.preferIPv6Addresses: false
14:37:04.228 [main] DEBUG io.netty.util.NetUtilInitializations - Loopback interface: lo (Software Loopback Interface 1, 127.0.0.1)
14:37:04.229 [main] DEBUG io.netty.util.NetUtil - Failed to get SOMAXCONN from sysctl and file \proc\sys\net\core\somaxconn. Default: 200
14:37:04.278 [main] DEBUG io.netty.resolver.dns.DefaultDnsServerAddressStreamProvider - Default DNS servers: [/10.100.101.50:53, /10.100.101.51:53] (sun.net.dns.ResolverConfiguration)
14:37:04.283 [main] DEBUG reactor.netty.resources.PooledConnectionProvider - Creating a new [http] client pool [PoolFactory{evictionInterval=PT0S, leasingStrategy=fifo, maxConnections=500, maxIdleTime=-1, maxLifeTime=-1, metricsEnabled=false, pendingAcquireMaxCount=1000, pendingAcquireTimeout=45000}] for [mdl07cj001.bt.wan:7003]
14:37:04.317 [main] DEBUG io.netty.channel.DefaultChannelId - -Dio.netty.processId: 15532 (auto-detected)
14:37:04.356 [main] DEBUG io.netty.channel.DefaultChannelId - -Dio.netty.machineId: f4:93:9f:ff:fe:f6:06:8a (auto-detected)
14:37:04.373 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.numHeapArenas: 24
14:37:04.373 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.numDirectArenas: 24
14:37:04.373 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.pageSize: 8192
14:37:04.373 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.maxOrder: 11
14:37:04.373 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.chunkSize: 16777216
14:37:04.373 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.smallCacheSize: 256
14:37:04.373 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.normalCacheSize: 64
14:37:04.373 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.maxCachedBufferCapacity: 32768
14:37:04.373 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.cacheTrimInterval: 8192
14:37:04.373 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.cacheTrimIntervalMillis: 0
14:37:04.373 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.useCacheForAllThreads: true
14:37:04.373 [main] DEBUG io.netty.buffer.PooledByteBufAllocator - -Dio.netty.allocator.maxCachedByteBuffersPerChunk: 1023
14:37:04.379 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.allocator.type: pooled
14:37:04.379 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.threadLocalDirectBufferSize: 0
14:37:04.379 [main] DEBUG io.netty.buffer.ByteBufUtil - -Dio.netty.maxThreadLocalCharBufferSize: 16384
14:37:04.392 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [id: 0xacaa8747] Created a new pooled channel, now 1 active connections and 0 inactive connections
14:37:04.409 [reactor-http-nio-2] DEBUG io.netty.buffer.AbstractByteBuf - -Dio.netty.buffer.checkAccessible: true
14:37:04.409 [reactor-http-nio-2] DEBUG io.netty.buffer.AbstractByteBuf - -Dio.netty.buffer.checkBounds: true
14:37:04.410 [reactor-http-nio-2] DEBUG io.netty.util.ResourceLeakDetectorFactory - Loaded default ResourceLeakDetector: io.netty.util.ResourceLeakDetector@22f23501
14:37:04.413 [reactor-http-nio-2] DEBUG reactor.netty.transport.TransportConfig - [id: 0xacaa8747] Initialized pipeline DefaultChannelPipeline{(reactor.left.httpCodec = io.netty.handler.codec.http.HttpClientCodec), (reactor.left.httpDecompressor = io.netty.handler.codec.http.HttpContentDecompressor), (reactor.right.reactiveBridge = reactor.netty.channel.ChannelOperationsHandler)}
14:37:04.438 [reactor-http-nio-1] DEBUG io.netty.util.ResourceLeakDetectorFactory - Loaded default ResourceLeakDetector: io.netty.util.ResourceLeakDetector@492aff7f
14:37:04.439 [reactor-http-nio-1] DEBUG io.netty.resolver.dns.DnsQueryContext - [id: 0xa3a7d02f] WRITE: UDP, [23067: /10.100.101.50:53], DefaultDnsQuestion(mdl07cj001.bt.wan. IN A)
14:37:04.443 [reactor-http-nio-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxCapacityPerThread: 4096
14:37:04.443 [reactor-http-nio-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.maxSharedCapacityFactor: 2
14:37:04.443 [reactor-http-nio-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.linkCapacity: 16
14:37:04.443 [reactor-http-nio-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.ratio: 8
14:37:04.443 [reactor-http-nio-1] DEBUG io.netty.util.Recycler - -Dio.netty.recycler.delayedQueue.ratio: 8
14:37:04.456 [reactor-http-nio-1] DEBUG io.netty.resolver.dns.DnsNameResolver - [id: 0xa3a7d02f] RECEIVED: UDP [23067: /10.100.101.50:53], DatagramDnsResponse(from: /10.100.101.50:53, to: /0:0:0:0:0:0:0:0:55442, 23067, QUERY(0), NoError(0), RD AA RA)
DefaultDnsQuestion(mdl07cj001.bt.wan. IN A)
DefaultDnsRawRecord(mdl07cj001.bt.wan. 3600 IN A 4B)
DefaultDnsRawRecord(OPT flags:0 udp:4000 0B)
14:37:04.460 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003] Registering pool release on close event for channel
14:37:04.460 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003] Channel connected, now 1 active connections and 0 inactive connections
14:37:04.461 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003] onStateChange(PooledConnection{channel=[id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003]}, [connected])
14:37:04.467 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003] onStateChange(GET{uri=/, connection=PooledConnection{channel=[id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003]}}, [configured])
14:37:04.468 [reactor-http-nio-2] DEBUG reactor.netty.http.client.HttpClientConnect - [id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003] Handler is being applied: {uri=http://mdl07cj001.bt.wan:7003/MIS/Notification/Email/api/notification/email, method=POST}
14:37:04.470 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003] onStateChange(POST{uri=/MIS/Notification/Email/api/notification/email, connection=PooledConnection{channel=[id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003]}}, [request_prepared])
14:37:04.474 [reactor-http-nio-2] DEBUG org.springframework.http.codec.FormHttpMessageWriter - [7e0b85f9] Writing form fields [to, cc, subject, content, contentType] (content masked)
14:37:04.488 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003] onStateChange(POST{uri=/MIS/Notification/Email/api/notification/email, connection=PooledConnection{channel=[id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003]}}, [request_sent])
14:37:04.517 [reactor-http-nio-2] DEBUG reactor.netty.http.client.HttpClientOperations - [id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003] Received response (auto-read:false) : [Cache-Control=no-cache, no-store, max-age=0, must-revalidate, Date=Mon, 18 Jan 2021 12:37:04 GMT, Pragma=no-cache, Transfer-Encoding=chunked, Expires=0, X-ORACLE-DMS-ECID=57445be9-bcb8-496b-bd25-4986e33f3e23-00000135, X-ORACLE-DMS-RID=0, X-Frame-Options=DENY, Accept=multipart/form-data, X-Content-Type-Options=nosniff, X-XSS-Protection=1; mode=block]
14:37:04.517 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003] onStateChange(POST{uri=/MIS/Notification/Email/api/notification/email, connection=PooledConnection{channel=[id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003]}}, [response_received])
14:37:04.519 [reactor-http-nio-2] DEBUG org.springframework.web.reactive.function.client.ExchangeFunctions - [7e0b85f9] Response 415 UNSUPPORTED_MEDIA_TYPE
14:37:04.530 [reactor-http-nio-2] DEBUG reactor.netty.channel.FluxReceive - [id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003] FluxReceive{pending=0, cancelled=false, inboundDone=false, inboundError=null}: subscribing inbound receiver
14:37:04.531 [reactor-http-nio-2] DEBUG reactor.netty.http.client.HttpClientOperations - [id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003] Received last HTTP packet
14:37:04.540 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003] onStateChange(POST{uri=/MIS/Notification/Email/api/notification/email, connection=PooledConnection{channel=[id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003]}}, [response_completed])
Exception in thread "main" org.springframework.web.reactive.function.client.WebClientResponseException$UnsupportedMediaType: 415 Unsupported Media Type from POST http://mdl07cj001.bt.wan:7003/MIS/Notification/Email/api/notification/email
at org.springframework.web.reactive.function.client.WebClientResponseException.create(WebClientResponseException.java:195)
14:37:04.541 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003] onStateChange(POST{uri=/MIS/Notification/Email/api/notification/email, connection=PooledConnection{channel=[id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003]}}, [disconnecting])
Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Error has been observed at the following site(s):
14:37:04.541 [reactor-http-nio-2] DEBUG reactor.netty.resources.DefaultPooledConnectionProvider - [id: 0xacaa8747, L:/10.0.69.115:49384 - R:mdl07cj001.bt.wan/10.100.139.63:7003] Releasing channel
|_ checkpoint ⇢ 415 from POST http://mdl07cj001.bt.wan:7003/MIS/Notification/Email/api/notification/email [DefaultWebClient]
Stack trace:
at org.springframework.web.reactive.function.client.WebClientResponseException.create(WebClientResponseException.java:195)
at org.springframework.web.reactive.function.client.DefaultClientResponse.lambda$createException(DefaultClientResponse.java:216)
at reactor.core.publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:106)
at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onNext(FluxOnErrorResume.java:79)
at reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1784)
at reactor.core.publisher.FluxDefaultIfEmpty$DefaultIfEmptySubscriber.onComplete(FluxDefaultIfEmpty.java:107)
at reactor.core.publisher.FluxMapFuseable$MapFuseableSubscriber.onComplete(FluxMapFuseable.java:150)
at reactor.core.publisher.FluxContextWrite$ContextWriteSubscriber.onComplete(FluxContextWrite.java:126)
at reactor.core.publisher.FluxMapFuseable$MapFuseableConditionalSubscriber.onComplete(FluxMapFuseable.java:344)
at reactor.core.publisher.FluxFilterFuseable$FilterFuseableConditionalSubscriber.onComplete(FluxFilterFuseable.java:391)
at reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1785)
at reactor.core.publisher.MonoCollect$CollectSubscriber.onComplete(MonoCollect.java:159)
at reactor.core.publisher.FluxMap$MapSubscriber.onComplete(FluxMap.java:142)
at reactor.core.publisher.FluxPeek$PeekSubscriber.onComplete(FluxPeek.java:259)
at reactor.core.publisher.FluxMap$MapSubscriber.onComplete(FluxMap.java:142)
at reactor.netty.channel.FluxReceive.onInboundComplete(FluxReceive.java:383)
at reactor.netty.channel.ChannelOperations.onInboundComplete(ChannelOperations.java:396)
at reactor.netty.channel.ChannelOperations.terminate(ChannelOperations.java:452)
at reactor.netty.http.client.HttpClientOperations.onInboundNext(HttpClientOperations.java:664)
at reactor.netty.channel.ChannelOperationsHandler.channelRead(ChannelOperationsHandler.java:94)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
at io.netty.channel.CombinedChannelDuplexHandler$DelegatingChannelHandlerContext.fireChannelRead(CombinedChannelDuplexHandler.java:436)
at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:324)
at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:296)
at io.netty.channel.CombinedChannelDuplexHandler.channelRead(CombinedChannelDuplexHandler.java:251)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357)
at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379)
at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365)
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:719)
at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655)
at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493)
at io.netty.util.concurrent.SingleThreadEventExecutor.run(SingleThreadEventExecutor.java:989)
at io.netty.util.internal.ThreadExecutorMap.run(ThreadExecutorMap.java:74)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.lang.Thread.run(Thread.java:748)
Suppressed: java.lang.Exception: #block terminated with an error
at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:99)
at reactor.core.publisher.Mono.block(Mono.java:1703)
at oca.demo.ocademo.GetAndPost.newPostMethod(GetAndPost.java:120)
at oca.demo.ocademo.Main.main(Main.java:8)
Process finished with exit code 1
如果您正在使用 spring,正如我在 RequestMapping 注释中看到的那样,您为什么不使用 restTemplate 或 webclient? (取决于您使用的 spring 版本)。会容易很多。
Spring 4 及以下的 Rest 模板,命令式
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> map= new LinkedMultiValueMap<>();
map.add("to", "alice@example.com");
map.add("to", "bob@anotherex.com");
map.add("cc", "alice@example.com");
map.add("cc", "bob@anotherex.com");
map.add("subject", "randomsubject");
map.add("content", "randomContent");
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
RestTemplate restTemplate= new RestTemplateBuilder().rootUri("http://localhost:8080").build();
restTemplate.postForLocation( "url", request);
WebClient for Spring 5+,反应式需要 WebFlux
WebClient client = WebClient.builder()
.baseUrl("http://localhost:8080")
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE)
.build();
client.post()
.uri("/api/notification/email...")
.body(BodyInserters.fromFormData("to", "alice@example.com")
.with("to", "bob@anotherex.com")
.with("cc", "alice@example.com")
.with("cc", "bob@anotherex.com")
.with("subject", "randomsubject")
.with("content", "randomContent"))
.retrieve()
.toBodilessEntity()
.block();
在这两种情况下都在内部使用多值映射,因此您可以使用相同的键添加各种值,例如 to 和 cc
Postman 允许将请求转换为其他语言,只需单击请求下方的代码 link url。