小编Eng*_*_DJ的帖子

设置多个@ControllerAdvice @ExceptionHandlers的优先级

我有多个用类注释的类@ControllerAdvice,每个类都带有一个@ExceptionHandler方法.

一个处理Exception的目的是如果找不到更具体的处理程序,则应该使用它.

可悲的是,Spring MVC似乎总是使用最通用的case(Exception)而不是更具体的case(IOException例如).

这是人们期望Spring MVC的表现吗?我正在尝试模拟来自Jersey的模式,它评估每个ExceptionMapper(等效组件)以确定它处理的声明类型与已抛出的异常的距离,并始终使用最近的祖先.

exception-handling spring-mvc

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

从Jenkins的Maven POM获取项目版本

在处理POM之后,Jenkins构建是否有任何方法可以了解项目的Maven版本号?

我有一些项目,其中版本控制由Maven控制,在后期构建工作中,我们想创建一个Debian包并调用一些shell脚本.我需要的是Maven以前作为Jenkins环境变量可用的版本号,所以我可以将它传递给后期构建操作.

需要明确的是,我没有需要知道如何让詹金斯版本号传递到Maven的; 相反,我希望Maven将版本号传递给Jenkins!

maven jenkins

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

通过环境变量在Spring Boot中设置日志级别

是否可以在Spring Boot应用程序中单独通过环境变量设置日志记录级别?

我不想使用,application.properties因为我在Cloud Foundry上运行并希望在没有部署的情况下获取更改(但在应用程序重新启动后,或者重新开始更精确).

我已经尝试设置env vars,LOGGING_LEVEL_ORG_SPRINGFRAMEWORK=TRACE但这没有效果.把logging.level.org.springframework: TRACEapplication.properties做的工作,虽然.

java spring spring-boot

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

Spring @Async不工作

一个@Async在方法@Service-annotated类不被异步调用-它阻塞线程.

我已经进入<task: annotation-driven />了我的配置,并且对该方法的调用来自于类外部,因此代理应该被命中.当我单步执行代码时,代理确实被命中,但它似乎没有任何与任务执行器中运行相关的任何类.

我已经把断点放进去AsyncExecutionInterceptor,他们永远不会受到打击.我调试过AsyncAnnotationBeanPostProcessor,可以看到建议应用.

该服务被定义为一个接口(该方法@Async在那里注释以获得良好的衡量标准),实现的方法也被注释@Async.两者都没有标记@Transactional.

什么想法可能出错了?

- = UPDATE = -

奇怪的是,它只有task在我的app-servlet.xml文件中有我的XML元素时才有效,而不是在我的app-services.xml文件中,如果我也从那里对服务进行组件扫描.通常我有一个只包含控制器的XML文件(并相应地限制组件扫描),另一个包含服务(同样限制了组件扫描,使得它不会重新扫描另一个加载的控制器)文件).

APP-servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:jee="http://www.springframework.org/schema/jee" 
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:webflow="http://www.springframework.org/schema/webflow-config" 
xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/task
    http://www.springframework.org/schema/task/spring-task-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/jee 
    http://www.springframework.org/schema/jee/spring-jee-3.0.xsd"
>
<task:annotation-driven executor="executor" />
<task:executor id="executor" pool-size="7"/>

<!-- Enable controller annotations -->
<context:component-scan base-package="com.package.store">
    <!-- <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> -->
</context:component-scan>

<tx:annotation-driven/>
<bean id="transactionManager" …
Run Code Online (Sandbox Code Playgroud)

java spring asynchronous

44
推荐指数
6
解决办法
7万
查看次数

杰克逊的@JsonView,@ JsonFilter和Spring

可以使用Jackson @JsonView@JsonFilter注释来修改Spring MVC控制器返回的JSON,同时使用MappingJacksonHttpMessageConverterSpring @ResponseBody@RequestBody注释吗?

public class Product
{
    private Integer id;
    private Set<ProductDescription> descriptions;
    private BigDecimal price;
    ...
}


public class ProductDescription
{
    private Integer id;
    private Language language;
    private String name;
    private String summary;
    private String lifeStory;
    ...
}
Run Code Online (Sandbox Code Playgroud)

当客户端请求收集时Products,我想返回每个的最小版本ProductDescription,也许只是它的ID.然后在后续调用中,客户端可以使用此ID来请求ProductDescription包含所有属性的完整实例.

能够在Spring MVC控制器方法上指定它是理想的,因为调用的方法定义了客户端请求数据的上下文.

java spring json spring-mvc jackson

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

Jackson 2.0与Spring 3.1

Spring MVC 3.1与Jackson 2.0兼容吗?Spring MVC是否会在类路径上自动检测Jackson,并且使用JSON内容类型的请求委托给Jackson仍有效?

java spring json spring-mvc jackson

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

ZooKeeper for Java/Spring Config?

是否有任何记录良好的Apache ZooKeeper用例用于分发Java应用程序的配置,特别是Spring服务?

像许多云服务用户一样,我需要更改可变数量的Java服务的配置,最好是在运行时,而不需要重新启动服务.

UPDATE

最后,我最终写了一些东西,将ZooKeeper节点作为属性文件加载,并创建一个ResourcePropertySource并将其插入到Spring上下文中.请注意,这不会反映上下文启动后ZooKeeper节点的更改.

public class ZooKeeperPropertiesApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    private static final Logger logger = LoggerFactory.getLogger(ZooKeeperPropertiesApplicationContextInitializer.class);

    private final CuratorFramework curator;
    private String projectName;
    private String projectVersion;

    public ZooKeeperPropertiesApplicationContextInitializer() throws IOException {
        logger.trace("Attempting to construct CuratorFramework instance");

        RetryPolicy retryPolicy = new ExponentialBackoffRetry(10, 100);
        curator = CuratorFrameworkFactory.newClient("zookeeper", retryPolicy);
        curator.start();
    }

    /**
     * Add a primary property source to the application context, populated from
     * a pre-existing ZooKeeper node.
     */
    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        logger.trace("Attempting to add …
Run Code Online (Sandbox Code Playgroud)

java service configuration spring apache-zookeeper

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

JPA的Write-Behind Cache

从这里和整个网络搜索看来,不可能将EHCache实现为Hibernate的后写缓存,因为这需要对Hibernate代码进行大量更改.

JPA提供程序是否有任何其他解决方案(最好是开源)可以"透明地"挂钩到后写缓存实现,最好是可以像Terracotta一样分发的解决方案?

我已经读过EclipseLink和Oracle Coherence可以实现这一点,但Coherence可悲的不是一个廉价的解决方案!

java hibernate terracotta jpa ehcache

15
推荐指数
1
解决办法
2052
查看次数

从Eclipse运行Spring JUnit测试时,将/ src/main/resources添加到Classpath

有没有办法在Eclipse中使用JUnit测试(特别是我正在使用SpringJUnit4ClassRunner)来使用src/main/resources以及来自的资源src/test/resources

Maven的surefire插件可以做到这一点,但是从Eclipse运行一个特定的单元测试却没有.

我有一个Spring配置加载,src/main/resources/spring-config/我想'覆盖'两个特定的文件.我已经放置了这些特定于测试的覆盖src/test/resources/spring-config/,并且当通过Maven eveerything运行单元测试时.当我从Eclipse运行JUnit测试时,只有测试文件可用,并且不出所料我的上下文无法加载.

更新

看来,配置测试上下文的Spring类src/main/resources/在使用通配符时无法找到资源,但如果我明确指定它们,则会找到它们.

找不到东西 src/main/resources/

@ContextConfiguration(locations = {"classpath:/spring-config/*.xml"})
Run Code Online (Sandbox Code Playgroud)

是否找到指定的文件src/main/resources/

@ContextConfiguration(locations = {"classpath:/spring-config/*.xml",
                                   "classpath:/spring-config/app-aws.xml"})
Run Code Online (Sandbox Code Playgroud)

eclipse junit spring unit-testing maven

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

使用Jackson从JSON树中的特定节点映射对象

是不是ObjectMapper只能从JSON树中的特定节点(和"向下")取消杰克逊的解组?

用例是可扩展的文档格式.我想要遍历树,然后将当前路径发布到可扩展的插件集,以查看用户是否正在使用以及知道如何处理文档的该部分的插件.

我希望插件作者不必处理低级细节JsonNode或流API; 相反,只是传递一些上下文和一个特定的JsonNode,然后能够使用可爱和方便ObjectMapper的解组他们的类的实例,考虑作为树的根传递的节点.

java json jackson

15
推荐指数
1
解决办法
3685
查看次数