我有一个.net开发背景,但对Java世界来说相对较新.我们已经开始使用JAX-RS开发RESTful服务(公共Web API),主要由移动平台(Android,iPhone,Windows Phone等)使用.
为什么要使用这个Web API
[HttpGet("hello")]
public HttpResponseMessage Hello()
{
var res = new HttpResponseMessage(HttpStatusCode.OK);
res.Content = new StringContent("hello", Encoding.UTF8, "text/plain");
return res;
}
Run Code Online (Sandbox Code Playgroud)
返回
{
"Version":{
"Major":1,
"Minor":1,
"Build":-1,
"Revision":-1,
"MajorRevision":-1,
"MinorRevision":-1
},
"Content":{
"Headers":[{
"Key":"Content-Type",
"Value":["text/plain; charset=utf-8"]
}]
},
"StatusCode":200,
"ReasonPhrase":"OK",
"Headers":[],
"RequestMessage":null,
"IsSuccessStatusCode":true
}
Run Code Online (Sandbox Code Playgroud)
代替
hello
Run Code Online (Sandbox Code Playgroud)
?
如何让Web API返回如下所示的HTTP响应?
200 OK
Content-Type: text/plain
hello
Run Code Online (Sandbox Code Playgroud)
我最终想要做的是返回JSON和其他具有各种状态代码的格式,因此以下代码不能帮助我作为答案.
[HttpGet("hello")]
public string Hello()
{
return "hello";
}
Run Code Online (Sandbox Code Playgroud)
(我是ASP.NET和其他Microsoft技术的新手.)
我正在使用这段代码从文件中读取,但出现错误“无法创建抽象类或接口“System.IO.TextReader”的实例”
using (FileStream fileStream = File.Open(fileName, FileMode.Open, FileAccess.Read))
using(TextReader reader = new TextReader(fileStream))//error
{
...
}
Run Code Online (Sandbox Code Playgroud)