我们的REST API接收一些JSON对象输入,其中某些字段必须不为null.那些可以是String/Integer,也可以是其他类实例作为引用.
我们试图找到一种方法来强制这些字段不为空,而不是在api中进行空检查的正确方法.当前:
if (myObject.getSomeOtherObject() == null)
throw new SomeException();
Run Code Online (Sandbox Code Playgroud)
我们想拥有的是:
class MyObject{
@Required
OtherObject someOtherObject;
// ...
}
Run Code Online (Sandbox Code Playgroud)
我们尝试了3件事:
升级到jackson 2.0.6并使用注释com.fasterxml.jackson.annotation.JsonProperty但是,这看起来不起作用.找到了这些参考文献:http: //jira.codehaus.org/browse/JACKSON-767
扩展JsonDeserializer以检查null但问题是它甚至没有在null输入上执行.
public class NotNullDeserializer<T> extends JsonDeserializer<T> {
@Override
public T deserialize(JsonParser jsonparser, DeserializationContext deserializationcontext) throws IOException, JsonProcessingException {
ParameterizedType superClass = (ParameterizedType) getClass().getGenericSuperclass();
Class<T> type = (Class<T>) superClass.getActualTypeArguments()[0];
T t = jsonparser.readValueAs(type);
if (t == null){
String classNameField = type.getName();
String field = jsonparser.getCurrentName();
throw new WrongInputException("The field '"+field+"' of type '"+classNameField+"' should not be …Run Code Online (Sandbox Code Playgroud) 我添加了一个关闭钩子:
Runtime.getRuntime().addShutdownHook(myShutdownHook);
Run Code Online (Sandbox Code Playgroud)
它正常工作正常,但是当我单击Eclipse中的红色停止按钮时则不行.有没有办法在Eclipse中调用关闭钩子?
我有一个类实现ServletContextListener,在启动时加载一些资源.
当我的逻辑中发生一些不良事件时,这些资源对于我希望在整个启动时失败的方式对应用程序至关重要.
是否有任何命令可以从ServletContextListener.contextInitialized()方法内部执行以停止和失败整个Tomcat启动?
代码:
File dir = new File(path);
boolean rc1 = dir.setExecutable(true, false);
boolean rc2 = dir.setReadable(true, false);
boolean rc3 = dir.setWritable(true, false);
if (!rc1 || !rc2 || !rc3){
logger.warn("One of the permissions set returned false: rc1="+rc1+" rc2="+rc2+" rc3="+rc3 + " [for dir '"+dir+"']");
}
Run Code Online (Sandbox Code Playgroud)
在Ubuntu上,所有3个调用都返回false.在我的Windows上,只有第三次调用setWritable会返回false.
目标是创建文件/目录,以便用户(tomcat)和组能够读/写.
但是在Ubuntu上创建的文件没有写入组的权限.
这是我的HQL:
Query query = createQueryOnCurrentSession("DELETE Email e " +
"where " +
"(status = :sent and creationTime <= :creation)" +
"or " +
"(status = :error and maxEttempts >= :maxEttempts)");
Run Code Online (Sandbox Code Playgroud)
这是生成的SQL:
delete from `email` where `status`=? and `creation_time`<=? or `status`=? and `attempts`>=?
Run Code Online (Sandbox Code Playgroud)
问题:为什么括号不在SQL中?我希望它是:
delete from `email` where (`status`=? and `creation_time`<=?) or (`status`=? and `attempts`>=?)
Run Code Online (Sandbox Code Playgroud)
也许我会在2个请求中删除?
delete from `email` where `status`=? and `creation_time`<=?
delete from `email` where `status`=? and `attempts`>=?
Run Code Online (Sandbox Code Playgroud) 我有对象链接有List的成员,而链接只有属性但解析列表有错误 - 它被创建为空.
在下面的测试中links.getLinks()返回空列表.有任何想法吗?
XML示例:
<links>
<link x="1" y="2" />
<link x="3" y="4" />
</links>
Run Code Online (Sandbox Code Playgroud)
Java
@JacksonXmlRootElement(localName="links")
public class Links extends BaseAmebaElement {
@JacksonXmlProperty(localName="link")
//@JacksonXmlElementWrapper(localName="link")
private Collection<Link> links;
public Collection<Link> getLinks() {
return links;
}
public void setLinks(Collection<Link> links) {
this.links = links;
}
}
Run Code Online (Sandbox Code Playgroud)
...
@JacksonXmlRootElement(localName="link")
public class Link {
@JacksonXmlProperty(localName="x", isAttribute=true)
private String href;
@JacksonXmlProperty(localName="y", isAttribute=true)
private String rel;
Run Code Online (Sandbox Code Playgroud)
...
XmlMapper xmlMapper = new XmlMapper ();
try {
Links links = xmlMapper.readValue(input, Links.class);
assertNotNull(links);
assertNotNull(links.getLinks());
assertEquals(2, …Run Code Online (Sandbox Code Playgroud) Tomcat中Request参数被丢弃的相关问题
嗯......显然,即使是最简单的请求(如下所示)在某些服务器上也会丢失参数,而有些则没问题。
@GET
@Path("/get-retrieve")
public String foo(){
return ""+httpServletRequest.getParameterMap().size();
}
Run Code Online (Sandbox Code Playgroud)
所以返回值是0(零)。
更新:AccessLogValve 记录的请求包含参数
127.0.0.1 - - [26/Nov/2012:03:04:58 -0800] "POST /api/get-retrieve?x=y HTTP/1.1" 200 16
Run Code Online (Sandbox Code Playgroud)
所以,问题可能出在 Tomcat 中的某个地方,抛出了这些参数......