为Web API 2编写了一个简单的函数,它返回了国家/地区列表.它返回有效的Json格式,但没有数组/对象名称.我有点难以理解这是如何实现的?
这是我的C#代码:
[Route("Constants/CountryList")]
[HttpGet]
public IHttpActionResult GetCountryList()
{
IEnumerable<ISimpleListEntity> list = new CountryStore().SimpleSortedListByName();
if (list == null || !list.Any())
{
return NotFound();
}
return Ok(list);
}
Run Code Online (Sandbox Code Playgroud)
ISimpleListEntity接口代码在这里.
public interface ISimpleListEntity
{
int Id { get; set; }
string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
此服务返回以下Json输出(没有对象/数组名称):
[
{
"Id":1,
"Name":"[Select]"
},
{
"Id":4,
"Name":"India"
},
{
"Id":3,
"Name":"Singapore"
},
{
"Id":2,
"Name":"United Arab Emirates"
}
]
Run Code Online (Sandbox Code Playgroud)
但是,我正在努力实现以下Json格式(使用名为'CountryList'的对象/数组名称):
{
"CountryList":[
{
"Id":1,
"Name":"[Select]"
},
{ …Run Code Online (Sandbox Code Playgroud) 我试图使用log4j将我的其余web服务的LoggingInInterceptor和LoggingOutInterceptor消息从控制台重定向到文件,如下所示,
cxf.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd">
<jaxrs:server id="base" address="/Restful">
<jaxrs:serviceBeans>
<ref bean="Service" />
</jaxrs:serviceBeans>
<jaxrs:features>
<cxf:logging />
</jaxrs:features>
</jaxrs:server>
<bean id="Service" class="com.xxx.yyy.services.ServiceImpl" />
</beans>
Run Code Online (Sandbox Code Playgroud)
org.apache.cxf.Logger文件
org.apache.cxf.common.logging.Log4jLogger
Run Code Online (Sandbox Code Playgroud)
log4j.properties
# Root logger option
log4j.rootLogger=INFO, file, stdout
## more informations
# http://cxf.apache.org/docs/debugging-and-logging.html
# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:\\ServiceLog.txt
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
Run Code Online (Sandbox Code Playgroud)
附加细节,
CXF 3.1.4和Java 7
在这里,我在控制台中接收这两个消息,但它没有在文件中写入.
如果有人能在这里帮助我,我们将不胜感激.
谢谢,
我想创建一个RESTful服务API,它允许我使用移动应用程序从数据库添加和检索数据.我尝试使用Apigility,但找不到足够的教程来支持我这样做.
不同的免费 RESTful服务构建器之间有什么区别,特别强调支持教程和文档的可用性?
谢谢.任何帮助将不胜感激.
Nette PHP框架是否适合在PHP中创建RESTful API(对于客户端JavaScript应用程序),我找不到合适的文档 IAuthenticator
SimpleAuthetication.我刚刚开始使用selenium webdriver为网站创建测试,填写表单点击按钮等.还有什么可以用于Selenium?这是硒的主要目的吗?测试填写表单,检查元素是否存在,并单击按钮,就像它模拟Web用户一样?
我知道这是UI测试,但是,我需要一个服务测试工具.然而,我遇到了SOAPUI,这不仅仅是与selenium相同类型的工具吗?我可以看到SOAPUI也有负载测试和安全测试,但这不是可以使用selenium完成的事情吗?
这两种工具之间的主要区别是什么?我还可以使用哪些工具来测试REST API?
谢谢.
我正在开发提供REST API的项目.最近,我们决定将其与Swagger集成,为每个端点创建详细的文档.几乎所有的REST都已成功集成,但对于其中一些我们遇到的困难很少.
因此,我们有一个带有"/ users"资源的REST,它默认按照JSON格式的给定标准返回用户列表,例如:
[
{
name: "user1",
age: 50,
group: "group1"
},
{
name: "user2",
age: 30,
group: "group2"
},
{
name: "user3",
age: 20,
group: "group1"
}
]
Run Code Online (Sandbox Code Playgroud)
此REST的要求之一是按" 组 "字段对用户进行分组,并提供以下格式的响应:
[
group1: [
{
name: "user1",
age: 50,
group: "group1"
},
{
name: "user3",
age: 20,
group: "group1"
}]
group2: [
{
name: "user2",
age: 30,
group: "group2"
}
]
]
Run Code Online (Sandbox Code Playgroud)
当查询param groupby等于"group" 时,我们提供这样的响应.因此,问题在于Swagger允许通过单个端点(方法和响应代码)仅提供单一响应格式.例如,我们不能为/ users端点" 200 "响应代码和GET HTTP方法提供2种响应格式.
而现在我对如何改变我们的REST设计以使其与Swagger兼容的方式感到困惑.
注意: 根据REST设计原则,为 …
我使用swagger-springmvc注释开发了一些由swagger记录的Rest Web服务.现在,我想使用swagger-editor生成客户端Rest Web服务代码,但swagger-editor需要Yaml或Json文件.你知道是否有办法生成这个文件?提前谢谢
编辑: 这可以通过使用swagger-mvn-plugin完成,但我没有找到如何做到的例子?
我一直在尝试使用OWIN TestServer类实现集成测试,除了PUT或DELETE方法的实现之外,一切都有效.POST方法代码(有效)如下所示:
using (var server = TestServer.Create<Startup>())
{
var response = await server.CreateRequest(uri)
.AddHeader("Authorization", "Bearer " + _token)
.And(
request =>
request.Content =
new ObjectContent(typeof (T), command, new JsonMediaTypeFormatter()))
.PostAsync();
Assert.AreEqual(response.StatusCode, expectedStatusCode);
return await response.Content.ReadAsStringAsync();
}
Run Code Online (Sandbox Code Playgroud)
要执行PUT请求,我尝试了以下两种方法:
using (var server = TestServer.Create<Startup>())
{
var response = await server.CreateRequest(uri)
.AddHeader("Authorization", "Bearer " + _token)
.And(
request =>
request.Content =
new ObjectContent(typeof (T), command, new JsonMediaTypeFormatter()))
.And(request => request.Method = = HttpMethod.Put)
.PostAsync();
Assert.AreEqual(response.StatusCode, expectedStatusCode);
return await response.Content.ReadAsStringAsync();
}
Run Code Online (Sandbox Code Playgroud)
和
using (var …Run Code Online (Sandbox Code Playgroud) 我有一个带条件操作的资源:
/foos/{id}/authorize
/foos/{id}/cancel
Run Code Online (Sandbox Code Playgroud)
这个想法是授权会将资源的状态从保存(默认)更改为授权(由第三方应用程序).授权可以从远程部分返回错误或者可以被授权.一旦授权,资源就无法再次被授权,因此这不是可以一次又一次调用的动作.
撤消授权资源时会发生取消.一旦取消,资源将永久保留.
考虑到此操作不安全并且不能被视为幂等因为第二次调用会返回"资源已被取消"这样的错误并且同时我不创建这种操作的RESTful世界中正确的动词是什么一个新资源,只是在已知资源中进行状态更改?
我正在使用Akka-HTTP 2.0-M2设计REST服务,并且遇到了我想要提供额外标头的情况,这些标头取决于所查询的Actor的回复.
目前,我有以下......
val route = {
path("oncologist") {
get {
parameters('active.as[Boolean].?, 'skip.as[Int].?, 'limit.as[Int].?).as(GetAllOncologists) {
req =>
complete {
(oncologistActor ? req).mapTo[OncologistList]
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
虽然这是没有问题的回归.我想将OncologistList的一些属性移动到响应头中,而不是将它们返回到正文中.也就是说,我正在返回总记录数和偏移量,我想生成一个上一个和下一个URL头值供客户端使用.我对如何进行感到茫然.
rest ×10
c# ×2
java ×2
json ×2
swagger ×2
web-services ×2
akka ×1
akka-http ×1
api ×1
automation ×1
cxf ×1
http-verbs ×1
ienumerable ×1
javascript ×1
log4j ×1
nette ×1
owin ×1
php ×1
scala ×1
selenium ×1
yaml ×1