I am facing the same problem as Springboot Required request part 'file' is not present
and the solutions provided here have not worked. Spring boot version v2.2.1
Postman Req Here
spring.http.multipart.enabled=true
Anything else I can do?
#RequestMapping(value = "/executescript", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public #ResponseBody String uploadFileHandler(#RequestParam("File") MultipartFile file)
Related
I am trying to create a simple spring boot application which has jersey jars as well for some internal rest client implementation to call. My application is starting fine but when trying to hit the controller method, nothing is called & on postman its saying as 404 Not Found.
Spring boot version: 1.5.14.RELEASE
Jersey Client: 2.25.1 (org.glassfish.jersey.core)
Controller class
#EnableAutoConfiguration(exclude = {SecurityAutoConfiguration.class, DataSourceAutoConfiguration.class})
#RequestMapping("/report")
public class ReportProcessController {
Controller method which I am trying to access
#RequestMapping(value = "/email", method = RequestMethod.GET)
#ResponseBody
public String sendEmail(HttpServletResponse response) {
I have a requirement to integrate OpenAPI 3 documentation for my Spring Boot 2 project. We did not used modals/DTOs on controllers.
Here is the sample controller:
#RestController
#RequestMapping(value = "/pet")
public class PetController {
#RequestMapping(value = "/save", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
#ResponseBody
public ResponseEntity<Map<String, Object>> savePet(
#RequestBody Map<String, Object> petObj, HttpServletRequest request)
throws Exception {
String petResponse = petDAO.savePet(petObj, request, true);
return new ResponseEntity<Map<String, Object>>(petResponse, HttpStatus.OK);
}
}
Request body:
{
"name":"Test",
"category":"school"
}
My response:
{
"petId":"1",
"petName":"Test",
"petCategory":"school",
"petStaus":"active"
}
I am not able to find a way to add the OpenAPI doc for my custom Map object. I want to add key, description, type, example(s) for each property in my Map manually.
Can anyone suggest how to do this?
This is the default behaviour of the springdoc-openapi library in order to ignore other injectable parameters supported by Spring MVC.
https://docs.spring.io/spring/docs/5.1.x/spring-framework-reference/web.html#mvc-ann-arguments
If you want to change this behaviour, you can just exlcude it as follow:
SpringDocUtils.getConfig().removeRequestWrapperToIgnore(Map.class);
When trying to hit the rest api end point of spring boot application I get 404 not found, below is the project structure snapshot attached
This is my project structure
Controller -
#RestController
#RequestMapping("/users")
public class UserRestController {
private final UserService userService;
public UserRestController(UserService userService){
this.userService = userService;
}
#RequestMapping(value = "/create", method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public void createUser(#RequestBody final UserDTO user){
userService.saveUser(user);
}
}
You are not scanning your controllers.
In the image you shared #ComponentScan only scans Service folder. But your controllers are in RestController folder.
Use #ComponentScan without any arguments. By default it will scan for beans in current folder and its sub folder.
e.g #ComponentScan()
#SpringBootApplication covers #EnableAutoConfiguration, #ComponentScan, #Configuration.
#RequestMapping(value = "/create", method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
and for this, you can simply do
#PostMapping(value ="/create"
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
Lets test it in postman, it should be POST and url must be
localhost:<your port>/users/create. Dont forget to add your UserDTO in your Postman Body.
In case you missing it, your application must be up and running.
imporatant Please check your HTTP method type when call end point form postman.it must be "post"
Bro if you are using Spring boot. then no need to write #EnableAutoConfiguration, #ComponentScan, #Configuration.
#SpringBootApplication Covers all these.
You just check your url it should be like http://localhost:8080/users/create
used autowired
#autowired
private final UserService userService;
For what purpose #PostMapping annotation is used in Spring MVC?
#PostMapping is a composed annotation that acts as a shortcut for #RequestMapping(method = RequestMethod.POST).
#PostMapping annotated methods handle the HTTP POST requests matched with given URI expression. e.g.
#PostMapping(path = "/members", consumes = "application/json", produces = "application/json")
public void addMember(#RequestBody Member member) {
//code
}
Follows this :example
Hope this helps..!
Spring Framework 4.3 has introduced #PostMapping annotation.
#PostMapping is a composed annotation that acts as a shortcut for
#RequestMapping(method = RequestMethod.POST)
Similarly the following annotations are available:
#GetMapping
#PutMapping
#DeleteMapping
#PatchMapping
These annotations can improve the readability of code.
Reference: Spring API documentation.
I'd like to use a placeholder to define the path of a request as shown below. When I attempt the request its seems that spring-cloud-feign is unable to swap the placeholder with an application.yml value.
#FeignClient("foo")
public interface FooClient{
#RequestMapping(value = "${my.special.placeholder}",
method = RequestMethod.GET,
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
ResponseEntity getSomeData(s);
}