[英]Getting initilized object instead of what the service sent
I'm using POST
to send an obejct as a parameter to a method in a service. 我正在使用POST
将对象作为参数发送到服务中的方法。 The method is invoking each time but spring is returning me an initilized object (with zeros and nulls) instead of what the serivce returned. 该方法每次都调用,但是spring会返回一个初始化对象(带有零和空值),而不是服务返回的对象。
With postman its working great: 与邮递员一起工作非常好:
I send: 我发送:
{
"userId": 10,
"resourceType": 11,
"privilegeValues": [
1,
2,
3
]
}
I get: 我得到:
{
"ErrorCode": 10,
"ErrorDescription": null,
"Privilages": [
1,
2,
3
]
}
C#: C#:
IPrivilagesRest: IPrivilagesRest:
namespace RestAPI
{
[ServiceContract(Namespace = "http://microsoft.wcf.documentation")]
public interface IPrivilagesRest
{
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/GetUserPrivilages", RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
UserPrivilegesResponse GetUserPrivilages(UserPrivilegesRequest userPrivilegesRequest);
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "/IsAlive", RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
bool isAlive();
}
}
PrivilagesProvider: PrivilagesProvider:
namespace RestAPI
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class PrivilagesProvider : IPrivilagesRest
{
/// <summary>
/// get privilages related to a specific user.
/// </summary>
/// <param name="userPrivilegesRequest"></param>
/// <returns></returns>
public UserPrivilegesResponse GetUserPrivilages(UserPrivilegesRequest userPrivilegesRequest)
{
Console.Clear();
Console.WriteLine(userPrivilegesRequest==null?"Null":"Not null!!!!!!!");
return new UserPrivilegesResponse() { Privilages = new int[] { 1, 2, 3 },ErrorCode=10 };
}
public bool isAlive()
{
return true;
}
}
}
Application.java Application.java
@SpringBootApplication
public class Application {
private static final Logger log = LoggerFactory.getLogger(Application.class);
public static void main(String args[]) {
SpringApplication.run(Application.class);
}
@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
@Bean
public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
return args -> {
UserPrivilegesRequest request=new UserPrivilegesRequest();
UserPrivilegesResponse response = restTemplate.postForObject("http://localhost:12345/PrivilagesServiceNamespace/PrivilagesService/GetUserPrivilages",request, UserPrivilegesResponse.class);
log.info("respose: "+ response);
};
}
}
UserPrivilegesResponse.java UserPrivilegesResponse.java
@ToString
public class UserPrivilegesResponse {
@Getter
@Setter
private int ErrorCode;
@Getter
@Setter
private int ErrorDescription;
@Getter
@Setter
private int[] Privilages;
}
Okay In the WCF C# project and the Spring JAVA project I wrote different type for the same property of the Response object. 好的,在WCF C#项目和Spring JAVA项目中,我为Response对象的相同属性编写了不同的类型。 Spring could not parse it so it gave me empty initilized (zero) object. Spring无法解析它,因此它给了我一个空的初始化(零)对象。
I recommend everyone to use @JsonProperty("XXX")
for each property of the response object so Spring will tell you which one he couldnt parse. 我建议每个人都将@JsonProperty("XXX")
用于响应对象的每个属性,这样Spring会告诉您他无法解析哪个属性。 Thats how I found my error. 那就是我发现错误的方式。 Use it even if the properties names are the same as XXX
. 即使属性名称与XXX
相同,也要使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.