标签: spring-annotations

动态更改注释驱动的Spring MVC中的@ResponseStatus

我真的不确定使用Spring 3.2 MVC是否可行.

我的控制器有一个声明如下的方法:

@RequestMapping(method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public @ResponseBody List<Foo> getAll(){
    return service.getAll();
}
Run Code Online (Sandbox Code Playgroud)

问题:

  1. 是什么意思@ResponseStatus(HttpStatus.OK)
  2. 它是否表示该方法将始终返回HttpStatus.OK状态代码.
  3. 如果从服务层抛出异常怎么办?
  4. 我可以在发生任何异常时更改响应状态吗?
  5. 如何根据同一方法中的条件处理多个响应状态?

spring-mvc spring-annotations

32
推荐指数
3
解决办法
5万
查看次数

在多个模块中使用Spring缓存注释

我有一个util模块,可以生成一个可以在其他应用程序中使用的jar.我希望这个模块使用缓存,并且更喜欢使用Spring的annotation-driven缓存.

所以Util-Module会有这样的事情:


DataManager.java

...
@Cacheable(cacheName="getDataCache")
public DataObject getData(String key) { ... }
...
Run Code Online (Sandbox Code Playgroud)

数据管理器 - ehcache.xml中

...
<cache name="getDataCache" maxElementsInMemory="100" eternal="true" />
...
Run Code Online (Sandbox Code Playgroud)

数据管理器弹簧-config.xml中

...
<cache:annotation-driven cache-manager="data-manager-cacheManager" />
<!-- ???? --->
<bean id="data-manager-cacheManager" 
    class="org.springframework.cache.ehcache.EhcacheCacheManager" 
    p:cache-manager="data-manager-ehcache"/>
<bean id="data-manager-ehcache" 
    class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" 
    p:config-location="data-manager-ehcache.xml"/>
...
Run Code Online (Sandbox Code Playgroud)

我还希望我的可部署单元通过Spring注释进行缓存,同时将上面的jar包含为依赖项.所以我Deployable-Unit会有这样的事情:


MyApp.java

...
@Cacheable(cacheName="getMyAppObjectCache")
public MyAppObject getMyAppObject(String key) { ... }
...
Run Code Online (Sandbox Code Playgroud)

我-APP-ehcache.xml中

...
<cache name="getMyAppObjectCache" maxElementsInMemory="100" eternal="true" />
...
Run Code Online (Sandbox Code Playgroud)

我的应用程序内弹簧-config.xml中

...
<cache:annotation-driven cache-manager="my-app-cacheManager" />
<!-- ???? --->
<bean id="my-app-cacheManager" 
    class="org.springframework.cache.ehcache.EhcacheCacheManager" 
    p:cache-manager="my-app-ehcache"/>
<bean id="my-app-ehcache" …
Run Code Online (Sandbox Code Playgroud)

spring caching annotations ehcache spring-annotations

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

如何使用注释执行Spring Lookup方法注入?

有没有办法使用注释使用查找方法注入?

鉴于以下课程:

@Service
public abstract class A {


    protected abstract createB();

}
Run Code Online (Sandbox Code Playgroud)

为了使它工作,我必须在spring applicationContext.xml中声明以下内容:

<bean id="b" class="com.xyz.B">
</bean>

<bean id="a" class="com.xyz.A">
    <lookup-method name="createB" bean="b"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

即使我正在使用,<context:component-scan base>我也必须在XML中声明它.我认为这不是一个好方法.

如何用注释做到这一点?

spring dependency-injection spring-annotations

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

Spring Boot @Value属性

我有一个Spring Boot应用程序,在其中一个类中,我尝试使用application.properties文件引用属性@Value.但是,该财产没有得到解决.我查看了类似的帖子,并尝试按照建议,但这没有帮助.这堂课是:

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class PrintProperty {

  @Value("${file.directory}")
  private String fileDirectory;

  public void print() {
    System.out.println(fileDirectory);
  }
}
Run Code Online (Sandbox Code Playgroud)

我有属性file.directory application.properties.我也有其他领域.

spring spring-annotations spring-boot

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

AbstractWizardFormController使用Annotated @Controllers

在Spring Framework中,AbstractWizardFormController似乎已被弃用.如何在Spring MVC Framework中实现多个页面表单.(我不使用webflow)

任何示例或指针都有助于考虑我在Spring中的有限知识.

java spring spring-mvc spring-annotations

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

Spring 4 - addResourceHandlers无法解析静态资源

我的maven spring项目目录结构如下所示.我正在使用基于Spring-4注释的配置.我配置如下的资源.我尝试了许多Stackoverflow问题和其他网站中提出的方法

Spring 4加载静态资源

http://imwill.com/spring-mvc-4-add-static-resources-by-annotation/#.U5GZlXKs9i4

但是jsp文件无法加载资源,所有静态内容请求都返回404错误.我在jsp中试过这些东西,

 <link href="resources/css/bootstrap.css" rel="stylesheet" media="screen">
 <link href="/resources/css/bootstrap.css" rel="stylesheet" media="screen">
 <link href="css/bootstrap.css" rel="stylesheet" media="screen">
Run Code Online (Sandbox Code Playgroud)

编辑:我正在使用servlet 2.5,因为到目前为止我无法将我的项目从JBoss 5升级到更高版本.JBoss5不支持servlet 3,那重要吗?

@Configuration
@ComponentScan("com.mgage.mvoice")
public class MyAppWebConfig extends WebMvcConfigurerAdapter {
     public void addResourceHandlers(ResourceHandlerRegistry registry) {  
        // I tried these many combinations separately.

        ResourceHandlerRegistration resourceRegistration = registry
            .addResourceHandler("resources/**");
        resourceRegistration.addResourceLocations("/resources/**");
        registry.addResourceHandler("/css/**").addResourceLocations("/css/**");
        registry.addResourceHandler("/img/**").addResourceLocations("/img/**");
        registry.addResourceHandler("/js/**").addResourceLocations("/js/**");
        registry.addResourceHandler("/resources/**")
                .addResourceLocations("classpath:/resources/"); 
              // do the classpath works with the directory under webapp?
     }

}
Run Code Online (Sandbox Code Playgroud)

项目结构

java spring spring-mvc spring-annotations spring-4

29
推荐指数
3
解决办法
8万
查看次数

如何使用注释将bean指定为非延迟

有没有人知道如何在使用注释配置bean时将bean指定为非延迟?

spring annotations spring-annotations

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

如果找不到,@ PathVariable可以返回null吗?

@PathVariable如果路径变量不在url中,是否可以使返回null?否则我需要做两个处理程序.一个用于/simple另一个/simple/{game},但两者都做同样的事情,如果没有定义游戏我从列表中选择第一个然而如果有游戏参数定义然后我使用它.

@RequestMapping(value = {"/simple", "/simple/{game}"}, method = RequestMethod.GET)
public ModelAndView gameHandler(@PathVariable("example") String example,
            HttpServletRequest request) {
Run Code Online (Sandbox Code Playgroud)

这是我在尝试打开页面时得到的/simple:

引起:java.lang.IllegalStateException:在@RequestMapping中找不到@PathVariable [example]

java path-variables spring-annotations spring-3

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

带有2个事务管理器的@EnableTransactionManagement注释

我正在使用@Configuration注释来配置spring而不是xml文件.我正在配置具有不同会话工厂和不同事务管理器的2个数据源.我在这里遇到了一个@EnableTransactionManagement注释问题.我在其文档中读到,

@EnableTransactionManagement更灵活; 它将回退到PlatformTransactionManager容器中任何bean 的类型查找.因此,名称可以是"txManager","transactionManager"或"tm":它无关紧要.

这意味着我给方法的任何名称,它总是会搜索返回PlatformTransactionManager对象的方法,而我有2个transactionmanagers.现在问题是,当我测试这个类时,它给了我错误:

org.springframework.beans.factory.NoSuchBeanDefinitionException:没有org.springframework.transaction.PlatformTransactionManager定义类型为[ ]的唯一bean :期望的单个bean但找到2

我甚至试图拥有2个不同的配置类但是徒劳无功.在xml配置中,情况并非如此.我用两个<tx:annotation-driven transaction-manager="" />标签注册了我的两个交易管理器,它运行正常.但是这里不能用注释做同样的事情.

如果我想在Spring注释配置类中配置2个具有2个不同事务管理器的数据源,我该怎么办?

java spring spring-annotations spring-3

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

如何从BindingResult获取控制器中的错误文本

我有一个返回JSON的控制器.它采用一种形式,通过弹簧注释验证自己.我可以从BindingResult获取FieldError列表,但它们不包含JSP将在标记中显示的文本.如何将错误文本发送回JSON?

@RequestMapping(method = RequestMethod.POST)
public
@ResponseBody
JSONResponse submit(@Valid AnswerForm answerForm, BindingResult result, Model model, HttpServletRequest request, HttpServletResponse response) {

    if (result.hasErrors()) {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        JSONResponse r = new JSONResponse();
        r.setStatus(JSONResponseStatus.ERROR);
        //HOW DO I GET ERROR MESSAGES OUT OF BindingResult??? 
    } else {
        JSONResponse r = new JSONResponse();
        r.setStatus(JSONResponseStatus.OK);
        return r;
    }

}
Run Code Online (Sandbox Code Playgroud)

JSONREsponse类只是一个POJO

public class JSONResponse implements Serializable {
    private JSONResponseStatus status;
    private String error;
    private Map<String,String> errors;
    private Map<String,Object> data;

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

调用BindingResult.getAllErrors()返回FieldError对象的数组,但它没有实际的错误.

java spring json spring-mvc spring-annotations

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