小编Mar*_*ijk的帖子

application.yml是否支持环境变量?

我尝试在我的application.yml configration中使用env变量,如:

spring:
  main:
    show_banner: false

---

spring:
  profiles: production
server:
  address: $OPENSHIFT_DIY_IP
  port: $OPENSHIFT_DIY_PORT
Run Code Online (Sandbox Code Playgroud)

但是env变量没有解决.我必须提供不同的符号吗?

在Rails中,您可以使用<%= ENV ['FOOVAR']%>

唯一的选择是运行应用程序,如:

java -jar my.jar --server.address=$OPENSHIFT_DIY_IP --server.port=$OPENSHIFT_DIY_PORT
Run Code Online (Sandbox Code Playgroud)

java spring yaml spring-boot

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

如何访问Spring休息控制器中的普通json主体?

拥有以下代码:

@RequestMapping(value = "/greeting", method = POST, consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
@ResponseBody
public String greetingJson(@RequestBody String json) {
    System.out.println("json = " + json); // TODO json is null... how to retrieve plain json body?
    return "Hello World!";
}
Run Code Online (Sandbox Code Playgroud)

尽管json在正文中发送,但String json参数始终为null.

请注意,我不想要自动类型转换,我只想要普通的json结果.

这例如有效:

@RequestMapping(value = "/greeting", method = POST, consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
@ResponseBody
public String greetingJson(@RequestBody User user) {
    return String.format("Hello %s!", user);
}
Run Code Online (Sandbox Code Playgroud)

可能我可以使用ServletRequest或InputStream作为参数来检索实际的身体,但我想知道是否有更简单的方法?

rest spring controller spring-mvc

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

如何在Spring Boot中使用Spring管理的Hibernate拦截器?

是否有可能在Spring Boot中集成Spring管理的Hibernate拦截器(http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch14.html)?

我正在使用Spring Data JPA和Spring Data REST,并且需要一个Hibernate拦截器来对实体上的特定字段进行更新.

使用标准JPA事件,不可能获得旧值,因此我认为我需要使用Hibernate拦截器.

hibernate spring-data spring-data-jpa spring-data-rest spring-boot

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

Thymeleaf不显示Spring表单错误消息

我正在将Spring jsp应用程序迁移到Thymeleaf但是在显示表单错误时遇到问题.

我正在使用SpringTemplateEngine和ThymeleafViewResolver并且模板的渲染工作.表单值也在表单输入字段中填充.

到目前为止唯一不起作用的是显示表单错误消息.

我的控制器看起来像:

@RequestMapping(method = RequestMethod.POST)
String save(@Valid CustomerForm form, BindingResult bindingResult, Model model, RedirectAttributes redirectAttributes) {
    if (bindingResult.hasErrors()) {
        model.addAttribute("form", form)
        return "app/customers/create"
    }
    ....
Run Code Online (Sandbox Code Playgroud)

我打印了bindingResult以验证它是否包含错误:

binding result = org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'customerForm' on field 'name': rejected value []; codes [customerForm.name.NotBlank,name.NotBlank,java.lang.String.NotBlank,NotBlank]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [customerForm.name,name]; arguments []; default message [name]]; default message [may not be empty]
Run Code Online (Sandbox Code Playgroud)

当我尝试使用以下方法显示错误:

<ul>
    <li th:each="e : ${#fields.detailedErrors()}" th:class="${e.global}? globalerr : fielderr">
        <span th:text="${e.global}? '*' : ${e.fieldName}">The field name</span> …
Run Code Online (Sandbox Code Playgroud)

spring thymeleaf

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

如何在@HandleBeforeSave事件中获取旧实体值以确定属性是否已更改?

我正试图在一个@HandleBeforeSave事件中得到旧实体.

@Component
@RepositoryEventHandler(Customer.class)
public class CustomerEventHandler {

    private CustomerRepository customerRepository;

    @Autowired
    public CustomerEventHandler(CustomerRepository customerRepository) {
        this.customerRepository = customerRepository;
    }

    @HandleBeforeSave
    public void handleBeforeSave(Customer customer) {
        System.out.println("handleBeforeSave :: customer.id = " + customer.getId());
        System.out.println("handleBeforeSave :: new customer.name = " + customer.getName());

        Customer old = customerRepository.findOne(customer.getId());
        System.out.println("handleBeforeSave :: new customer.name = " + customer.getName());
        System.out.println("handleBeforeSave :: old customer.name = " + old.getName());
    }
}
Run Code Online (Sandbox Code Playgroud)

如果我尝试使用该findOne方法获取旧实体,但这将返回新事件.可能是因为当前会话中的Hibernate/Repository缓存.

有没有办法获得旧实体?

我需要这个来确定给定的属性是否被更改.如果属性是更改,我需要执行一些操作.

java hibernate spring-data-rest

22
推荐指数
3
解决办法
9465
查看次数

Intellij"自动制作项目"的困境

回到12月,我从Eclipse切换到IntelliJ,但在几天之内我又转回了.

今天我再次尝试了IntelliJ,基本上我只剩下一个问题了.

问题是我想在保存时编译Java类.原因是我正在使用JRebel来获取新类.我不想每次都按Command-F9编译更改的类.

我读到了关于EclipseModel插件的内容,但这似乎不再起作用,因为可以在插件页面的注释中阅读:http://plugins.jetbrains.com/plugin/?id = 3822

我还尝试了"自动生成项目",但这会编译所有类,而不仅仅是更改的类.这只是耗费时间,它也会导致JRebel重新加载所有类...

有什么建议?

java eclipse intellij-idea jrebel

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

更改Spring Data findAll()方法的默认排序顺序

我正在使用Spring Data JPA,我想知道是否可以更改Spring Data findAll()方法使用的实体的默认排序顺序?

spring jpa spring-data spring-data-jpa

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

JSR-353如何使用javax.json.JsonObjectBuilder添加空值

由于javax.json文档建议创建JsonObject的方法是使用提供的构建器,如:

JsonBuilderFactory factory = Json.createBuilderFactory(config);
 JsonObject value = factory.createObjectBuilder()
     .add("firstName", "John")
     .add("lastName", "Smith")
     .add("age", 25)
     .add("address", factory.createObjectBuilder()
         .add("streetAddress", "21 2nd Street")
         .add("city", "New York")
         .add("state", "NY")
         .add("postalCode", "10021"))
     .add("phoneNumber", factory.createArrayBuilder()
         .add(factory.createObjectBuilder()
             .add("type", "home")
             .add("number", "212 555-1234"))
         .add(factory.createObjectBuilder()
             .add("type", "fax")
             .add("number", "646 555-4567")))
     .build();
Run Code Online (Sandbox Code Playgroud)

此示例添加了给定键的值.在现实生活中,这些值可能来自某些(pojo)域对象,如:

JsonBuilderFactory factory = Json.createBuilderFactory(config);
 JsonObject value = factory.createObjectBuilder()
     .add("firstname", customer.getFirstame())
     .add("lastname", customer.getLastame())
     .add("age", customer.getAge())
     ....
Run Code Online (Sandbox Code Playgroud)

如果key或value为null,则JsonOBjectBuilder抛出NPE.如果客户没有注册年龄,则上述代码将抛出NPE.

因此,基本上对于我添加的每个字段,我必须检查值是否为null并添加实际值,否则不添加字段或为密钥添加JsonValue.NULL.这导致很多(不希望的)样板...

在我的情况下,我最终得到了自定义JsonUtils类,包括各种静态方法,如:

public static void add(JsonObjectBuilder builder, String name, Long value) {
    if (value == null) {
        builder.add(name,  JsonValue.NULL);
    } …
Run Code Online (Sandbox Code Playgroud)

java json glassfish java-ee jsr-353

16
推荐指数
1
解决办法
4829
查看次数

配置执行器端点安全性

Spring Boot Actuator端点默认受基本http安全保护.

可以改为使用Spring Security吗?我已成功设置Spring Security并使用它来保护我的其他页面.

我尝试security.basic.enabled: false并添加.antMatchers("/manage/**").hasRole("ADMIN")了我的授权请求(注意我使用不同的URL作为端点的根)但这没有帮助.我一直在获得一个基本的http auth日志,它不是在AuthenticationManager中配置的用户.

任何的想法?

编辑 - 提供更多细节 -

我的Application.java看起来像:

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application extends WebMvcConfigurerAdapter {


    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/app").setViewName("app/index");
        registry.addViewController("/app/login").setViewName("app/login");
    }

    @Bean
    public ApplicationSecurity applicationSecurity() {
        return new ApplicationSecurity();
    }

    @Order(Ordered.LOWEST_PRECEDENCE - 8)
    protected static class ApplicationSecurity extends WebSecurityConfigurerAdapter {

        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            // @formatter:off
            auth.inMemoryAuthentication()
                .withUser("test1")
                    .password("test1pw")
                    .roles("USER", "ADMIN") …
Run Code Online (Sandbox Code Playgroud)

spring-security spring-boot

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

JSR 303 Bean验证可以与Spring Data Rest一起使用吗?

我从文档了解http://docs.spring.io/spring-data/rest/docs/2.1.2.RELEASE/reference/html/validation-chapter.html,我可以与某些前缀声明验证.

我正在使用JSR 303,因此我的域实体使用验证注释进行注释.

可以 - 如果是,如何 - 我使用JSR 303 Bean验证与Spring Data Rest?

PS:我正在使用Spring Boot

java spring-data-jpa spring-data-rest spring-boot

12
推荐指数
2
解决办法
5797
查看次数