小编abh*_*bhi的帖子

如何在返回String的Spring MVC @ResponseBody方法中响应HTTP 400错误?

我使用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.

java spring spring-mvc http-error

366
推荐指数
8
解决办法
34万
查看次数

哪个是在Python中连接字符串的首选方法?

由于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中做了哪些改变?

python string concat python-3.x

325
推荐指数
10
解决办法
62万
查看次数

使用JUnit配置IntelliJ IDEA以进行单元测试

我决定今天早上通过试用版试用IntelliJ并安装了JUnit插件.我创建了一个新的Java项目,我想为它编写一个测试用例.

如何将junit.jar文件添加到项目中?(我实际上想将它添加到每个java项目中,现在和永远更多 - 有没有办法做到这一点?).

junit unit-testing intellij-idea

175
推荐指数
4
解决办法
20万
查看次数

循环遍历Python中的列表

我有一个包含子列表的列表.我想打印所有长度等于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中更改任何设置吗?

python list

166
推荐指数
3
解决办法
75万
查看次数

C++中size_t和int之间有什么区别?

在几个C++示例中,我看到使用size_t类型,我将使用一个简单的int.有什么区别,为什么size_t应该更好?

c c++ int types

157
推荐指数
4
解决办法
12万
查看次数

org.xml.sax.SAXParseException:prolog中不允许使用内容

我有一个基于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)

java xml

149
推荐指数
8
解决办法
52万
查看次数

使用Gson for Java进行JSON解析

我想解析来自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)

java json gson

146
推荐指数
8
解决办法
32万
查看次数

C++中的'typeid'与'typeof'

我想知道有什么区别之间typeid以及typeof在C++中.这就是我所知道的:

此外,这里是我创建的测试代码测试,我发现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)

c++ typeof typeid

144
推荐指数
5
解决办法
32万
查看次数

Spring中的@RequestBody和@ResponseBody注释

有人可以解释Spring 3中的注释@RequestBody@ResponseBody注释吗?它们适用于什么?任何例子都会很棒.

java spring annotations spring-mvc

134
推荐指数
3
解决办法
28万
查看次数

在一个语句中一次向HashMap添加多个条目

我需要初始化一个常量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 iphone android hashmap nsdictionary

121
推荐指数
6
解决办法
12万
查看次数