upload file springboot Required request part ‘file’ is not present

This is how your request in Postman should look like: My sample code: application.properties #max file and request size spring.http.multipart.max-file-size=10MB spring.http.multipart.max-request-size=11MB Main Application Class: Application.java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } Rest controller class: import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestBody; … Read more

Could not verify the provided CSRF token because your session was not found in spring security

According to spring.io: When should you use CSRF protection? Our recommendation is to use CSRF protection for any request that could be processed by a browser by normal users. If you are only creating a service that is used by non-browser clients, you will likely want to disable CSRF protection. So to disable it: @Configuration … Read more

Return HTTP 204 on null with spring @RestController

You can use the @ResponseStatus annotation. This way you can have a void method and you don’t have to build a ResponseEntity. @DeleteMapping(value = HERO_MAPPING) @ResponseStatus(value = HttpStatus.NO_CONTENT) public void delete(@PathVariable Long heroId) { heroService.delete(heroId); } BTW returning 200 when the object exists and 204 otherwise it’s a bit unusual regarding API REST design. It’s … Read more

Reading HTTP headers in a Spring REST controller

The error that you get does not seem to be related to the RequestHeader. And you seem to be confusing Spring REST services with JAX-RS, your method signature should be something like: @RequestMapping(produces = “application/json”, method = RequestMethod.GET, value = “data”) @ResponseBody public ResponseEntity<Data> getData(@RequestHeader(value=”User-Agent”) String userAgent, @RequestParam(value = “ID”, defaultValue = “”) String id) … Read more

Difference between path and value attributes in @RequestMapping annotation

As mentioned in the comments (and the documentation), value is an alias to path. Spring often declares the value element as an alias to a commonly used element. In the case of @RequestMapping (and @GetMapping, …) this is the path property: This is an alias for path(). For example @RequestMapping(“/foo”) is equivalent to @RequestMapping(path=”/foo”). The … Read more

When looking at the differences between X-Auth-Token vs Authorization headers, which is preferred?

Authorization is the primary header used by clients to authenticate against peers in HTTP as foreseen in RFC 7235. It is often linked to the Basic authentication scheme as per RFC 7617, but that is not a given. The Basic scheme allows clients to provide a username-password-pair separated by a colon (:) coded in Base64. … Read more