ServiceStack - REST服务不会将json反序列化为请求对象

时间:2012-10-01 09:29:11

标签: rest servicestack

我在ServiceStack中创建了一个REST服务,但是当我用REST URL调用它时,它没有反序列化JSON我将服务发布到我的请求对象中。如果我使用默认URL或在查询字符串中提供参数,它可以正常工作。

我的请求对象:

[Route("/myservice", "POST")]
public class MyRequestObject
{
    public string MyString { get; set; }
    public int MyInt { get; set; }
}

我的服务:

public class MyService : Service
{
    public object Post(MyRequestObject request)
    {
        return new
            {
                MyString = request.MyString,
                MyInt = request.MyInt
            };
    }
}

我发布的服务json:

{"myString": "abc", "myint": 42}

我的REST URL是/ api / myservice,默认URL是/ api / json / asynconeway / MyRequestObject。我已经验证了使用REST和默认URL调用服务。

非常感谢您的投入。

成功标题

请求

POST /api/json/asynconeway/MyRequestObject HTTP/1.1
Host: bd.local
Connection: keep-alive
Content-Length: 32
Origin: chrome-extension://aejoelaoggembcahagimdiliamlcdmfm
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.79 Safari/537.4
Accept: application/json
Accept-Encoding: gzip,deflate,sdch
Accept-Language: da-DK,da;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

响应

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
COMMERCE-SERVER-SOFTWARE: Microsoft Commerce Server, Enterprise Edition
X-Powered-By: ServiceStack/3,914 Win32NT/.NET
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Mon, 01 Oct 2012 10:32:37 GMT
Content-Length: 29

错误标题

请求

POST /api/myservice HTTP/1.1
Host: bd.local
Connection: keep-alive
Content-Length: 32
Origin: chrome-extension://aejoelaoggembcahagimdiliamlcdmfm
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.79 Safari/537.4
Accept: application/json
Accept-Encoding: gzip,deflate,sdch
Accept-Language: da-DK,da;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

响应

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
COMMERCE-SERVER-SOFTWARE: Microsoft Commerce Server, Enterprise Edition
X-Powered-By: ServiceStack/3,914 Win32NT/.NET
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Mon, 01 Oct 2012 10:30:38 GMT
Content-Length: 11

1 个答案:

答案 0 :(得分:1)

问题可能出在发布POST的代码中。数据json是否被字符串化,例如:

JSON.stringify({"myString": "abc", "myint": 42})