spring-mvc

Handling empty results on findAll(): Is there a orElseThrow() for lists?

浪尽此生 提交于 2020-06-29 03:56:45
问题 I am creating a little Spring REST service. I have a findById() call: @GetMapping("/items/{id}") MyItem one(@PathVariable String id) { return repository.findById(id).orElseThrow(() -> new MyItemNotFoundException(id)); } If there is no MyItem object with the given id , I am throwing an exception using the Optional<T>.orElseThrow() method. This is very useful and quite simple. Now I added a findAll() call from the PagingAndSorting<T, ID> repository: @GetMapping("/items") List<MyItem> all() {

Handling empty results on findAll(): Is there a orElseThrow() for lists?

随声附和 提交于 2020-06-29 03:56:05
问题 I am creating a little Spring REST service. I have a findById() call: @GetMapping("/items/{id}") MyItem one(@PathVariable String id) { return repository.findById(id).orElseThrow(() -> new MyItemNotFoundException(id)); } If there is no MyItem object with the given id , I am throwing an exception using the Optional<T>.orElseThrow() method. This is very useful and quite simple. Now I added a findAll() call from the PagingAndSorting<T, ID> repository: @GetMapping("/items") List<MyItem> all() {

How do I send an array of files using xhr2 and FormData? (Java + Spring)

喜欢而已 提交于 2020-06-28 08:20:08
问题 I'm using <input type="file" multiple /> to upload a list of files. This works fine as is, but I want the ability to remove individual files before upload, so I'm storing the FileList in a separate object and routing it through xhr. However, it doesn't work. The form looks like this: <form:form commandName="documentsBean" enctype="multipart/form-data"> <input type="hidden" name="submittedFormAction" value="attachDocumentSave"/> <input type="file" name="files" id="attachFiles" multiple/>

Why i am getting an error Factory method 'halLinkDisocoverer' threw exception in springboot?

拟墨画扇 提交于 2020-06-28 07:14:55
问题 Hi i am new at springboot, i trying to create RESTful API with it, it's already work when i create controller with it, but when i trying to add this line at pom.xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-rest</artifactId> </dependency> it give me error when i trying to run the application with java -jar target/gs-accessing-mongodb-data-rest-0.1.0.jar org.springframework.beans.factory.BeanCreationException: Error creating bean with name

Intercepting incoming client request using ClientHttpRequestInterceptor with RestController

我是研究僧i 提交于 2020-06-28 06:42:12
问题 I want to append some data to the incoming request. Like random generated token or uuid to the incoming request. And then I want to process it through the controller. I came to know about ClientHttpRequestInterceptor . But looking at this doc, it seems like it only intercept the response, it doesn't intercept the request. Which is what I am not looking. Is there any other way to do this ? And how can I register this intercept in my RestController ? So that before controller process the

Spring Boot escape characters at Request Body for XSS protection

雨燕双飞 提交于 2020-06-28 06:19:20
问题 I'm trying to secure my spring boot application using a XSSFilter like this: public class XSSFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public void destroy() { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { chain.doFilter(new XSSRequestWrapper((HttpServletRequest) request), response); } } And the wrapper: public class

Binding request parameters with underscores in Spring MVC 3.0

跟風遠走 提交于 2020-06-27 08:46:41
问题 Consider the following requirement: request parameters have to be bound to objects using Spring MVC 3.0. The request parameters contain underscores (e.g. http://myurl:80/x?param_one=1&param_two=2 ). These parameters should be bound to the following object: class MyObject { private Integer paramOne; private Integer paramTwo; ... } How would you go about doing this? Important note: consider that there may be a substantial amount of parameters and objects like this and that it's not considered

Spring security application of antMatcher() vs. antMatchers()

大兔子大兔子 提交于 2020-06-27 07:51:06
问题 Just want to see whether I'm interpreting the answer to this question the right way. If we only need to secure one path like this: http.antMatcher("/api/**").authorizeRequests().... Then use antMatcher() . If we need to secure multiple URL paths like this: http .authorizeRequests() .antMatchers("/high_level_url_A/sub_level_1").hasRole('USER') .antMatchers("/high_level_url_A/sub_level_2").hasRole('USER2') ... Then use antMatchers() . There are two answers in this question, but the example

spring mvc @RequestPart validation via annotation

冷暖自知 提交于 2020-06-26 19:44:01
问题 How can I validate that the body request part is not empty? @PostMapping("/messages") @ResponseStatus(HttpStatus.CREATED) fun createMessage(@Valid @RequestPart message: MessageCreate, @Valid @RequestPart @NotEmpty body: MultipartFile, @RequestParam attachments: List<MultipartFile>) { return service.create(message, body, attachments) } I tried to create a custom validator annotation that checks body.isEmpty() result but it has no effect. What is missing ? Is it possible do to it this way ? 回答1

.tmp files not being deleted in Multipart Spring MVC File Upload

一笑奈何 提交于 2020-06-25 21:38:52
问题 I've implemented a Spring MVC REST service that accepts a multipart message with both a file upload and a JSON body as the constitutive parts. Here are the main classes involved: My Controller: @RestController public class MyController { @Autowired private MyService myService; @RequestMapping(value = "/publish", method = RequestMethod.POST, consumes = "multipart/form-data", produces = "application/json") public PublishContentResponse publishContent(@RequestPart("json") PublishContentRequest