小编uri*_*rir的帖子

在JSON对象中强制执行非空字段

我们的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)

java rest json jackson required

25
推荐指数
1
解决办法
4万
查看次数

关闭挂钩在Eclipse中不起作用

我添加了一个关闭钩子:

Runtime.getRuntime().addShutdownHook(myShutdownHook);
Run Code Online (Sandbox Code Playgroud)

它正常工作正常,但是当我单击Eclipse中的红色停止按钮时则不行.有没有办法在Eclipse中调用关闭钩子?

java eclipse shutdown-hook

25
推荐指数
2
解决办法
2万
查看次数

如何在ServletContextListener中的异常中止Tomcat启动?

我有一个类实现ServletContextListener,在启动时加载一些资源.

当我的逻辑中发生一些不良事件时,这些资源对于我希望在整个启动时失败的方式对应用程序至关重要.

是否有任何命令可以从ServletContextListener.contextInitialized()方法内部执行以停止和失败整个Tomcat启动?

java tomcat servlets

9
推荐指数
1
解决办法
5090
查看次数

设置文件权限始终返回FALSE

代码:

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上创建的文件没有写入组的权限.

java io permissions file-permissions file

7
推荐指数
1
解决办法
9348
查看次数

HQL中的括号不会转换为SQL

这是我的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)

java hibernate

6
推荐指数
1
解决办法
4390
查看次数

无法解析具有属性的元素列表

我有对象链接有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)

java xml jackson fasterxml

4
推荐指数
1
解决办法
2874
查看次数

服务器请求丢失参数

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 中的某个地方,抛出了这些参数......

java tomcat web-services servlets servlet-filters

3
推荐指数
1
解决办法
2485
查看次数