HttpWebRequest:空体

时间:2016-01-06 08:35:05

标签: java c# spring web-services rest

我有一些运行良好的Java REST WS。 代码:

@RequestMapping(value = "/filter", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public String filterByAttribute(@RequestBody final Constraint filterConstraint) {
RestConfigurationService.LOGGER.info("Calling rest service filterByAttribute with params [ attributeName: " + filterConstraint.getAttribute()
        + ", attributeValue: " + filterConstraint.getValue() + "]");
//...Some stuff....
    return "hello";
}

Constraint Pojo:

/*    */ @JsonIgnoreProperties(ignoreUnknown=true)
/*    */ public class Constraint
/*    */ {
/*    */   @JsonProperty("attribute")
/*    */   private String attribute;
/*    */   @JsonProperty("value")
/*    */   private String value;
...

我尝试使用以下代码在我的前面C#webapp中调用WS:

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(completeUrl);
        request.ContentType = "application/json; charset=utf-8";
        request.Method = "POST";

        using (var streamWriter = new StreamWriter(request.GetRequestStream()))
        {
            string json = "{ \"attribute\": \"Applications\", \"value\": \"Pompes et appareils immergés\" }";

            streamWriter.Write(json);
            streamWriter.Flush();
        }

        string token = (string)Session["token"];
        request.Headers.Add("Authorization", "Bearer " + token);


        try
        {
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
...

(我使用OAuth,这部分没关系。) 在C#端,我收到错误400错误请求。 在Java WS Side:

06-01-2016 09:27:46,141 [ramework.web.method.support.InvocableHandlerMethod:168:DEBUG] http-bio-8080-exec-6 - - 错误解析参数[1] [类型= java.lang.String中] HandlerMethod详细信息: 控制器[com.blabla.volta.rest.service.RestConfigurationService] 方法[public org.springframework.http.ResponseEntity com.blabla.volta.rest.service.RestConfigurationService.filterByAttribute(java.lang.String,java.lang.String)]

org.springframework.http.converter.HttpMessageNotReadableException:缺少必需的请求正文: public org.springframework.http.ResponseEntity com.blabla.volta.rest.service.RestConfigurationService.filterByAttribute(java。 lang.String,java.lang.String中)     在org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:151)

我错过了什么?

修改:我尝试过

string json = "pwet";

在java方面我得到了

org.springframework.http.converter.HttpMessageNotReadableException:无法读取文档:意外字符('p'(代码112)):预期有效值(数字,字符串,数组,对象,'true','false'或'null')

所以我明白我的json(第一个)没有很好地格式化,但是java服务实际上看到了msg。

0 个答案:

没有答案