Java 获取初始化对象而不是服务发送的对象
我使用Java 获取初始化对象而不是服务发送的对象,java,json,spring,post,Java,Json,Spring,Post,我使用POST将obejct作为参数发送到服务中的方法。每次调用该方法时,spring都会返回一个初始化对象(带零和空),而不是serivce返回的对象 邮递员的工作很好: 我发送: { "userId": 10, "resourceType": 11, "privilegeValues": [ 1, 2, 3 ] } 我得到: { "ErrorCode": 10, "ErrorDescription": null, "Privilages"
POST
将obejct作为参数发送到服务中的方法。每次调用该方法时,spring都会返回一个初始化对象(带零和空),而不是serivce返回的对象
邮递员的工作很好:
我发送:
{
"userId": 10,
"resourceType": 11,
"privilegeValues": [
1,
2,
3
]
}
我得到:
{
"ErrorCode": 10,
"ErrorDescription": null,
"Privilages": [
1,
2,
3
]
}
C#:
IPRIVILAGEREST:
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();
}
}
隐私提供者:
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;
}
}
}
UserPrivilegesResponse.java
@ToString
public class UserPrivilegesResponse {
@Getter
@Setter
private int ErrorCode;
@Getter
@Setter
private int ErrorDescription;
@Getter
@Setter
private int[] Privilages;
}
好的,在WCF C#项目和Spring JAVA项目中,我为响应对象的相同属性编写了不同的类型。
Spring无法解析它,所以它给了我空的初始化(零)对象
我建议每个人对响应对象的每个属性都使用@JsonProperty(“XXX”)
,这样Spring就会告诉您他无法解析哪个属性。这就是我发现错误的原因。即使属性名称与XXX
相同,也要使用它
@ToString
public class UserPrivilegesResponse {
@Getter
@Setter
private int ErrorCode;
@Getter
@Setter
private int ErrorDescription;
@Getter
@Setter
private int[] Privilages;
}