RESTful Api - Spring MediaType 与 Restlet MediaType
RESTful Api - Spring MediaType vs Restlet MediaType
我有一个 REST 客户端:
import org.restlet.representation.ObjectRepresentation;
import org.restlet.data.MediaType;
ObjectRepresentation<ApprovalResponse> objectRepresentation = (ObjectRepresentation<ApprovalResponse>) cr.post(approvalRequest, MediaType.APPLICATION_JAVA_OBJECT);
还有一个Spring引导服务RESTfulapi:
import org.springframework.http.MediaType;
@PostMapping(value = "/rest/approvals-submit")
public @ResponseBody ApprovalResponse submit(@RequestHeader(name="Authorization") String token, @RequestBody ApprovalRequest approvalRequest) {
System.out.println("jwt token: "+token);
System.out.println(approvalRequest.getMessageToEvaluator());
ApprovalResponse approvalResponse = new ApprovalResponse();
approvalResponse.setApprovalId("Test approvalResponse from micro service");
return approvalResponse;
}
客户端调用API成功。即 System.out.println(approvalRequest.getMessageToEvaluator());
打印成功。
问题
我的问题是响应对象没有返回到 REST 客户端。
错误信息
休息客户端
org.restlet.resource.ResourceException: Not Acceptable (406) - The
resource identified by the request is only capable of generating
response entities which have content characteristics not acceptable
according to the accept headers sent in the request
Server/Api
Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException:
Could not find acceptable representation]
问题
所以我认为这些错误是因为没有正确定义 MediaTypes。你知道他们应该被定义成什么吗?
直接聊天后更新
我们在 rest 端点上添加了以下 postMapping 注释:
@PostMapping(value = "/rest/approvals-submit")
public @ResponseBody ApprovalResponse submit(@RequestHeader(name="Authorization") String token, @RequestBody ApprovalRequest approvalRequest, produces={MediaType.APPLICATION_JSON_VALUE,
MediaType.APPLICATION_XML_VALUE},consumes = MediaType.APPLICATION_JSON_VALUE) {
System.out.println("jwt token: "+token);
System.out.println(approvalRequest.getMessageToEvaluator());
ApprovalResponse approvalResponse = new ApprovalResponse();
approvalResponse.setApprovalId("Test approvalResponse from micro service");
return approvalResponse;
}
这个加在@PostMapping注解上产生={MediaType.APPLICATION_JSON_VALUE,
MediaType.APPLICATION_XML_VALUE},消费 = MediaType.APPLICATION_JSON_VALUE。
在客户端,我们将响应类型更改为表示,我们能够获得 json 响应:
ClientResource cr = new ClientResource(endpointUrl);
ChallengeResponse challengeResponse = new ChallengeResponse(ChallengeScheme.HTTP_OAUTH_BEARER);
challengeResponse.setRawValue(token);
cr.setChallengeResponse(challengeResponse);
Request req = cr.getRequest();
Representation representation = cr.post(approvalRequest);
System.out.println(representation.getText());
最后,using Jackson Object Mapper 响应可以映射到批准响应 object:
// now convert the response to java
ObjectMapper objectMapper = new ObjectMapper();
ApprovalResponse approvalResponse = objectMapper.readValue(json, ApprovalResponse.class);
System.out.println(approvalResponse);
System.out.println(approvalResponse.getApprovalId());
这是一个内容协商问题。内容协商是通过 Content-Type header 完成的,为了更好地理解它,您可以阅读 this blog entry
我有一个 REST 客户端:
import org.restlet.representation.ObjectRepresentation;
import org.restlet.data.MediaType;
ObjectRepresentation<ApprovalResponse> objectRepresentation = (ObjectRepresentation<ApprovalResponse>) cr.post(approvalRequest, MediaType.APPLICATION_JAVA_OBJECT);
还有一个Spring引导服务RESTfulapi:
import org.springframework.http.MediaType;
@PostMapping(value = "/rest/approvals-submit")
public @ResponseBody ApprovalResponse submit(@RequestHeader(name="Authorization") String token, @RequestBody ApprovalRequest approvalRequest) {
System.out.println("jwt token: "+token);
System.out.println(approvalRequest.getMessageToEvaluator());
ApprovalResponse approvalResponse = new ApprovalResponse();
approvalResponse.setApprovalId("Test approvalResponse from micro service");
return approvalResponse;
}
客户端调用API成功。即 System.out.println(approvalRequest.getMessageToEvaluator());
打印成功。
问题
我的问题是响应对象没有返回到 REST 客户端。
错误信息
休息客户端
org.restlet.resource.ResourceException: Not Acceptable (406) - The resource identified by the request is only capable of generating response entities which have content characteristics not acceptable according to the accept headers sent in the request
Server/Api
Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]
问题
所以我认为这些错误是因为没有正确定义 MediaTypes。你知道他们应该被定义成什么吗?
直接聊天后更新
我们在 rest 端点上添加了以下 postMapping 注释:
@PostMapping(value = "/rest/approvals-submit")
public @ResponseBody ApprovalResponse submit(@RequestHeader(name="Authorization") String token, @RequestBody ApprovalRequest approvalRequest, produces={MediaType.APPLICATION_JSON_VALUE,
MediaType.APPLICATION_XML_VALUE},consumes = MediaType.APPLICATION_JSON_VALUE) {
System.out.println("jwt token: "+token);
System.out.println(approvalRequest.getMessageToEvaluator());
ApprovalResponse approvalResponse = new ApprovalResponse();
approvalResponse.setApprovalId("Test approvalResponse from micro service");
return approvalResponse;
}
这个加在@PostMapping注解上产生={MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE},消费 = MediaType.APPLICATION_JSON_VALUE。
在客户端,我们将响应类型更改为表示,我们能够获得 json 响应:
ClientResource cr = new ClientResource(endpointUrl);
ChallengeResponse challengeResponse = new ChallengeResponse(ChallengeScheme.HTTP_OAUTH_BEARER);
challengeResponse.setRawValue(token);
cr.setChallengeResponse(challengeResponse);
Request req = cr.getRequest();
Representation representation = cr.post(approvalRequest);
System.out.println(representation.getText());
最后,using Jackson Object Mapper 响应可以映射到批准响应 object:
// now convert the response to java
ObjectMapper objectMapper = new ObjectMapper();
ApprovalResponse approvalResponse = objectMapper.readValue(json, ApprovalResponse.class);
System.out.println(approvalResponse);
System.out.println(approvalResponse.getApprovalId());
这是一个内容协商问题。内容协商是通过 Content-Type header 完成的,为了更好地理解它,您可以阅读 this blog entry