ServiceStack GET操作获取null对象

时间:2013-09-27 19:13:08

标签: json servicestack

我正在查询我最近不得不使用的ServiceStack服务以使POST操作正常工作,现在当我调用我的GET操作时,JSON对象不再被传入,并且在我的生命中我无法弄清楚我做了什么来打破它...

这是我的请求标题:

Request Url: http://internalserver:8181/citations
Request Method: GET
Status Code: 200
Params: {}

这是我的全球配置:

    public override void Configure(Container container)
    {
        container.RegisterAutoWired<CitationRequest>();
        container.RegisterAutoWired<Citation>();

        //Not sure I need these usings...?
        using (var addCitation = container.Resolve<CitationService>())
        {
            addCitation.Post(container.Resolve<CitationRequest>());
            addCitation.Get(container.Resolve<CitationRequest>());
            addCitation.Delete(container.Resolve<CitationRequest>());
        }

        Plugins.Add(new CorsFeature());
        RequestFilters.Add((httpReq, httpRes, requestDto) =>
        {
            if (httpReq.HttpMethod == "OPTIONS")
                httpRes.EndRequestWithNoContent(); //   extension method                    
        });

        SetConfig(new EndpointHostConfig
        {
            DefaultContentType = ContentType.Json,
            ReturnsInnerException = true,
            DebugMode = true,
            AllowJsonpRequests = true,
            ServiceName = "SSD Citations Web Service",
            WsdlServiceNamespace = "http://www.servicestack.net/types",
            WriteErrorsToResponse = true
        });
    }

这是我的服务:

[Route("/citations/{ReportNumber}/{ReportNumber_Prefix}/{AgencyId}", "GET, DELETE, OPTIONS")]
[Route("/citations", "GET, POST, DELETE, OPTIONS")]
public class CitationRequest : IReturn<CitationResponse>
{
    public string ReportNumber { get; set; }
    public int ReportNumber_Prefix { get; set; }
    public string AgencyId { get; set; }
    public DateTime ViolationDateTime { get; set; }
    public CitationStatus Status { get; set; }
}
public class CitationResponse
{
    public bool Accepted { get; set; }
    public string ActivityId { get; set; }
    public int ParticipantId { get; set; }
    public string Message { get; set; }
    public Exception RmsException { get; set; }
}
public class CitationService : Service
{
    public Repository Repository { get { return new Repository(); } }
    public CitationResponse Get(Citation citation)
    {
        var isDuplicate = Repository.IsDuplicateReportNumber(citation.AgencyId, citation.ReportNumber, citation.ReportNumber_Prefix);
        var citationResponse = new CitationResponse
           {
                Accepted = isDuplicate,
                Message = isDuplicate ? "Report Number already exists in db." : "Report Number has not yet been used."
            };
        return citationResponse;
    }

    public CitationResponse Post(CitationRequest request)
    {
        var response = new CitationResponse { Accepted = false };
        if (string.IsNullOrEmpty(request.ReportNumber))
        {
            response.Accepted = false;
            response.Message = "No data sent to service.  Please enter data in first.";
            return response;
        }
        try
        {
            response.ActivityId = Repository.CreateCitation(request.ReportNumber,     request.ReportNumber_Prefix, request.ViolationDateTime, request.AgencyId, request.Status);
            response.Accepted = true;
        }
        catch (Exception ex)
        {
            response.Accepted = false;
            response.Message = ex.Message;
        }
        return response;
    }

    public CitationResponse Delete(CitationRequest citation)
    {
        var citationResponse = new CitationResponse();
        try
        {
            if (Repository.DeleteCitation(citation.ReportNumber, citation.AgencyId, citation.ReportNumber_Prefix))
            {
                citationResponse.Accepted = true;
                citationResponse.Message = "Citation removed from db successfully.";
            }
            else
            {
                citationResponse.Accepted = false;
                citationResponse.Message = "Citation NOT deleted.";
            }
        }
        catch (Exception ex)
        {
            citationResponse.Accepted = false;
            citationResponse.Message = ex.Message;
            citationResponse.RmsException = new Exception(ex.Message);
            throw;
        }

        return citationResponse;            
    }
}

我的POST方法完全填充了我传递给它的JSON对象,所有值都在那里并且可用。我的GET操作中的相同EXACT对象具有所有属性的空值。这是一个示例有效载荷:

{"ReportNumber":"TEST275455",
 "ReportNumber_Prefix":"2013",
 "AgencyId":"BBC",
 "Status":"COMP",
 "ViolationDateTime":"9-21-2013 12:00:00"}

我很难过,拉着我的头发试图找出为什么Get与Post不同?非常感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

您已将路由信息添加到CitationRequest请求DTO:

[Route("/citations/{ReportNumber}/{ReportNumber_Prefix}/{AgencyId}", "GET, DELETE, OPTIONS")]
[Route("/citations", "GET, POST, DELETE, OPTIONS")]
public class CitationRequest : IReturn<CitationResponse>

建议Get使用它,但您的实现需要Citation代替吗?

public CitationResponse Get(Citation citation)

Post的声明使用了正确的声明:

public CitationResponse Post(CitationRequest request)