使用POST到自定义Route时,ServiceStack JSON值为null

时间:2014-04-14 00:01:39

标签: json rest servicestack

我对ServiceStack和REST服务一般都很陌生,所以如果这是初级的,或者我完全走错了路,请原谅我......

我正在使用ServiceStack版本4.0.15,ORMLite和SQL Server。

我正在尝试在自定义Route上设置POST端点,该端口接受邮件正文中的JSON对象,并从该对象在数据库中创建记录。我现在连接它的方式,如果我POST到[localhost] / json / reply / CreatePatientRequest的默认ServiceStack路由,一切正常。但是,如果我使用自定义的[localhost] / patient路由,请求对象在到达我的服务时会包含所有空值。

首先,我有一名患者DTO:

public class Patient
{
    [AutoIncrement]
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int SSN { get; set; }
    ...
    public DateTime CreatedAt { get; set; }
}

这是我的要求:

[Route("/patient", "POST")]
public class CreatePatientRequest : IReturn<PatientResponse>
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int SSN { get; set; }
    ...
}

PatientResponse只是一个返回Id,First和Last名称以及Message的类。所有这些类都具有其他属性。这是我的服务:

public class PatientService : Service
    {
        private static string connectionString = ConfigurationManager.ConnectionStrings["ApiDbConnectionString"].ConnectionString;
        private static OrmLiteConnectionFactory dbFactory = new OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider);
        private static IDbConnection dbConn = dbFactory.OpenDbConnection();

        public PatientResponse Post(CreatePatientRequest request)
        {
            var response = new PatientResponse { Id = 0 };
            if (request.SSN == 0)
            {
                response.Message = "No SSN present in request. Please specify SSN and resend.";
                return response;
            }
            try
            {
                dbConn.Insert(new Patient
                {
                    FirstName = request.FirstName.ToString(),
                    LastName = request.LastName.ToString(),
                    SSN = request.SSN,
                    ...
                    CreatedAt = DateTime.Now
                });
                response.Message = "Record created successfully.";
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
            }

            return response;
        }
    }
}

同样,如果我POST到CreatePatientRequest的默认ServiceStack路由,一切都很好,数据库得到更新,我得到PatientResponse JSON返回。但是,如果我发布到我的自定义&#34; / patient&#34; Route,CreatePatientRequest请求对象具有所有空值。

有人请帮忙!谢谢!

更新

这里是请求标题+正文:

POST http://localhost:26809/patient HTTP/1.1
User-Agent: Fiddler
Host: localhost:26809
Content-Length: 545

{
  "MDPatientId": "MD0003",
  "RXPatientId": "RX0003",
  "FirstName": "Sarah",
  "LastName": "Palin",
  "SSN": 135241234,
  "DateOfBirth": "\/Date(500274000000-0000)\/",
  "GenderId": 2,
  "RaceId": 1,
  "MaritalStatusId": 2,
  "PrimaryLanguageId": 1,
  "HomePhone": "",
  "CellPhone": "123-456-7890",
  "Email": "spalin@email.com",
  "Height": 58.000000,
  "Weight": 124.000000,
  "HIVStatus": "Negative",
  "Genotype": "1",
  "ViralLoad": "20,000,000",
  "DiagnosisCode": 183.740000
}

响应标题:

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html
Server: Microsoft-IIS/8.0
X-Powered-By: ServiceStack/4.015 Win32NT/.NET
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcU291cmNlXFdvcmtzcGFjZXNcQ3VzdG9tQXV0aFxDdXN0b21BdXRoXHBhdGllbnQ=?=
X-Powered-By: ASP.NET
Date: Tue, 15 Apr 2014 16:11:27 GMT
Content-Length: 9252

响应正文是一堆HTML,当在Fiddler的WebView选项卡中查看时,显示了ServiceStack在...&#34;上生成的&#34; CreatePatientRequest快照,但是没有JSON响应。

1 个答案:

答案 0 :(得分:1)

使用像Fiddler这样的通用HTTP客户端使用请求正文(例如POST / PUT)创建原始JSON HTTP请求时,指定 对您来说很重要&# 39;使用Content-Type: application/json HTTP标头重新发送。

此外,根据您的服务的默认ContentType类型,您还应该使用Accept: application/json指定所需的响应类型,例如:

POST http://localhost:26809/patient HTTP/1.1
User-Agent: Fiddler
Content-Type: application/json
Accept: application/json
Host: localhost:26809
Content-Length: xxx

{"json":"string"}

在ServiceStack中设置默认ContentType

可以使用以下命令指定ServiceStack中的默认ContentType:

SetConfig(new HostConfig {
   DefaultContentType = MimeTypes.Json
});