小编Seb*_* S.的帖子

Spring数据JPA和可以为null的参数

我的理解是,使用Spring数据JPA我不能有一个查询方法来获取列等于给定的非null方法参数的所有行,并且当method参数为null时,使用相同的方法获取此列为NULL的所有行.

那是对的吗?

所以我必须在我的JAVA代码中区分它,我必须使用一个单独的查询方法显式询问空值,如下例所示?

// Query methods
List<Something> findByParameter(Parameter parameter);
List<Something> findByParameterIsNull();

...

List<Something> result = new ArrayList<>();

if (parameter == null)
  result = findByParameterIsNull();
else
  result = findByParameter(parameter);
Run Code Online (Sandbox Code Playgroud)

这很糟糕,如果我有4个参数可以为null并且必须编码16种不同的查询方法.

java parameters null spring-data-jpa

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

Java 8:比较不同类型列表的更有效方法?

在单元测试中,我想验证两个列表包含相同的元素.要测试的列表是Person对象列表的构建,其中String提取了一个类型的字段.另一个列表包含String文字.

人们经常会找到以下代码片段来完成此任务(请参阅此答案):

List<Person> people = getPeopleFromDatabasePseudoMethod();
List<String> expectedValues = Arrays.asList("john", "joe", "bill");

assertTrue(people.stream().map(person -> person.getName()).collect(Collectors.toList()).containsAll(expectedValues));
Run Code Online (Sandbox Code Playgroud)

Person课程如下:

public class Person {

    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    // other getters and setters
}
Run Code Online (Sandbox Code Playgroud)

在上面的示例中,使用Java 8技术将人员(或人员)列表转换为字符串列表,并且以旧式方式进行比较.

现在我想知道,如果有更直接或更有效的方法使用其他Java 8语句进行比较,例如allMatch()或某些Predicate<T>或其他东西.

comparison lambda java-8 java-stream

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

如何将JAX-RS与CDI集成在Servlet 3.0容器中

我有一个使用JSF 2.2(Mojorra 2.1.3)和CDI 1.1(Weld 2.0.3)在Servlet 3.0容器(Jetty 9.0.4)上运行的Web应用程序.没有使用完整的应用程序服务器.在这个应用程序中,我还有一个服务REST请求的JAX-RS 2.0(Jersey 2.2)资源类.我已经集成了JAXB绑定和JSON编组(Jackson 2.2).我使用Maven 3.0.5进行构建管理.这些是我项目设置的相关部分:

Maven pom.xml:

...
<dependencies>

    <!-- Servlet 3.0 API -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>

    <!-- Contexts and Dependency Injection for Java EE -->
    <dependency>
        <groupId>org.jboss.weld.servlet</groupId>
        <artifactId>weld-servlet</artifactId>
        <version>2.0.3.Final</version>
    </dependency>

    <!-- JavaServer Faces -->
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-api</artifactId>
        <version>2.2.2</version>
    </dependency>
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-impl</artifactId>
        <version>2.2.2</version>
    </dependency>

    <!-- JAX-RS RESTful Web Services -->
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.2</version>
    </dependency>

    <!-- JSON Mapping Framework -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.2.2</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.2</version>
    </dependency> …
Run Code Online (Sandbox Code Playgroud)

rest jax-rs java-ee cdi servlet-3.0

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

CDI injection is not working in Servlets

I really need your help. I'm working on this problem for weeks or months now. I apologize for the long post, but I want to explain the problem and my settings as precisely as possible.

My JAVA EE 6 web application mainly contains 3 Servlets: a javax.faces.webapp.FacesServlet, a StreamingServlet extending javax.servlet.http.HttpServlet and a ClientServlet extending org.eclipse.jetty.websocket.WebSocketServlet. I want to use CDI with these Servlets but it only works for the FacesServlet, to be more precisely, for the Beans …

servlets jetty java-ee cdi jboss-weld

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

生成serialVersionID而不是1L,2L的好处,

我与一位同事讨论了可序列化类的serialVersionUID:他总是从a开始,serialVersionUID = 1L然后在对类进行一些重大更改时将其递增1.

JDK类似乎总是在类中使用更复杂的生成的serialVersionUID java.lang.String:

private static final long serialVersionUID = -6849794470754667710L;
Run Code Online (Sandbox Code Playgroud)

现在,我的同事询问这种serialVersionUID与简单的serialVersionUID(如1L,2L,3L ......)相比的优势?

我们还搜索了SOF,但对给出的答案不是很满意,例如在为什么生成long serialVersionUID而不是简单的1L?.

在我看来,使用生成的ID的优点是它不需要任何有关"旧"类版本的信息.但是当手动递增ID时,您必须知道到目前为止的最高值.这可能听起来微不足道(通过查看当前的serialVersionUID并将其增加1),但在更大的软件项目,更大的开发团队或分布式环境中可能更复杂.

java serialization serialversionuid

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

使用jetty-maven-plugin时过滤web.xml?

原始帖子:
我想过滤在web.xmlJetty 8.1 servlet容器上运行的Java 6 Web应用程序的部署描述符,但到目前为止它不起作用.我想根据活动的maven配置文件设置一个不同的JSF项目阶段:

<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>${jsfProjectStage}</param-value>
</context-param>
Run Code Online (Sandbox Code Playgroud)

pom.xml看起来像这样的配置文件部分:

<profiles>
    <profile>
        <id>development</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <jsfProjectStage>Development</jsfProjectStage>
        </properties>
    </profile>
    <profile>
        <id>production</id>
        <properties>
            <jsfProjectStage>Production</jsfProjectStage>
        </properties>
    </profile>
</profiles>
Run Code Online (Sandbox Code Playgroud)

在互联网上,您可以找到几种方法来实现这一目标,例如使用备用web.xml文件.但对我来说,最明显的方式似乎是使用maven-war-plugin:

<build>
    <finalName>...</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <webResources>
                    <webResource>
                        <directory>src/main/webapp/WEB-INF</directory>
                        <filtering>true</filtering>
                        <targetPath>WEB-INF</targetPath>
                        <includes>
                            <include>web.xml</include>
                        </includes>
                    </webResource>
                </webResources>
            </configuration>
        </plugin>

        ...

    </plugins>
</build>
Run Code Online (Sandbox Code Playgroud)

由于您使用此代码段找到了许多答案或文章,因此我想知道为什么它对我不起作用.我试图替换<webResource><resource>(经常发现这样),我也尝试添加<filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>,但没有任何的工程.

有没有人有想法,这里缺少什么才能正确过滤web.xml?或者我必须明确指定<filters>

由于
塞巴斯蒂安

更新:
感谢user944849我现在知道我没有过滤的原因web.xml不是maven-war-plugin而是jetty-maven-plugin,因为我mvn jetty:run用来运行(未组装的)webapp.有没有人知道如何web.xml在运行未组装的webapp之前使用jetty-maven-plugin过滤?

web.xml java-ee maven maven-jetty-plugin

5
推荐指数
1
解决办法
3465
查看次数

WebSockets无法在Tomcat 7上运行

我在32位Windows XP以及64位Debian 6.0.5 Linux上安装了Apache Tomcat 7.0.29,并尝试使用websocket示例.但它们运行不正常.我甚至无法连接到服务器.

使用echo示例(选择消息API)并按下Connect按钮没有任何反应.但是在20秒后,日志文本区域中出现"WebSocket connection closed"消息.但正如其他文章所述,这是一个已知问题.

当使用自制的websocket应用程序并尝试连接到服务器时,我注意到打印了MessageInbound#onOpen方法的日志语句,因此调用了此方法.但是,浏览器Javascript部分中的onopen回调没有触发.但是在终止tomcat实例之后,会立即调用onopen客户端,然后紧跟onclose.

有没有人可以证实这个或类似的行为?或者有没有人在Tomcat 7上使用websocket示例?谢谢你的帮助.

更新:我为自己创建的示例应用程序添加了代码.

这是服务器部分,WebSocketTestServlet.java:

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;

import org.apache.catalina.websocket.MessageInbound;
import org.apache.catalina.websocket.StreamInbound;
import org.apache.catalina.websocket.WebSocketServlet;
import org.apache.catalina.websocket.WsOutbound;

@WebServlet(value = "/websocket", loadOnStartup = 1)
public class WebSocketTestServlet extends WebSocketServlet
{
    private static final long serialVersionUID = 1L;

    @Override
    protected StreamInbound createWebSocketInbound(String subProtocol, HttpServletRequest request)
    {
        return new WebSocketTestInbound();
    }

    public static final class WebSocketTestInbound extends MessageInbound
    {
        @Override
        protected void onOpen(WsOutbound outbound)
        {
            System.out.println("WebSocketTestServlet#onOpen");
        } …
Run Code Online (Sandbox Code Playgroud)

tomcat websocket tomcat7

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

Primefaces selectOneButton:通过JS/jquery设置选定的选项

是否可以在客户端更改JSF selectOneButton组件的选定项?Primefaces是否已经提供了一种方便的方法来执行此操作?

在组件上设置'widgetVar'属性时,相应的JS对象有一个方法'select(a)',它似乎停用以前激活的按钮,然后激活参数'a'给出的新按钮:

select: function(a)
{
    this.buttons.filter(".ui-state-active").removeClass("ui-state-active ui-state-hover").children(":radio").removeAttr("checked");
    a.addClass("ui-state-active").children(":radio").attr("checked", "checked").change()
}
Run Code Online (Sandbox Code Playgroud)

但我不知道该怎么设置参数'a'.这是正确的方法吗?

jquery jsf client-side primefaces

2
推荐指数
1
解决办法
1572
查看次数

Java 8:按人员状态过滤地图<Long,Person>到List <Long>

我正在尝试过滤a Map<Long, Person> people并仅返回状态为SUBSCRIBEDa 的这些人的ID List<Long>.以下是老式的代码:

public List<Long> getSubscribedPeople() {
    final List<Long> subscribedPeople = new ArrayList<>();

    for (final Map.Entry<Long, Person> entry : subscribedPeople.entrySet()) {
        if (entry.getValue().getStatus() == PersonStatus.SUBSCRIBED) {
            subscribedPeople.add(entry.getKey());
        }
    }

    return subscribedPeople;
}
Run Code Online (Sandbox Code Playgroud)

Person类看起来如下:

class Person {
    private Long id;
    private PersonStatus status;

    // getters and setters
}
Run Code Online (Sandbox Code Playgroud)

我试过以下,但这只给了我一个List<Entry<Long, Person>>:

public List<Long> getSubscribedPeople() {
    return people.entrySet()
                 .stream()
                 .filter(e -> e.getValue().getStatus() == PersonStatus.SUBSCRIBED)
                 .collect(Collectors.toList());
}
Run Code Online (Sandbox Code Playgroud)

我不得不以map某种方式到流?

java lambda java-8

2
推荐指数
1
解决办法
771
查看次数