小编iwe*_*ein的帖子

为什么System.Uri不识别本地文件路径的查询参数?

我需要在文件路径中添加一些额外的查询信息作为查询参数,以便稍后在文件处理期间解析路径.我虽然System.Uri类可以帮助我,但看起来它没有给我我对本地文件路径的期望.

var fileUri = new Uri("file:///c://a.txt?select=10")
// fileUri.AbsoluteUri = "file:///c://a.txt%3Fselect=10"
// fileUri.Query = ""

var httpUri = new Uri("http://someAddress/a.txt?select=10")
// httpUri.AbsoluteUri = "http://someaddress/a.txt?select=10"
// httpUri.Query = "?select=10"
Run Code Online (Sandbox Code Playgroud)

在"ftp://someAddress/a.txt?select = 10"的情况下 - 查询字符串也为空

我知道System.Uri可能会解析" a.txt?select = 10 "来更正文件名" a.txt%3Fselect = 10 ",但是为什么 - 如何逃避这个?

提前致谢

.net c# uri query-parameters

8
推荐指数
2
解决办法
3228
查看次数

为什么jquery将json作为参数名而不是作为请求体发布?

对于具有RESTful后端的webapp,我使用jquery的$ post将一些json发布到服务器.令我惊讶的是,json被填充在请求的表单数据的参数键中,而不是在请求体中.我可以想到其他一些方法,但问题是为什么它不能像我期望的那样工作.

在服务器上我使用scalatra并打印一些请求信息:

println("Request received:")
println(fromInputStream(request.getInputStream).getLines().mkString)
println("--------------")
println(request.getParameterMap.toString)
println("==============")
Run Code Online (Sandbox Code Playgroud)

现在一个简单的卷曲,我认为是正确的:

curl -X POST http://localhost:8080/x -H "Content-Type: application/json" -d '{"a":"b"}'
Run Code Online (Sandbox Code Playgroud)

生产:

Request received:
{"a":"b"}
-------------
{}
==============
Run Code Online (Sandbox Code Playgroud)

并用html + js来说明问题:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<body>
  <button type="button" onclick="doPost()">
    POST
  </button>
  <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
  <script type="text/javascript">
function doPost() {
    $.post("http://localhost:8080/x",     
           JSON.stringify({"a":"b"} ), 
           function(data) {});
}
  </script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

生产:

Request received:

--------------
{{"a":"b"}=[Ljava.lang.String;@7a2897ac}
============== 
Run Code Online (Sandbox Code Playgroud)

因此,如果我使用带有字符串化的json字符串和回调的$ post,我会将所有内容填充到单个参数键中.如果这是正常的,我想知道为什么,以及我应该如何在服务器上彻底解开这个问题.如果不正常,我想知道我应该怎么做才能使用$ post在响应体中获取它.

更新:现在有一个jquery功能请求,以支持$ .post上的contentType

rest jquery json scalatra

7
推荐指数
1
解决办法
5155
查看次数

Glassfish:使用非根上下文部署的Web应用程序解释相对于domain1/docroot的请求

webapp使用Spring MVC.

<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
    <property name="urlMap">
        <map>
            <entry key="/*" value-ref="defaultHandler"/>
        </map>
    </property>
    <property name="order" value="2"/>
</bean>
<bean name="defaultHandler" class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>

<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/"/>
    <property name="suffix" value=""/>        
</bean>
Run Code Online (Sandbox Code Playgroud)

因此请求http://localhost:8080/application-context-folder/index.jsp 应该解析为application-context-folder/index.jsp并且它们将解析为domain1/docroot/application-context-folder.

是设计还是我需要在应用程序或配置中更改某些内容?

@Edit:有一个拼写错误,请求的URL是 http://localhost:8080/application-context-folder/index.jsp, not http://localhost:8080/index.jsp

glassfish spring-mvc

6
推荐指数
1
解决办法
1852
查看次数

使用Salat和MongoDB时处理compsite键的最佳方法是什么?

我正在使用Salat与MongoDB,我正在尝试转换为自然键以避免数据库中的重复.我正在使用的案例类看起来有点像:

case class Foo(someRelatedId: String, email: String ...)
Run Code Online (Sandbox Code Playgroud)

我想添加一个由someRelatedId + email组成的自然键,并让MongoDB使用它而不是默认的ObjectId.从文档中我感觉有可能,但我仍然在寻找一个有效的解决方案.这在很大程度上是由于我对Scala本身缺乏熟练程度,我敢肯定.

更新:我现在有一个有效的解决方案,但我仍然想知道这是否是最好的方法

case class Foo(someRelatedId: String, email: String, naturalKey: String)

object Foo {
  def apply((someRelatedId: String, email: String) {
    apply(someRelatedId, email, someRelatedId+email)
  }
}
Run Code Online (Sandbox Code Playgroud)

然后在package.scala中我映射到自定义salat上下文:

implicit val ctx = new Context() {
  val name = Some("Custom Context")
}
ctx.registerGlobalKeyOverride(remapThis = "naturalKey", toThisInstead = "_id")
Run Code Online (Sandbox Code Playgroud)

这样我避免在我的域类中有一个强制的(无意义的)_id字段,但我必须在伴随对象上重载apply(),这看起来有点笨拙.

scala mongodb salat

6
推荐指数
1
解决办法
542
查看次数

使用specs2进行seq空测试

如何Seq[String]在Scala中使用specs2 检查a 是否为空?我正在使用seq must be emptyseq.length must be greaterThan(0)但最终总是出现类型不匹配错误.

ret is Seq[String]

ret.length must be greaterThan(0)

[error] ApiTest.scala:99: type mismatch;
[error]  found   : Int
[error]  required: org.specs2.matcher.Matcher[String]
[error]         ret.length must be greaterThan(0)
Run Code Online (Sandbox Code Playgroud)

unit-testing specs scala specs2

6
推荐指数
2
解决办法
3059
查看次数

HTML5:如何使用AngularJS将焦点设置在列表中的文本输入上

我使用AngularJS和ng-repeat指令将一个对象数组显示为一个列表.

<li ng-repeat="cue in cues" class="form-inline">
    <input type="text" ng-model="cues[$index].text" class="input-xlarge"/>
    {{cue.isNewest}}
</li>
Run Code Online (Sandbox Code Playgroud)

属性"isNewest"仅在数组的一个元素上为true.我想将键盘焦点设置在该项目的文本输入上.我怎么能用AngularJS做到这一点?

html5 focus input angularjs

6
推荐指数
1
解决办法
2万
查看次数

使用spray-json处理默认值的好方法是什么

在某些情况下,默认值比case类中的选项更有意义:

case class Car(numberOfWheels:Int = 4, color:String)

case class Car(numbeOfWheels:Option[Int], color:String) //silly
Run Code Online (Sandbox Code Playgroud)

在第一种情况下,我希望能够轻松地将以下json转换为实例:

{"color":"red"}
Run Code Online (Sandbox Code Playgroud)

但是有了标准jsonFormat2(Car),喷雾json抱怨缺少价值numberOfWheels.

我如何最干净地解决这个问题?

scala spray spray-json

6
推荐指数
1
解决办法
1197
查看次数

在IntelliJ IDEA中格式化多行语句时,我可以对齐运算符吗?

我正在查看IntelliJ IDEA 12中的选项,我不能放过这个.所以请帮我停止浪费时间.

这是我的Build.scala中的一些代码:

val logging =
  Seq(
    "ch.qos.logback" % "logback-classic" % "1.0.13",
    "org.slf4j" % "slf4j-api" % "1.7.5",
    "com.weiglewilczek.slf4s" % "slf4s_2.9.1" % "1.0.7"
  )
Run Code Online (Sandbox Code Playgroud)

我希望它看起来像这样:

val logging =
  Seq(
    "ch.qos.logback"          % "logback-classic" % "1.0.13",
    "org.slf4j"               % "slf4j-api"       % "1.7.5",
    "com.weiglewilczek.slf4s" % "slf4s_2.9.1"     % "1.0.7"
  )
Run Code Online (Sandbox Code Playgroud)

我更喜欢使用代码格式化程序自动创建.在我(不可靠)的内存中,我之前使用过IntelliJ IDEA,但我无法找到要打开的选项.

你知道这个选项隐藏在哪里吗?

scala code-formatting intellij-idea

6
推荐指数
1
解决办法
1081
查看次数

如何在SprayTest中使用json主体模拟POST请求?

如果我有一个端点解组json像这样:

(path("signup")& post) {
    entity(as[Credentials]) { credentials =>
    …
Run Code Online (Sandbox Code Playgroud)

如何使用Spray测试规范测试:

"The Authentication service" should {

"create a new account if none exists" in {
   Post("/api/authentication/signup", """{"email":"foo", "password":"foo:" }""") ~> authenticationRoute ~> check {
    handled === true
  }
}
}
Run Code Online (Sandbox Code Playgroud)

由于几个原因,这显然不起作用.什么是正确的方法?

spray spray-test

6
推荐指数
1
解决办法
5100
查看次数

如何使用Mockito验证Specs2中特定字符串匹配器的调用

我按照这些方针进行测试:

httpClient.post(anyString, anyString) returns (first, second)

//do my thing

there were two(httpClient).post(anyString, anyString)
Run Code Online (Sandbox Code Playgroud)

这工作正常,但我想验证第一个调用通过与第二个调用不同的主体.身体相当大,我不想在严格的例子上进行精确匹配.我试过这个:

there was one(httpClientMock).postMessage(anyString, argThat(contain("FOO"))
there was one(httpClientMock).postMessage(anyString, argThat(contain("FOO"))
Run Code Online (Sandbox Code Playgroud)

这让Mockito抱怨​​道:

InvalidUseOfMatchersException: 
 [error] Invalid use of argument matchers!
 [error] 2 matchers expected, 3 recorded:
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

  there was one(httpClientMock).postMessage(argThat(contain("foo")), argThat(contain("FOO")))
  there was one(httpClientMock).postMessage(argThat(contain("foo")), argThat(contain("FOO")))
Run Code Online (Sandbox Code Playgroud)

这导致:

Wanted 1 time:
 [error] -> ...
 [error] But was 2 times. Undesired invocation: ...
Run Code Online (Sandbox Code Playgroud)

在我看来,这样的事情应该是可能的,但我似乎无法弄明白.见解?

scala mockito specs2

5
推荐指数
1
解决办法
8591
查看次数