Hibernate实体配置成WEB-INF(Hibernate entities configuration into WEB-INF)
我需要将hibernate实体配置xml放入WEB-INF目录。
/hibernate.cfg.xml进入目录: / WEB-INF / hibernate /
实体配置文件分别为: / WEB-INF / hibernate / entities /
我必须写入标签
<mapping resource="/WEB-INF/hibernate/entities/book.hbm.xml" />
以正确加载实体配置?我收到此错误:
java.lang.Exception: java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.hibernate.MappingNotFoundException: resource: /WEB-INF/hibernate/entities/book.hbm.xml not found
提前致谢。
I need to put hibernate entities configuration xml, into the WEB-INF directory.
The /hibernate.cfg.xml is into the directory: /WEB-INF/hibernate/
The entities configuration files are into: /WEB-INF/hibernate/entities/
What I have to write into the tag
<mapping resource="/WEB-INF/hibernate/entities/book.hbm.xml" />
to load correctly entities configuration?I receive this error:
java.lang.Exception: java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.hibernate.MappingNotFoundException: resource: /WEB-INF/hibernate/entities/book.hbm.xml not found
Thanks in advance.
原文:https://stackoverflow.com/questions/29078555
最满意答案
你有两个选择。
在getter或field上使用
@JsonIgnore
带有
@JsonIgnore
注解的Sample
类:class Sample { private String prop1; private String prop2; @JsonIgnore // could be here private String prop3; @JsonIgnore // could be here public String getProp3() { return prop3; } }
ObjectMapper
使用Sample
类中的注释:new ObjectMapper().writeValueAsString(new Sample("one", "two", "three"))
第二个变体是使用自定义序列化器:
自定义序列化器:
class SampleSerializer extends JsonSerializer<Sample> { @Override public void serialize(Sample sample, JsonGenerator jgen, SerializerProvider provider) throws IOException { jgen.writeStartObject(); jgen.writeStringField("prop1", sample.getProp1()); jgen.writeStringField("prop2", sample.getProp2()); jgen.writeEndObject(); } }
添加注释以使用自定义序列化器:
@JsonSerialize(using = SampleSerializer.class) class Sample { private String prop1; private String prop2; private String prop3; }
ObjectMapper
使用Sample
类中的注释:new ObjectMapper().writeValueAsString(new Sample("one", "two", "three"))
如果您完全不使用Jackson批注,则可以将自定义序列化程序添加到
ObjectMapper
:
Sample
类不包含任何Jackson注释:class Sample { private String prop1; private String prop2; private String prop3; }
ObjectMapper
应该知道如何序列化Sample
类的方法:ObjectMapper mapper = new ObjectMapper(); SimpleModule module = new SimpleModule(); module.addSerializer(Sample.class, new SampleSerializer()); mapper.registerModule(module); mapper.writeValueAsString(new Sample("one", "two", "three"));
You have two options.
Use
@JsonIgnore
on getter or field
Sample
class with@JsonIgnore
annotation:class Sample { private String prop1; private String prop2; @JsonIgnore // could be here private String prop3; @JsonIgnore // could be here public String getProp3() { return prop3; } }
ObjectMapper
use annotations from theSample
class:new ObjectMapper().writeValueAsString(new Sample("one", "two", "three"))
Second variant, is to use custom serializer:
Custom serializer:
class SampleSerializer extends JsonSerializer<Sample> { @Override public void serialize(Sample sample, JsonGenerator jgen, SerializerProvider provider) throws IOException { jgen.writeStartObject(); jgen.writeStringField("prop1", sample.getProp1()); jgen.writeStringField("prop2", sample.getProp2()); jgen.writeEndObject(); } }
Add annotation to use custom serializer:
@JsonSerialize(using = SampleSerializer.class) class Sample { private String prop1; private String prop2; private String prop3; }
ObjectMapper
use annotations from theSample
class:new ObjectMapper().writeValueAsString(new Sample("one", "two", "three"))
If you do not use Jackson annotations for the class at all, you could add your custom serializer to
ObjectMapper
:
Sample
class does not contain any Jackson annotation:class Sample { private String prop1; private String prop2; private String prop3; }
ObjectMapper
should know about the way how to serializeSample
class:ObjectMapper mapper = new ObjectMapper(); SimpleModule module = new SimpleModule(); module.addSerializer(Sample.class, new SampleSerializer()); mapper.registerModule(module); mapper.writeValueAsString(new Sample("one", "two", "three"));
相关问答
更多-
什么是@JsonUnwrapped的杰克逊反序列化等价物?(What's the Jackson deserialization equivalent of @JsonUnwrapped?)[2022-04-02]
您可以使用@JsonCreator和@JsonProperty为每个字段: @JsonCreator public Parent(@JsonProperty("age") Integer age, @JsonProperty("firstName") String firstName, @JsonProperty("lastName") String lastName) { this.age = age; this.name = new Name(firstName, las ... -
你必须配置你的对象映射器来支持这种情况。 ObjectMapper objectMapper = ... objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); 这个功能的文档可以在这里找到: 空梁上失败 确定在找到某个类型没有访问者时会发生什么的功能(并且没有注释表明它将被序列化)。 如果启用(默认),则抛出异常以将它们指示为不可序列化的类型; 如果禁用,它们将被序列化为空对象,即没有任何属性。 The ans ...
-
正如您所料,您已经使用了错误的方式来反序列化。 Forms类仅适用于PlayForms,不适用于Json请求。 看看BodyParser和JsonActions文档: @BodyParser.Of(BodyParser.Json.class) public Result index() { RequestBody body = request().body(); Data data = Json.fromJson(body.asJson(), Data.class); return ok( ...
-
如何忽略某些没有jackson注释的反序列化字段?(How to ignore certain fields for deserialization without jackson annotations?)[2024-04-02]
你有两个选择。 在getter或field上使用@JsonIgnore 带有@JsonIgnore注解的Sample类: class Sample { private String prop1; private String prop2; @JsonIgnore // could be here private String prop3; @JsonIgnore // could be here public String getProp3() { ... -
杰克逊支持没有注释和专用bean字段的多态性(Jackson support for polymorphism without annotations and dedicated bean fields)[2023-09-14]
如果混合使用不符合您的喜好,则没有任何预定义可以通过,但您可以通过对JacksonAnnotationIntrospector进行子类化并使用它配置mapper来相对轻松地实现此目的。 在您的实现中,您可以覆盖注释访问的所有方面:在您的情况下,它可能足以覆盖findTypeResolver() (如果您想要每个属性覆盖,'findPropertyTypeResolver()')。 然后,该方法可以使用您想要构造TypeResolverBuilder任何机制(很可能是StdTypeResolverBuilde ... -
杰克逊正确地使用我的机器1.8.1中的Jackson版本对细节属性进行了反序列化。 您能否确认您为Employee类创建了访问者,如果不是,那可能是原因。 Jackson correctly deserialized the details attribute with the version of Jackson that I have in my machine-1.8.1. Can you confirm that you have accessors created for Employee cla ...
-
您根本不必创建2个类。 使用Jackson,您可以使用注释在序列化和反序列化期间完全控制属性的行为,在getter中使用@JsonIgnore可以防止在Json响应中对属性进行序列化,并在setter中使用@JsonProperty注释在属性期间设置属性反序列化。 代码如下所示: import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; import co ...
-
你需要的是用@JsonProperty注释计算属性,所以它看起来像这样: class MyClass { @Getter @Setter int a; @Getter @Setter int b; @JsonProperty public int getADivB() { return a / b; } } What you need is to annotate calculated property with @JsonProperty so it wi ...
-
禁用DeserializationConfig.Feature.USE_GETTERS_AS_SETTERS 。 mapper.configure(DeserializationConfig.Feature.USE_GETTERS_AS_SETTERS, false); 使用静态导入可缩短此行。 或者,如果您希望注释仅为此属性配置事物,而不是如上所述指定全局设置,则将某些内容标记为“团队”的设置者。 public class Foo { @JsonSetter("teams") public vo ...
-
显然,最新的春季启动版本不再出现这种问题。 如果我有这样的注释,它现在有效 @OneToMany(mappedBy = "alert", cascade = CascadeType.ALL, fetch=FetchType.EAGER) private List
alertInfos; Apparently this issues are no longer the case with the newest spring boot release. It works now if I ...