Spring

JPA use Spring Data Specification for delete and update

社会主义新天地 提交于 2021-02-09 11:33:34
问题 JPA Specification is powerful in order to build WHERE clauses. But, it seems to be defined only for SELECT (with findAll method). Is it possible to have the same behavior with DELETE and UPDATE? So that, it would be possible to avoid selecting scope before deleting or updating it. Thx 回答1: You can use CriteriaUpdate and CriteriaDelete. Im building my specification from a RSQL query here but it can be done in a lot of other ways as well (see https://www.programcreek.com/java-api-examples/index

Spring Controller: RequestParam not validated despite @Valid and @Size

旧街凉风 提交于 2021-02-09 11:14:27
问题 I have a simple Controller method : @GetMapping("/search") public List<Result> search(@RequestParam @Valid @NotNull @Size(min = 4) String query) { return searchService.search(query); } When I omit the "query" parameter, I get a 400 Bad Request, as expected. Testing the method with these query parameters doesn't work. All but the last test should return a "400 Bad Request". "/search" --> actual 400 Bad Request, test passes "/search?query=" --> actual 200 Ok, expected 400 because @Size(min=4) "

Spring Controller: RequestParam not validated despite @Valid and @Size

我与影子孤独终老i 提交于 2021-02-09 11:14:26
问题 I have a simple Controller method : @GetMapping("/search") public List<Result> search(@RequestParam @Valid @NotNull @Size(min = 4) String query) { return searchService.search(query); } When I omit the "query" parameter, I get a 400 Bad Request, as expected. Testing the method with these query parameters doesn't work. All but the last test should return a "400 Bad Request". "/search" --> actual 400 Bad Request, test passes "/search?query=" --> actual 200 Ok, expected 400 because @Size(min=4) "

Spring-boot: register mongodb custom converter

ぃ、小莉子 提交于 2021-02-09 11:12:09
问题 I'm using this dependency into my spring bott service: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> I've created a custom converter: import org.springframework.core.convert.converter.Converter; @Component public class ReferenceWriterConverter implements Converter<Reference, DBObject> { @Override public DBObject convert(Reference reference) { DBObject dbObject = new BasicDBObject(); //... return dbObject; } } I

Test annotated with @DataJpaTest not autowiring field annotated with @Autowired

无人久伴 提交于 2021-02-09 11:11:58
问题 I have a Spring Boot app which contains a Spring Data Jpa repository. I need to run a unit (or component?) test around this repository. I do not have a lot of experience with Spring Data Jpa. Here's my test. It's trivially simple, and I cannot get it to pass. import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import static org.junit.Assert.assertNotNull; @DataJpaTest public class

Invalid flag -parameters in java 1.7

梦想的初衷 提交于 2021-02-09 11:11:58
问题 I have the task to create the spring-boot application using Java 7. So, as usual, I created a template on start.spring.io resource and open him via File -> New -> Project from Existing Sources... When I run with jdk-8 , everything works fine, but when I change JDK to version 1.7 (also I change java-version in pom.xml ) I get a compilation error: Error:java: invalid flag: -parameters Screenshot: Pom.xml: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"

springmvc异常统一处理(一)

空扰寡人 提交于 2021-02-09 11:11:18
目录 使用 @ ExceptionHandler 注解 实现 HandlerExceptionResolver 接口 使用 @ControllerAdvice+ @ ExceptionHandler 注解 参考资料 正文 Spring 统一异常处理有 3 种方式,分别为: 使用 @ ExceptionHandler 注解 实现 HandlerExceptionResolver 接口 使用 @controlleradvice 注解 使用 @ ExceptionHandler 注解 使用该注解有一个不好的地方就是:进行异常处理的方法必须与出错的方法在同一个Controller里面。使用如下: 1 @Controller 2 public class GlobalController { 3 4 /** 5 * 用于处理异常的 6 * @return 7 */ 8 @ExceptionHandler({MyException.class}) 9 public String exception(MyException e) { 10 System.out.println(e.getMessage()); 11 e.printStackTrace(); 12 return "exception"; 13 } 14 15 @RequestMapping("test") 16 public

Can spring boot controller receive plain/text?

断了今生、忘了曾经 提交于 2021-02-09 11:10:07
问题 I am trying to process a POST request with body of plain text (utf-8) but it seems that spring does not like the plain text nature of the call. Could it be that it is not supported - or otherwise, am I coding it wrong? @RestController @RequestMapping(path = "/abc", method = RequestMethod.POST) public class NlpController { @PostMapping(path= "/def", consumes = "text/plain; charset: utf-8", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<Object> doSomething(@RequestBody

Can spring boot controller receive plain/text?

回眸只為那壹抹淺笑 提交于 2021-02-09 11:09:59
问题 I am trying to process a POST request with body of plain text (utf-8) but it seems that spring does not like the plain text nature of the call. Could it be that it is not supported - or otherwise, am I coding it wrong? @RestController @RequestMapping(path = "/abc", method = RequestMethod.POST) public class NlpController { @PostMapping(path= "/def", consumes = "text/plain; charset: utf-8", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<Object> doSomething(@RequestBody

Can spring boot controller receive plain/text?

独自空忆成欢 提交于 2021-02-09 11:08:49
问题 I am trying to process a POST request with body of plain text (utf-8) but it seems that spring does not like the plain text nature of the call. Could it be that it is not supported - or otherwise, am I coding it wrong? @RestController @RequestMapping(path = "/abc", method = RequestMethod.POST) public class NlpController { @PostMapping(path= "/def", consumes = "text/plain; charset: utf-8", produces = MediaType.APPLICATION_JSON_VALUE) public ResponseEntity<Object> doSomething(@RequestBody