spring-web

How to upload file in spring?

与世无争的帅哥 提交于 2021-02-16 14:59:41
问题 I am not able to get the file name in spring controller <form:form method="post" modelAttribute="sampleDetails" enctype="multipart/form-data"> <input type="file" name="uploadedFileName" id="fileToUpload" required="" > <input type="submit" name="import_file" value="Import File" id="" /> </form:form> Its my post method in controller @RequestMapping(method = RequestMethod.POST) public String importQuestion(@Valid @RequestParam("uploadedFileName") MultipartFile multipart, @ModelAttribute(

Can not access deployed WAR file on Tomcat

笑着哭i 提交于 2021-02-08 11:55:52
问题 I have a Spring boot rest service project which works on my local machine. When I run the application as "Spring Boot App" I can access the rest service by going to http://127.0.0.1:8080/persons/all and it returns JSON as it's supposed to. I changed the pom.xml packaging to war, I then created a war by going to Run as -> Maven build in Spring tools suit. It creates a war file. When I upload the war file on http://myserverip:8080/manager/ I get no errors and it shows up under Applications The

How to validate the size of @RequestParam of type List

浪尽此生 提交于 2020-01-30 05:18:18
问题 I'm creating a Spring-Boot microservice REST API that expects @RequestParam of type List<String> . How can I validate that the list contains a minimum and a maximum amount of values? So far I've tried using @Size(min=1, max=2) which is supposed to support collections as well ( javax.validation.constraints.Size ). I also tried adding @Valid and a BindingResult parameter along with the @Size annotation without success. I'd prefer using a solution similar to the first example using @Size(min=1,

jdbcTemplate is null and throws null pointer exception [duplicate]

血红的双手。 提交于 2020-01-25 06:05:04
问题 This question already has answers here : Why is my Spring @Autowired field null? (17 answers) Closed 3 years ago . I am new to Spring. I am trying to develop REST API using Spring web MVC. I am trying to fetch some data using JdbcTemplate from my database (MySQL). But when i run the code in the server I am getting 500- Internal Server Error and the stack trace shows java.lang.NullPointerException . When i tried to debug the code i found out that jdbcTemplate variable has null value. I have

jdbcTemplate is null and throws null pointer exception [duplicate]

泄露秘密 提交于 2020-01-25 06:04:06
问题 This question already has answers here : Why is my Spring @Autowired field null? (17 answers) Closed 3 years ago . I am new to Spring. I am trying to develop REST API using Spring web MVC. I am trying to fetch some data using JdbcTemplate from my database (MySQL). But when i run the code in the server I am getting 500- Internal Server Error and the stack trace shows java.lang.NullPointerException . When i tried to debug the code i found out that jdbcTemplate variable has null value. I have

How to change ErrorAttributes of ResponseStatusException?

六眼飞鱼酱① 提交于 2020-01-23 10:09:28
问题 How can I change the error attributes that are exposed when throwing a ResponseStatusException ? Especially I want to hide the exception , error and status type in the json, but only during production. @RestController public class MyController { @GetMapping("/test") public Object get() { throw new org.springframework.web.server.ResponseStatusException( HttpStatus.Forbidden, "some message"); } } Result: { "timestamp": "2018-11-06T12:16:50.111+0000", "status": 403, "error": "Forbidden",

Spring CORS controller annotation not working

时光总嘲笑我的痴心妄想 提交于 2020-01-12 15:12:14
问题 I want to allow cross origin requests for one domain. My project uses Spring so I want to take advantage of the new CORS support. I am using version 4.2.0 for all springframework dependencies. I followed the example here https://spring.io/blog/2015/06/08/cors-support-in-spring-framework#disqus_thread and tried the first version. My controller annotations looks like: @CrossOrigin(origins = "http://fiddle.jshell.net/", maxAge = 3600) @Controller @RequestMapping("/rest") public class

Spring CORS controller annotation not working

守給你的承諾、 提交于 2020-01-12 15:11:56
问题 I want to allow cross origin requests for one domain. My project uses Spring so I want to take advantage of the new CORS support. I am using version 4.2.0 for all springframework dependencies. I followed the example here https://spring.io/blog/2015/06/08/cors-support-in-spring-framework#disqus_thread and tried the first version. My controller annotations looks like: @CrossOrigin(origins = "http://fiddle.jshell.net/", maxAge = 3600) @Controller @RequestMapping("/rest") public class

How do I inject a buffered reader into a class with a file reader as its parameter using Spring boot?

早过忘川 提交于 2020-01-07 03:25:09
问题 I have this spring boot application in which I have the below line inside a method. BufferedReader reader = new BufferedReader(new FileReader("somePath")); How can I inject this into my code so I can mock it for my unit tests? Using guice I could use a provider. But how can I achieve this using spring boot? Any help would be much appreciated. 回答1: If you want to mock the class which have the below line inside a method. BufferedReader reader = new BufferedReader(new FileReader("somePath"));

SubProtocolWebSocketHandler - No handlers

对着背影说爱祢 提交于 2020-01-03 04:40:06
问题 I have this ugly error during deploying Spring app on JBoss. 18:11:16,025 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/consumer]] (MSC service thread 1-7) Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener: org.springframework.context.ApplicationContextException: Failed to start bean 'subProtocolWebSocketHandler'; nested exception is java.lang.IllegalArgumentException: No handlers at org