有没有办法迫使杰克逊不要把类名放在Json输出中?我问了一个问题,导致这个问题,但我要问什么,我希望是一个更集中的问题.我这样做是在使用Spring MVC,但我不确定这有多重要.
所以,而不是
{"NamedSystem":{"name":"Bob Dole","id":"0198b96c-fe18-4aa6-9e61-b5f7149414c2"}}
我想要
{"name":"Bob Dole","id":"0198b96c-fe18-4aa6-9e61-b5f7149414c2"}
我可能更喜欢前者,但我不知道如何让杰克逊在反序列化时识别出类名,所以如果有人知道,那也会有所帮助.
有人可以解释为什么我会在 spring-ws 中使用这些端点注释之一而不是另一个吗?特别是,哪一个被认为最具包容性?我可以同时使用这三个吗?我已阅读该文档,并且知道 @Action 使用 ws-addressing 而 @SoapAction 使用 SOAP Action 传输标头,但我不太确定哪个更适合使用。
谢谢。
标题基本上都说明了一切.我尝试过谷歌搜索,但返回了大量的误报.我想我只是想知道这两个具体数字背后是否有一定的理由,或者他们是否很容易就有其他许多数字?
编辑:并且,由于数字的来源已被回答,布尔hashCode方法的编写者使用这些数字的任何理由(除了他们是素数)?其他任何一个素数都可以效果吗?
我有一个这样的课:
public class Loader implements DisposableBean {
Class1 c1;
Class2 c2;
Loader(Class c1, Class c2) {
this.c1 = c1;
this.c2 = c2;
}
@Override
public void destroy() throws Exception {
c1.do();
c2.do();
}
}
Run Code Online (Sandbox Code Playgroud)
它在@Configuration类中创建为@Bean.
@Bean(autowire = Autowire.BY_NAME, name = "loader")
public Loader getLoader() {
return new Loader(getC1(), getC2());
}
Run Code Online (Sandbox Code Playgroud)
它通过我的web.xml中的上下文加载器侦听器进行实例化
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-loader.xml</param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)
该应用程序上下文具有以下行:
<context:component-scan base-package="name.of.package.loader.is.in" />
Run Code Online (Sandbox Code Playgroud)
我在tomcat中启动war时在构造函数中放置了一个断点,我可以验证c1和c2是否按预期填充.但是,当我关闭tomcat并进入destroy方法时,c1和c2都为null,因此抛出了NPE.为什么这些价值不再存在?
啊,darnit,应该进一步挖掘.它在我不熟悉的代码部分被清除,我终于找到了它.
我在调用服务时让杰克逊正确地将json反序列化为一个对象(特别是我们使用Jackson的能力来使用JAXB注释,因为我们也希望服务使用XML).我正在使用Spring MVC,我正在使用RestTemplate类来调用服务.
这是我为junit设置MappingJacksonHttpMessageConverter的地方:
ObjectMapper jsonMapper = new ObjectMapper();
AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
jsonMapper.getDeserializationConfig().setAnnotationIntrospector(introspector);
jsonMapper.getSerializationConfig().setAnnotationIntrospector(introspector);
jsonMapper.getSerializationConfig().setSerializationInclusion(Inclusion.NON_NULL);
MappingJacksonHttpMessageConverter jacksonConverter = new MappingJacksonHttpMessageConverter();
jacksonConverter.setObjectMapper(jsonMapper);
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
converters.add(jacksonConverter);
template.setMessageConverters(converters);
Run Code Online (Sandbox Code Playgroud)
我这样称呼服务:
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("Accept", "application/json");
HttpEntity<String> requestEntity = new HttpEntity<String>(requestHeaders);
ResponseEntity<NamedSystem> responseEntity = template.exchange(baseURL + "/{NamedSystemId}",
HttpMethod.GET, requestEntity, NamedSystem.class, orgId1);
Run Code Online (Sandbox Code Playgroud)
我的NamedSystem课程设置如下:
@XmlRootElement(name = "NamedSystem", namespace = "http://schemas.abc.workplace.com/NamedSystem")
public class NamedSystem {
private String id;
private String name;
private String description;
private Set<NamedSystemAlias> aliases;
private String href; …Run Code Online (Sandbox Code Playgroud) 所以,我在我的一个java文件中有这样的东西:
@Resource(name = "initializationCache")
Boolean initializationCache;
Run Code Online (Sandbox Code Playgroud)
在配置文件中,我有这个:
<bean id="initializationCache" class="java.lang.Boolean">
<constructor-arg value="${initialization.cache}" />
</bean>
Run Code Online (Sandbox Code Playgroud)
我将如何使用原始布尔值来完成这项工作?
这个SO的答案解释了我遇到的问题:HashSet.remove()和Iterator.remove()无法正常工作
基本上,一旦我向HashSet添加了一些东西,如果我修改了它的任何字段,那么该集合将使用包含具有完全相同字段的对象的集合的任何相等性测试失败,因为它存储的哈希码是用于它的时候设置了不同的字段.
那么,既然这个答案解释了正在发生什么,那么为什么一个好的解决方法是既具有使用集合的唯一性又能够修改集合中对象的内部字段?或者这是不可能的?
我试图使用Jsoup在html中提取特定内容.以下是示例html内容.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body class="">
<div class="shop-section line bmargin10 tmargin10">
<div class="price-section fksk-price-section unit">
<div class="price-table">
<div class="line" itemprop="offers" itemscope="" itemtype="http://schema.org/Offer">
<div class="price-save">
<span class="label-td"><span class="label fksk-label">Price :</span></span>
</div>
<span class="price final-price our fksk-our" id="fk-mprod-our-id">Rs.<span class="small-font"> </span>11990</span>
</div>
<meta itemprop="price" content="Rs. 11990" />
<meta itemprop="priceCurrency" content="INR" />
<div class="our-price-desc fksk-our-price-desc">
<small>(Prices are inclusive of all taxes)</small>
</div>
</div>
</div>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我使用以下命令获得了所需的输出:
document.select(".price-table").select(".line").select("span").get(2).text()
Run Code Online (Sandbox Code Playgroud)
看起来很冗长.我不能直接使用span类("价格最终价格我们的fksk-our")?
对此有何帮助?
这对我来说真的很令人沮丧,所以我会尝试解释我所知道的,现在不要解释.请务必在帖子底部查看我的更新.
首先,我们有一场使用Spring-WS来处理消息的战争.我们还有一个使用WebServiceTemplate发送消息的客户端.我们在Tomcat 7上部署.
在我们当前的开发环境中,Tomcat 7服务器正在使用JRE 6u24.在本地,当我使用客户端发送SOAP消息时,我正在运行相同的版本.一切都按预期工作.
当我创建本地JRE 7u04时,我得到500响应并且服务器看到以下错误:
[2012-05-05 20:19:13,179] DEBUG - org.springframework.web.servlet.FrameworkServlet.processRequest(671) | Could not complete request
org.springframework.ws.soap.saaj.SaajSoapEnvelopeException: Could not access envelope: Unable to create envelope from given source: ; nested exception is com.sun.xml.messaging.saaj.SOAPExceptionImpl: Unable to create envelope from given source:
at org.springframework.ws.soap.saaj.SaajSoapMessage.getSaajVersion(SaajSoapMessage.java:260)
at org.springframework.ws.soap.saaj.SaajSoapMessage.getImplementation(SaajSoapMessage.java:342)
at org.springframework.ws.soap.saaj.SaajSoapMessage.<init>(SaajSoapMessage.java:117)
at org.springframework.ws.soap.saaj.SaajSoapMessageFactory.createWebServiceMessage(SaajSoapMessageFactory.java:184)
at org.springframework.ws.soap.saaj.SaajSoapMessageFactory.createWebServiceMessage(SaajSoapMessageFactory.java:58)
at org.springframework.ws.transport.AbstractWebServiceConnection.receive(AbstractWebServiceConnection.java:90)
at org.springframework.ws.transport.support.WebServiceMessageReceiverObjectSupport.handleConnection(WebServiceMessageReceiverObjectSupport.java:86)
at org.springframework.ws.transport.http.WebServiceMessageReceiverHandlerAdapter.handle(WebServiceMessageReceiverHandlerAdapter.java:57)
at org.springframework.ws.transport.http.MessageDispatcherServlet.doService(MessageDispatcherServlet.java:222)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:560)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:471)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:402)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329) …Run Code Online (Sandbox Code Playgroud) 是否可以使用Spring注释配置覆盖导入的资源?
配置类:
@Configuration
@ImportResource({"classpath:applicationContext.xml"})
public class CoreConfiguration {
@Resource(name = "classA")
private ClassA classA;
@Bean(name = "nameIWantToOverride")
private ClassB classB() {
return new ClassB("different setting");
}
}
Run Code Online (Sandbox Code Playgroud)
applicationContext.xml包括:
<bean name="classA" class="a.b.c.ClassA">
<property name="nameIWantToOverride" ref="classB" />
</bean>
Run Code Online (Sandbox Code Playgroud)
如果classA有一个classB字段,但我希望它使用我在配置类中定义的ClassB,那可能吗?我试过切换订单,但没有帮助.似乎XML优先,因为当我运行实例化配置的简单测试时,它永远不会到达classB方法.如果我更改名称使其与xml文件中的bean不匹配,那么它确实到达了classB方法.
我已经看到了它可以以另一种方式工作的地方:Spring框架可以覆盖基于XML的配置的基于注释的配置吗?但是这个方向怎么样?由于这是更新的做事方式,我认为你能做到这一点.
我该怎么做才能解决这个问题?
编辑:使用XML更新.假设classA有多个字段,但我只想替换它.