我正在使用POST
将obejct作为参数发送到服务中的方法。该方法每次都在调用,但是spring返回一个已启动的对象(带零和空值),而不是返回的是serivce。
邮递员的工作很棒:
我发送:
{
"userId": 10,
"resourceType": 11,
"privilegeValues": [
1,
2,
3
]
}
我明白了:
{
"ErrorCode": 10,
"ErrorDescription": null,
"Privilages": [
1,
2,
3
]
}
C#:
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:
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
@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
@ToString
public class UserPrivilegesResponse {
@Getter
@Setter
private int ErrorCode;
@Getter
@Setter
private int ErrorDescription;
@Getter
@Setter
private int[] Privilages;
}
答案 0 :(得分:0)
好的在WCF C#项目和Spring JAVA项目中,我为Response对象的相同属性编写了不同的类型。 Spring无法解析它,因此它给了我空的初始化(零)对象。
我建议大家对响应对象的每个属性使用@JsonProperty("XXX")
,以便Spring告诉你哪一个他无法解析。多数民众赞成我如何找到我的错误。即使属性名称与XXX
相同,也请使用它。