我一直在看杰克逊,但似乎我必须将Map转换为JSON,然后将结果JSON转换为POJO.
有没有办法将Map直接转换为POJO?
我正在通过curl发布一个文本文件,其中包含多行的列表到我的服务.当我在Spring MVC控制器中读取请求的主体时,没有新行字符,所有文本都在一行上.
curl会删除换行符吗?有没有办法维护新行字符,因为我需要它们来解析文本.这是我用来阅读帖子正文的代码片段:
StringBuilder builder = new StringBuilder();
String line = null;
try {
BufferedReader reader = request.getReader();
while ((line = reader.readLine()) != null) {// this is just one line of text!
builder.append(line);
}
} catch (Exception e) {
//handle exception
}
Run Code Online (Sandbox Code Playgroud) 如果在没有Accept标头的情况下将请求发送到我的API,我想将JSON设置为默认格式.我的控制器中有两个方法,一个用于XML,另一个用于JSON:
@RequestMapping(method = RequestMethod.GET,produces=MediaType.APPLICATION_ATOM_XML_VALUE)
@ResponseBody
public ResponseEntity<SearchResultResource> getXmlData(final HttpServletRequest request) {
//get data, set XML content type in header.
}
@RequestMapping(method = RequestMethod.GET, produces=MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity<Feed> getJsonData(final HttpServletRequest request){
//get data, set JSON content type in header.
}
Run Code Online (Sandbox Code Playgroud)
当我发送没有Accept标头的请求时,getXmlData会调用该方法,这不是我想要的.getJsonData如果没有提供Accept标头,有没有办法告诉Spring MVC调用该方法?
编辑:
有一个defaultContentType领域ContentNegotiationManagerFactoryBean可以解决问题.
pom.xml为了让PowerMock与Mockito合作,我需要添加哪些罐子?我有以下依赖项:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-support</artifactId>
<version>1.4.11</version>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
但是当我@PrepareForTest在类级别添加注释时,Eclipse无法找到它,但它可以找到PowerMockito.我错过了什么罐子?
我编写了一个REST API,我尝试测试没有Accept标头的请求.如果我通过Curl发送请求它会添加一个默认标题Accept: */*,如果你添加了Curl的-v参数,你可以看到这个.
有没有办法通过Curl发送没有Accept标头的请求?
我正在序列化以下模型:
class Foo {
private List<String> fooElements;
}
Run Code Online (Sandbox Code Playgroud)
如果fooElements包含字符串'one','two'和'three.JSON包含一个字符串:
{
"fooElements":[
"one, two, three"
]
}
Run Code Online (Sandbox Code Playgroud)
我怎么能让它看起来像这样:
{
"fooElements":[
"one", "two", "three"
]
}
Run Code Online (Sandbox Code Playgroud) 有没有一种方法来启用/禁用UNWRAP_ROOT_VALUE,并WRAP_ROOT_VALUE在杰克逊的ObjectMapper动态.我必须根据调用的服务启用/禁用这些属性,有些请求需要a JsonRootName,有些则不需要.我@JsonRootName在需要它的类中有注释.我有一个ObjectMapper扩展Jackson Object映射器的自定义类.我正在调用一个方法来启用/禁用属性,具体取决于调用的服务,但它似乎没有工作.
public void setWrapValue(boolean wrap) {
final AnnotationIntrospector introspector = new JacksonAnnotationIntrospector();
this.configure(org.codehaus.jackson.map.DeserializationConfig.Feature.UNWRAP_ROOT_VALUE, wrap);
this.configure(org.codehaus.jackson.map.SerializationConfig.Feature.WRAP_ROOT_VALUE, wrap);
this.setDeserializationConfig(this.getDeserializationConfig().withAnnotationIntrospector(introspector));
this.setSerializationConfig(this.getSerializationConfig().withAnnotationIntrospector(introspector));
}
Run Code Online (Sandbox Code Playgroud) 我有一个通过Spring MVC Web服务返回的数据的ATOM-XML表示.我正在使用JAXB进行序列化,我有许多名称空间但我希望默认名称空间设置为Atom而没有前缀.这是我到目前为止,package-info.java 但原子前缀被设置为ns3.
@XmlSchema(namespace = com.mycomponay.foo.ATOM_NAMESPACE,
xmlns = {
@XmlNs(prefix = "foo", namespaceURI = com.mycomponay.foo.NAMESPACE_FOO),
}, elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.mycompany.web;
import javax.xml.bind.annotation.XmlNs;
Run Code Online (Sandbox Code Playgroud)
另外我注意到命名空间显示在chrome中但不在Firefox中显示.
我试图模拟 Spring 的MessageSource.getMessage方法,但Mockito它抱怨一条无用的消息,我正在使用:
when(mockMessageSource.getMessage(anyString(), any(Object[].class), any(Locale.class)))
.thenReturn(anyString());
Run Code Online (Sandbox Code Playgroud)
错误信息是:
You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
when(mock.get(anyInt())).thenReturn(null);
doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
verify(mock).someMethod(contains("foo"))
Also, this error might show up because you use argument matchers with methods
that cannot be mocked Following methods *cannot* be stubbed/verified: final/private/equals()
/hashCode().
Run Code Online (Sandbox Code Playgroud)
知道我做错了什么吗?
如果我有文字:
test: firstString, blah: anotherString, blah:lastString
Run Code Online (Sandbox Code Playgroud)
如何获取文本"firstString"
我的正则表达式是:
test:(.*),
Run Code Online (Sandbox Code Playgroud)
编辑
带回来firstString, blah: anotherString,但我只需要带回'firstString'文本?