[英]SpringBoot RestController generic POST type
I'm experimenting with building microservices using Spring Boot.我正在尝试使用 Spring Boot 构建微服务。
I have a back-end API that receives ResponseEntity POST requests and processes it (saving to database etc).我有一个后端 API,它接收 ResponseEntity POST 请求并对其进行处理(保存到数据库等)。 Where Data is an Object of a self-created class.
其中 Data 是自创建类的对象。
Now I have a top-level API (that handles authentication,..).现在我有一个顶级 API(处理身份验证,..)。 The end-users will communicate with the back-end services through this top-level API.
最终用户将通过这个顶级 API 与后端服务进行通信。 So this API basically just has to forward all the requests to the right back-end api's.
所以这个 API 基本上只需要将所有请求转发到正确的后端 api。
In this top API I don't want to need to include all my classes (eg the Data class in this case) and I would rather just send it as String json data or something.在这个顶级 API 中,我不想包含我所有的类(例如在这种情况下的 Data 类),我宁愿将它作为 String json 数据或其他东西发送。 So I tried this:
所以我试过这个:
@RequestMapping(method = RequestMethod.POST, value="/data")
ResponseEntity<String> createUnit(@RequestBody String data) {
URI uri = util.getServiceUrl("dataservice");
String url = uri.toString() + "/data";
ResponseEntity<String> result = restTemplate.postForEntity(url, data, String.class);
return new ResponseEntity<String>(result.getBody(), HttpStatus.OK);
}
But this results in an org.springframework.web.client.HttpClientErrorException: 415 Unsupported Media Type
.但这会导致
org.springframework.web.client.HttpClientErrorException: 415 Unsupported Media Type
。
So my question is, is there a way to forward these requests to my back-end without the need to include all my Object classes in my API?所以我的问题是,有没有办法将这些请求转发到我的后端,而无需在我的 API 中包含所有对象类? I figured this should be able since this is the same as when a web-browser sends requests in json format without knowing what kind of Object the data actually is.
我认为这应该可以,因为这与 Web 浏览器以 json 格式发送请求而不知道数据实际上是什么类型的对象时相同。
The back-end handling looks like this:后端处理如下所示:
@RequestMapping(method = RequestMethod.POST, value="/data")
ResponseEntity<Data> saveData(@RequestBody Data data) {
//Some code that processes the data
return new ResponseEntity<Data>(dataProcessed, HttpStatus.OK);
}
When posting the String
to the backend service you have to specify the Content-Type
header so Spring knows which HttpMessageConverter
to use to deserialize the Data
object.将
String
发布到后端服务时,您必须指定Content-Type
标头,以便 Spring 知道使用哪个HttpMessageConverter
来反序列化Data
对象。
With RestTemplate
you can specify the header like this:使用
RestTemplate
您可以像这样指定标题:
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<String>(data, headers);
restTemplate.postForEntity(url, entity, responseType);
Even though the question was already answered, I'll show another way I managed to solve the problem.即使问题已经得到解答,我也会展示我设法解决问题的另一种方法。
I used type Object
instead of String
:我使用类型
Object
而不是String
:
@RequestMapping(method = RequestMethod.POST, value="/data")
ResponseEntity<Object> createUnit(@RequestBody Object data) {
URI uri = util.getServiceUrl("dataservice");
String url = uri.toString() + "/data";
ResponseEntity<Object> result = restTemplate.postForEntity(url, data, Object.class);
return new ResponseEntity<Object>(result.getBody(), HttpStatus.OK);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.