我正在尝试做一些在gson中非常容易的事情.由于我切换到Jackson作为序列化器,我无法弄清楚如何实现这个:
我想仅序列化已由Annotation标记的字段.GSON代码将是:
class Foo {
@Expose
public String sometext="Hello World";
@Expose
public int somenumber=30;
public float noop=1.0;
...
}
Run Code Online (Sandbox Code Playgroud)
应该导致(JSON)
{
Foo: {
sometext:'Hello World',
somenumber: 30
}
}
Run Code Online (Sandbox Code Playgroud)
(语法错误可能会被忽略 - 源仅用于演示)
那么什么是杰克逊对口GSON的@Expose和new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();?
我得到了一个 Makefile,它确实构建了一个 32 位和 64 位版本的项目:
#############################
# 32 bit variant of test tool
#############################
hidtest_32: $(OBJS_32)
$(CXX_32) -m32 -g $^ $(LIBS) -o hidtest_32
hid_32.o: hid.c
$(CC_32) -m32 $(CFLAGS) $< -o hid_32.o
../hidtest/hidtest_32.o: ../hidtest/hidtest.cpp
$(CC_32) -m32 $(CFLAGS) $< -o ../hidtest/hidtest_32.o
#############################
# 64 bit variant of test tool
#############################
hidtest_64: $(OBJS_64)
$(CXX_64) -m64 -g $^ $(LIBS) -o hidtest_64
hid_64.o: hid.c
$(CC_64) -m64 $(CFLAGS) $< -o hid_64.o
../hidtest/hidtest_64.o: ../hidtest/hidtest.cpp
$(CC_64) -m64 $(CFLAGS) $< -o ../hidtest/hidtest_64.o
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,两种变体都使用完全相同的构建过程,只是编号32已替换为64.
我试过类似的东西 …
我想使用gmock(Google Mock)作为XCode中C++项目的模拟框架.因此我必须将gmock编译为gmock.framework.不幸的是,该项目没有专门的XCode项目(gtest包含一个).
在XCode中创建一个Framework项目并构建它失败了多个错误(基本上标题中使用的包含路径似乎被错误地查找).
那么(并且对于一般问题很抱歉)如何将gmock编译为Framework以便能够在其他XCode项目中使用它?
可能重复:
在Rails 2.3.5中覆盖to_json
LIB/responses.rb
module Responses
class Response
def to_json
JSON.pretty_generate(self)
end
end
class ErrorResponse < Response
def initialize(cause)
self[:type]="Error"
self[:casue]=cause
end
end
class DataResponse < Response
attr_accessor :data
end
end
Run Code Online (Sandbox Code Playgroud)
这由控制器使用:
response=Responses::DataResponse.new
response.data=someData
render :json => response
Run Code Online (Sandbox Code Playgroud)
现在,我得到一个错误wrong number of arguments (1 for 0)在lib/responses.rb:3:in to_json.为什么?没有参数传递给to_json隐式调用的render :json.那我的错误在哪里?
通常 rails 会神奇地解码所有params. 现在我得到了一个 javascript,它执行params="value="+encodeURIComponent('ab#cd');然后调用http://server/controller?value=ab%23cd. 如果我params[:value]在我的控制器中访问,它包含ab%23cd而不是ab#cd我所期望的。
如何解决这个问题?为什么 rails 没有自动解码这个参数?
我正在使用 jersey 在不同位置注入 POJO。这是我的配置:
register(new AbstractBinder() {
@Override
protected void configure() {
bind(Bar.class).to(Bar.class).in(Singleton.class);
bindFactory(FooFactory.class).to(Foo.class).in(Singleton.class);
...
}
});
Run Code Online (Sandbox Code Playgroud)
福工厂:
public class FooFactory implements Factory<Foo> {
@Override
public Foo provide() {
return Foo.newInstance();
}
}
Run Code Online (Sandbox Code Playgroud)
注入资源的工作原理:
@Path("/myresource")
public class MyResource{
@Inject
protected Bar instance;
}
Run Code Online (Sandbox Code Playgroud)
但
public class Foo {
@Inject
protected Bar instance;
}
Run Code Online (Sandbox Code Playgroud)
才不是。Foo.instance一片空白。为什么?以及如何让它发挥作用?
我有Java String ôð¤ Ø$î1<¨ V¸dPžÐ ÀH@ˆàÀༀ@~€4`,我想用ANSI编码写一个文件.
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(output),"windows-1252"));
try {
out.append(str);
} finally {
out.close();
}
Run Code Online (Sandbox Code Playgroud)
调试器说str包含ôð¤ Ø$î1<¨ V¸dPÐ ÀH@àÀà¼@~4.只要我将其写入输出文件,该文件只包含?ÒÜ@4.那么我的方法写入文件有什么问题?
对不起这个奇怪的字符串 - 我试图在java中重写一个delphi 7函数.这些字符串是我得到的唯一样本.
我有一个发送应用程序事件的类。接收者不能错过这个事件,因此发送者依赖于接收者。
@Service
@DependsOn("receiver")
class Sender {
...
@PostConstruct
public void init(){
applicationEventPublisher.publishEvent(new MyEvent());
}
}
@Service
class Receiver {
...
@EventListener
public void onEvent(MyEvent event) {
System.out.println("Event catched");
}
}
Run Code Online (Sandbox Code Playgroud)
在调试模式下,您可以看到它Sender在之后初始化Receiver,这应该导致接收方始终捕获发送方的事件 - 但事实并非如此。
事实上,在初始化接收器和准备接收事件之间似乎存在延迟。如果我在发送方中延迟发布事件几毫秒,接收方就会按预期捕获它。
因此,似乎并@DependsOn不能完全确保接收器在发送者之前完全初始化,这与记录的内容完全相反。
如何在不使用任何难看的延迟的情况下实现接收者捕获事件?
我有一个程序,检查服务器上是否有版本更新.现在我必须做类似的事情
if(update_avail) {
system("updater.exe");
exit(0);
}
Run Code Online (Sandbox Code Playgroud)
但无需等待"updater.exe"完成.否则我无法替换我的主程序,因为它正在运行.那么如何执行"updater.exe"并立即退出?我知道*nix方式fork等等,如何在Windows中执行此操作?
我已经通过bean绑定(Netbeans)将JDialog绑定到了我的(单独的)控制器类。我的对话框有一个“关闭”按钮。此按钮的action属性绑定到控制器中的一个动作。
对话:
public class AppVersionCheckDialog extends javax.swing.JDialog {
...
binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, controller, org.jdesktop.beansbinding.ELProperty.create("${closeButtonActionListener}"), btnOk, org.jdesktop.beansbinding.BeanProperty.create("action"));
bindingGroup.addBinding(binding);
...
}
Run Code Online (Sandbox Code Playgroud)
所以基本上我得到了
public class AppVersionCheckDialogController extends AbstractController {
private final Action closeAction = new AbstractAction("Close") {
@Override
public void actionPerformed(ActionEvent e) {
// dialog.dispose() - no reference of dialog instance here
}
};
public Action getCloseButtonActionListener(){
return closeAction;
}
}
Run Code Online (Sandbox Code Playgroud)
在我的控制器中。
我对控制器内的对话框没有任何引用。我不想介绍一个,因为它打破了将事物绑定在一起的整个原理。
那么如何关闭对话框呢?有没有办法将对话框实例绑定到控制器的属性?如果是这样,怎么办?
服务
@Service
class Foo{
Foo(@Value("${my.property}")int delay){
...
}
}
Run Code Online (Sandbox Code Playgroud)
消费者
class Bar {
@Autowire
Foo foo;
}
Run Code Online (Sandbox Code Playgroud)
bean.xml
<context:component-scan base-package="foo.*" />
<context:spring-configured />
<context:property-placeholder location="classpath:foo/internal.properties"/>
Run Code Online (Sandbox Code Playgroud)
internal.properties确实包含my.property=5000. 但似乎 spring 甚至不关心@Value注释。如果我运行该应用程序,spring 会抱怨没有找到默认构造函数。
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [foo.Foo]: No default constructor found; nested exception is java.lang.NoSuchMethodException...
Run Code Online (Sandbox Code Playgroud)
我什至尝试使用constructor-arg标记在 beans.xml 中配置参数。此方法会产生相同的错误。
为什么价值注入不起作用?
我必须使用 swing-ui 设计器工具来创建我的 UI,该工具仅支持图形编辑 JPanel。这些面板(它们基本上包含复杂的按钮设计)像 JButton 一样工作。我不能使用 JPanel 以外的任何东西作为这些面板的基类(UI 编辑器限制)。
执行此操作的最通用的解决方案是什么?
@Injectable()
export class MyService {
...
constructor(@Inject('CONFIG') private config:ConfigOptions){
this.connection= // await initConnection(config); <-- this returns a promise! How to await this?
}
send(payload:string){
this.connection.send(payload)
}
}
Run Code Online (Sandbox Code Playgroud)
如何await异步调用以initConnection()确保服务在使用前已初始化?