Spring Boot控制器请求正文为原型的

时间:2018-12-18 09:54:56

标签: java spring-boot

我在客户端使用HttpPost发送已编码的protobuf对象,该对象已在byte[]中序列化。我使用ByteArrayEntity(byte[])来设置HttpPost的正文。

VerificationCodeRequest.Builder testRequestBuilder = new VerificationCodeRequest.Builder();
testRequestBuilder.method = "XX";
testRequestBuilder.country_code = "XXX";
testRequestBuilder.phone_number = "XXXXXXXX";
VerificationCodeRequest testRequest = testRequestBuilder.build();
byte[] data = VerificationCodeRequest.ADAPTER.encode(testRequest);

CloseableHttpClient client = HttpClientBuilder.create().build();
HttpPost request = new HttpPost(urlS);
request.setHeader("User-Agent", "Java client");
request.setHeader("Content-Type", "application/x-protobuf");

request.setEntity(new ByteArrayEntity(data));

HttpResponse response = client.execute(request);

但是在Spring引导应用程序上,请求映射没有处理该请求。

我尝试了多种方式

@RequestMapping(path = "request-verification", method = RequestMethod.POST, consumes =  "application/x-protobuf")
public ResponseEntity requestVerificationCode(VerificationCodeRequest body)


@RequestMapping(path = "request-verification", method = RequestMethod.POST, consumes =  "application/x-protobuf")
public ResponseEntity requestVerificationCode(byte[] body)

唯一的工作就是接受InputStream,但是我相信我应该能够直接将主体行进到至少byte []甚至Protobuf java对象。

@RequestMapping(path = "request-verification", method = RequestMethod.POST, consumes =  "application/x-protobuf")
public ResponseEntity requestVerificationCode(InputStream body)

0 个答案:

没有答案