Spring

spring @valid 注解

无人久伴 提交于 2021-02-09 06:05:36
用于验证注解是否符合要求,直接加在变量之前,在变量中添加验证信息的要求,当不符合要求时就会在方法中返回message 的错误提示信息。 @PostMapping public User create (@Valid @RequestBody User user) { @NotBlank(message = "密码不能为空" ) private String password; @NotBlank 注解所指的 password 字段,表示验证密码不能为空,如果为空的话,create 方法会将message 中的"密码不能为空"返回。 注解说明 @Null 限制只能为null @NotNull 限制必须不为null @AssertFalse 限制必须为false @AssertTrue 限制必须为true @DecimalMax(value) 限制必须为一个不大于指定值的数字 @DecimalMin(value) 限制必须为一个不小于指定值的数字 @Digits(integer,fraction) 限制必须为一个小数,且整数部分的位数不能超过integer,小数部分的位数不能超过fraction @Future 限制必须是一个将来的日期 @Max(value) 限制必须为一个不大于指定值的数字 @Min(value) 限制必须为一个不小于指定值的数字 @Past

Validate UUID Restful service

≡放荡痞女 提交于 2021-02-08 20:54:09
问题 I have a RESTful service which receives POST request with UUID values and writes them in DB. So the problem is to validate if UUID is valid or not. For this purpose I implemented custom annotation: @Constraint(validatedBy = {}) @Target({ElementType.FIELD}) @Retention(RUNTIME) @Pattern(regexp = "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[34][0-9a-fA-F]{3}-[89ab][0-9a-fA-F]{3}-[0-9a-fA-F]{12}") public @interface validUuid { String message() default "{invalid.uuid}"; Class<?>[] groups() default {}; Class<?

Spring: Why is @PreDestroy not called at end of each test class?

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-08 20:52:06
问题 I have an integration test class annotated as follows @WebAppConfiguration @ContextConfiguration(classes = {AppConfiguration.class}) @RunWith(SpringJUnit4ClassRunner.class) public class CacheConsumerTest { } Here's my AppConfiguration @Configuration @ComponentScan(basePackages = {"com.etc.etc.etc."}) @EnableWebMvc public class AppConfiguration { } For some reason, none of my @Component beans' @PreDestroy is getting called at the end of all tests in CacheConsumerTest . @PostConstruct is

Liquibase migration on startup doesnt work using Spring-Boot

半城伤御伤魂 提交于 2021-02-08 20:00:08
问题 Following this documentation: To automatically run Liquibase database migrations on startup, add the org.liquibase:liquibase-core to your classpath. The master change log is by default read from db/changelog/db.changelog-master.yaml but can be set using liquibase.change-log. In addition to YAML, Liquibase also supports JSON, XML, and SQL change log formats. I configured my application to use liquibase migrations. I added liquibase.change-log property so my whole application.properties file

Spring boot - setting default Content-type header if it's not present in request

情到浓时终转凉″ 提交于 2021-02-08 16:59:13
问题 I'm having the following problem: suppose I sometimes receive POST requests with no Content-type header set. In this case I want to assume that Content-type=application/json by default. Can I achieve this somehow using spring boot features and not using filters? Thanks 回答1: As of Spring Boot 2.x, you need to create a class that extends the WebMvcConfigurer interface, e.g.: @Configuration class WebMvcConfiguration implements WebMvcConfigurer { @Override public void configureContentNegotiation(

Forbid Unauthenticated requests in Spring Cloud Gateway

三世轮回 提交于 2021-02-08 16:37:34
问题 I have implemented custom pre filter in spring cloud gateway which allows authenticated requests to go through the downstream process. What I want is if the request is unauthenticated then return with response of 401 UNAUTHORIZE status and stop the downstream processing. Can I achieve this spring cloud gateway. Please help. My filter code is below public class ValidUserFilter implements GatewayFilterFactory { @Override public GatewayFilter apply(Object config) { return (exchange, chain) -> {

Getting details of binding errors for a JSON form in Spring MVC

北城余情 提交于 2021-02-08 15:57:18
问题 I use Spring MVC 3.2.4. I have a controller method that receives data from an HTML form. The method has the following signature: @RequestMapping(...) public String updateProduct(@Valid Product product, BindingResult bindingResult) In the method, I can get information about both data binding (e.g., a non-integer value in an integer field) and validation (JSR-303) errors in the following way: if (bindingResult.hasErrors()) { List<FieldError> errors = bindingResult.getFieldErrors(); for

Injecting object into Spring Configuration

梦想与她 提交于 2021-02-08 15:38:26
问题 I am turning old xml/java configuration into pure java config. In xml I used injection of parameters into configuration file like this: <bean class="com.project.SpringRestConfiguration"> <property name="parameters" ref="parameters" /> </bean> @Configuration public class SpringRestConfiguration { private Parameters parameters; public void setParameters(Parameters parameters) { this.parameters = parameters; } // @Bean definitions ... } Is it possible to inject Parameters in javaconfig? (Without

JUnit test in Spring - overriding and ignoring beans from application other configuration classes

拜拜、爱过 提交于 2021-02-08 15:29:07
问题 We have large application written in Spring 3. I need to write JUnit test checking behavior of some service. It is not a unit but part of a system. There are some services and repositories working together insite it -> lot of injected beans inside. The app also uses aspects. My question is. How to manage config and beans in this case of tests? I need to use beans defined in app configes and in tests only redefine beans using persistence to work with a embedded db. So I need to use beans from