我使用Spring MVC作为一个简单的JSON API,@ResponseBody基于以下方法.(我已经有一个直接生成JSON的服务层.)
@RequestMapping(value = "/matches/{matchId}", produces = "application/json")
@ResponseBody
public String match(@PathVariable String matchId) {
String json = matchService.getMatchJson(matchId);
if (json == null) {
// TODO: how to respond with e.g. 400 "bad request"?
}
return json;
}
Run Code Online (Sandbox Code Playgroud)
问题是,在给定的场景中,用HTTP 400错误响应的最简单,最干净的方法是什么?
我确实遇到过这样的方法:
return new ResponseEntity(HttpStatus.BAD_REQUEST);
Run Code Online (Sandbox Code Playgroud)
...但我不能在这里使用它,因为我的方法的返回类型是String,而不是ResponseEntity.
由于Python string无法更改,我想知道如何更有效地连接字符串?
我可以这样写:
s += stringfromelsewhere
Run Code Online (Sandbox Code Playgroud)
或者像这样:
s = []
s.append(somestring)
later
s = ''.join(s)
Run Code Online (Sandbox Code Playgroud)
在写这个问题时,我发现了一篇很好的文章谈论这个话题.
http://www.skymind.com/~ocrow/python_string/
但它是在Python 2.x.中,所以问题是在Python 3中做了哪些改变?
我决定今天早上通过试用版试用IntelliJ并安装了JUnit插件.我创建了一个新的Java项目,我想为它编写一个测试用例.
如何将junit.jar文件添加到项目中?(我实际上想将它添加到每个java项目中,现在和永远更多 - 有没有办法做到这一点?).
我有一个包含子列表的列表.我想打印所有长度等于3的子列表.
我在python中执行以下操作:
for x in values[:]:
if len(x)==3:
print x
Run Code Online (Sandbox Code Playgroud)
values是原始列表.上面的代码是否打印了每个长度等于3的子列表x?我想只显示length==3一次的子列表.
问题已经解决了.问题出在Eclipse编辑器上.我不明白原因,但是当我运行循环时它只显示我列表的一半.
我有必要在Eclipse中更改任何设置吗?
在几个C++示例中,我看到使用size_t类型,我将使用一个简单的int.有什么区别,为什么size_t应该更好?
我有一个基于Java的Web服务客户端连接到Java Web服务(在Axis1框架上实现).
我在日志文件中遇到以下异常:
Caused by: org.xml.sax.SAXParseException: Content is not allowed in prolog.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLScanner.reportFatalError(Unknown Source)
at org.apache.xerces.impl.XMLDocumentScannerImpl$PrologDispatcher.dispatch(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at javax.xml.parsers.SAXParser.parse(Unknown Source)
at org.apache.axis.encoding.DeserializationContext.parse(DeserializationContext.java:227)
at org.apache.axis.SOAPPart.getAsSOAPEnvelope(SOAPPart.java:696)
at org.apache.axis.Message.getSOAPEnvelope(Message.java:435)
at org.apache.ws.axis.security.WSDoAllReceiver.invoke(WSDoAllReceiver.java:114)
at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32)
at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118)
at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83)
at org.apache.axis.client.AxisClient.invoke(AxisClient.java:198)
at org.apache.axis.client.Call.invokeEngine(Call.java:2784)
at org.apache.axis.client.Call.invoke(Call.java:2767)
at org.apache.axis.client.Call.invoke(Call.java:2443)
at org.apache.axis.client.Call.invoke(Call.java:2366)
at org.apache.axis.client.Call.invoke(Call.java:1812)
Run Code Online (Sandbox Code Playgroud) 我想解析来自JSON类型的数据String.我正在使用Google Gson.
我有:
jsonLine = "
{
"data": {
"translations": [
{
"translatedText": "Hello world"
}
]
}
}
";
Run Code Online (Sandbox Code Playgroud)
我的班级是:
public class JsonParsing{
public void parse(String jsonLine) {
// there I would like to get String "Hello world"
}
}
Run Code Online (Sandbox Code Playgroud) 我想知道有什么区别之间typeid以及typeof在C++中.这就是我所知道的:
typeid在type_info的文档中提到, 它在C++头文件typeinfo中定义.
typeof在C的GCC扩展和C++ Boost库中定义.
此外,这里是我创建的测试代码测试,我发现typeid它不会返回我的预期.为什么?
main.cpp中
#include <iostream>
#include <typeinfo> //for 'typeid' to work
class Person {
public:
// ... Person members ...
virtual ~Person() {}
};
class Employee : public Person {
// ... Employee members ...
};
int main () {
Person person;
Employee employee;
Person *ptr = &employee;
int t = 3;
std::cout << typeid(t).name() << std::endl;
std::cout << typeid(person).name() << std::endl; // …Run Code Online (Sandbox Code Playgroud) 有人可以解释Spring 3中的注释@RequestBody和@ResponseBody注释吗?它们适用于什么?任何例子都会很棒.
我需要初始化一个常量HashMap,并希望在一行语句中执行它.像这样避免某事:
hashMap.put("One", new Integer(1)); // adding value into HashMap
hashMap.put("Two", new Integer(2));
hashMap.put("Three", new Integer(3));
Run Code Online (Sandbox Code Playgroud)
在目标C中类似于此:
[NSDictionary dictionaryWithObjectsAndKeys:
@"w",[NSNumber numberWithInt:1],
@"K",[NSNumber numberWithInt:2],
@"e",[NSNumber numberWithInt:4],
@"z",[NSNumber numberWithInt:5],
@"l",[NSNumber numberWithInt:6],
nil]
Run Code Online (Sandbox Code Playgroud)
我没有找到任何一个例子来说明如何做这个看了这么多.
java ×5
c++ ×2
python ×2
spring ×2
spring-mvc ×2
android ×1
annotations ×1
c ×1
concat ×1
gson ×1
hashmap ×1
http-error ×1
int ×1
iphone ×1
json ×1
junit ×1
list ×1
nsdictionary ×1
python-3.x ×1
string ×1
typeid ×1
typeof ×1
types ×1
unit-testing ×1
xml ×1