spring-boot

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

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

Spring-boot: register mongodb custom converter

烈酒焚心 提交于 2021-02-09 11:08:35
问题 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

Spring Boot - Enable SSL (HTTPS) with CA certificate

亡梦爱人 提交于 2021-02-09 10:55:45
问题 Maybe I will find help here. I want to enable SSL on Spring Boot application. I have a configuration like: server: port: 8999 ssl: enabled: true key-store: classpath:keystore.jks key-store-password: mypass key-password: mypass The problem is my keystore. I've imported *crt file into keystore with alias 'tomcat': keytool -importcert -file certificate.crt -keystore native.jks -alias tomcat However, I still can't properly access my rest api. It vomits with error in firefox: SSL_ERROR_RX_RECORD

Spring Security returns 403 with valid JWT

点点圈 提交于 2021-02-09 08:10:39
问题 I'm using Postman to test a simple OAuth2 API I'm creating in Spring Boot 2.2.6 with Spring Security. I successfully receive a JWT when requesting new user credentials, but all of my endpoints return a 403 Forbidden error when I attempt to access them with this token in my headers. My classes are as follows: My server security configuration: @Configuration @EnableWebSecurity @Order(1) @EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true) class ServerSecurityConfiguration