Using Craig Walls book "Spring in Action, 4th Edition", 7.2. Processing multipart form data
The code does not run no matter what path you try. I even tried
C:\something. Don't you have to create the directory first?
When I run the code, I get the error below:
root cause org.springframework.web.multipart.MultipartException:
Could not parse multipart servlet request; nested exception is
java.io.IOException: The temporary upload location
[C:\Users\jokra_000\sts-bundle\pivotal-tc-server-developer-3.1.1.RELEASE\base-instance\work\Catalina\localhost\spittr\tmp\spittr\uploads]
is not valid
Has anyone successfully uploaded an image file as outlined in Chapter 7? It seems there's far more to it than what's described, as the code Craig supplied does not run. Instead it crashes and will not upload a file.
Any suggestions on how to implement MultipartFile and the Path?
Craig's suggestion:
in AbstractAnnotationConfigDispatcherServletInitializer:
#Override
protected void customizeRegistration(Dynamic registration) {
registration.setMultipartConfig(
new MultipartConfigElement("/tmp/spittr/uploads", 2097152, 4194304, 0));
}
in Controller processRegistration:
MultipartFile profilePicture = spitterForm.getProfilePicture();
profilePicture.transferTo(new File("/tmp/spittr/" + spitter.getUsername() + ".jpg"));
It worked when I did the following:
I changed MultipartResolver:
#Bean
public MultipartResolver multipartResolver() throws IOException {
return new CommonsMultipartResolver();
}
and also added the dependency:
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>
and then imported to the file that extends WebMvcConfigurerAdapter:
import org.apache.commons.fileupload.FileItemFactory;
Related
I have a Spring Boot application using Thymeleaf as template resolver, which works fine when debugging from NetBeans, but gives me this error running its .jar:
Error resolving template "/theme/property", template might not exist or might not be accessible by any of the configured Template Resolvers
The app is set to auto-configurer with the annotation #SpringBootApplication, at an extension of SpringBootServletInitializer. I haven't set any contextPath into the properties file. I'm using Thymeleaf 2.1.6 and Spring 4 version. The jar is generated with Maven.
Doing some research I've come out that in some controllers I was passing a double slash, which I've solved but most pages still not working.
This controller works:
#GetMapping("/{idweb}")
String frontEndHome(#PathVariable("idweb")Integer idweb, Model model){
...
return "theme/home";
With the return statement set as return "/theme/home"; doesn't work. I guess, because the template resolver is recieving a double slash (//).
This other controller raises the error:
#GetMapping("/{idweb}/property")
String frontEndProperty(#PathVariable("idweb") Integer idweb, #RequestParam(value = "idproperty", required = false) Integer idproperty, Model model) throws Exception {
...
return "theme/property";
The index controller works fine as well:
#GetMapping("/")
public String index(Model model){
...
return "index";
}
That's my application starter class:
#SpringBootApplication
public class RentalWebsApplication extends SpringBootServletInitializer {
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(RentalWebsApplication.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(RentalWebsApplication.class, args);
}
}
For Thymeleaf I haven't set any configuration, although I've tested the app setting this into the application.properties file, with the same result:
spring.thymeleaf.prefix=classpath:/templates/
All html files are set into:
src/main/resources/templates
The html files from the examples are in:
src/main/resources/templates/index.html
src/main/resources/templates/theme/home.html
src/main/resources/templates/theme/property.html
There are some other questions dealing with the same issue, but none has a solution that works for me. Any help, would be much appreciated.
Update
Deploying the jar into Pivotal Web Services, the whole website works fine, but not deploying it with Boxfuse, Heroku or running the jar locally. Therefore, I guess the origin of the problem is some wrong configuration, that Pivotal system detects and corrects.*
*
PWS isn't correcting a configuration problem. It unpacks your jar file before running the application which stops the double slash from causing a problem. – Andy Wilkinson
At the end the solution was related to the double slashes, that the classpath:/templates/ gets if we set a return statement with a slash at the beginning like:
return "/theme/property"
Instead of:
return "theme/property"
In my case, the problem was not at the controller, but in the html with the Thymeleaf references of fragments, like in this example:
<footer th:replace="/index::footer"></footer>
Instead of:
<footer th:replace="index::footer"></footer>
What I don't understand is why the IDE's (NetBeans and STS), where not raising the error.
use
return new ModelAndView("member2",map);
instead of
return new ModelAndView("/member2",map);
Remove spring.thymeleaf.prefix=classpath:/templates/ from your application.properties.
This question already has answers here:
How to export swagger.json (or yaml)
(7 answers)
Closed 3 years ago.
I am using java spring boot framework to create REST api for my project and I am using "springfox-swagger2 and springfox-swagger-ui" for generating swagger documentation. I am able to see my documentation using the URL http://localhost:8080/swagger-ui.html.
How can I create or generate swagger.json / spec.json, The documentation should not be with this application, we are using a separate application for listing the API docs.
You can get the url with your swagger-ui html page:
GET http://localhost:8080/v2/api-docs?group=App
And actually you can get all the urls with chrome/firefox develop tools network feature.
If you use Maven, you can generate client and server side documentation (yaml, json and html) by using swagger-maven-plugin
Add this to your pom.xml:
.....
<plugin>
<groupId>com.github.kongchen</groupId>
<artifactId>swagger-maven-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<apiSources>
<apiSource>
<springmvc>true</springmvc>
<locations>com.yourcontrollers.package.v1</locations>
<schemes>http,https</schemes>
<host>localhost:8080</host>
<basePath>/api-doc</basePath>
<info>
<title>Your API name</title>
<version>v1</version>
<description> description of your API</description>
<termsOfService>
http://www.yourterms.com
</termsOfService>
<contact>
<email>your-email#email.com</email>
<name>Your Name</name>
<url>http://www.contact-url.com</url>
</contact>
<license>
<url>http://www.licence-url.com</url>
<name>Commercial</name>
</license>
</info>
<!-- Support classpath or file absolute path here.
1) classpath e.g: "classpath:/markdown.hbs", "classpath:/templates/hello.html"
2) file e.g: "${basedir}/src/main/resources/markdown.hbs",
"${basedir}/src/main/resources/template/hello.html" -->
<templatePath>${basedir}/templates/strapdown.html.hbs</templatePath>
<outputPath>${basedir}/generated/document.html</outputPath>
<swaggerDirectory>generated/swagger-ui</swaggerDirectory>
<securityDefinitions>
<securityDefinition>
<name>basicAuth</name>
<type>basic</type>
</securityDefinition>
</securityDefinitions>
</apiSource>
</apiSources>
</configuration>
</plugin>
........
You can download *.hbs template at this address:
https://github.com/kongchen/swagger-maven-example
Execute mvn swagger:generate
JSon documentation will be generated at your project /generated/swagger/ directory.
Past it on this address :
http://editor.swagger.io
And generate what ever you want ( Server side or Client side API in your preferred technology )
I'm a little late here, but I just figured out that you can open your browser console and find the URL to the GET request that returns the JSON definition for your Swagger docs. The following technique worked for me when mapping my API to AWS API Gateway.
To do this:
Navigate to your Swagger docs endpoint
Open the browser console
Refresh the page
Navigate to the network tab and filter by XHR requests
Right click on the XHR request that ends in ?format=openapi
You can now just copy and paste that into a new JSON file!
I have done this with a small trick
I have added the following code in the end of my home controller test case
import org.springframework.boot.test.web.client.TestRestTemplate;
public class HomeControllerTest extends .... ...... {
#Autowired
private TestRestTemplate restTemplate;
#Test
public void testHome() throws Exception {
//.......
//... my home controller test code
//.....
String swagger = this.restTemplate.getForObject("/v2/api-docs", String.class);
this.writeFile("spec.json", swagger );
}
public void writeFile(String fileName, String content) {
File theDir = new File("swagger");
if (!theDir.exists()) {
try{
theDir.mkdir();
}
catch(SecurityException se){ }
}
BufferedWriter bw = null;
FileWriter fw = null;
try {
fw = new FileWriter("swagger/"+fileName);
bw = new BufferedWriter(fw);
bw.write(content);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
if (fw != null)
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
I don't know this is right way or not But it is working :)
Dependency
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
You should be able to get your swagger.json at
http://localhost:8080/api-docs
assuming your don't have kept the versioning as in the pet store sample application. In that case the URL would be:
http://localhost:8080/v2/api-docs
To get the api json definition for REST API, if swagger is configured properly. you can use directly swagger/docs/v1, this means the complete url will be, if version v1 (or just specify the version)
http://localhost:8080/swagger/docs/v1
I'm trying to make the example of this link: https://github.com/spring-guides/gs-uploading-files work with Apcahe commons-fileuploadbut the error: Required MultipartFile parameter 'file' is not present is occurring.
The configuration that I'm making on the example is:
1 - Adding the dependency of commons-fileupload in the pom.xml:
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
2 - Config the multipartResolver() in the Application.java
#Bean
public MultipartResolver multipartResolver() {
return new CommonsMultipartResolver();
}
Without this configuration everything works fine as Spring is using org.springframework.web.multipart.support.StandardServletMultipartResolver for handle uploads, but I want use commons-fileupload.
There is something that I'm missing?
Thanks.
#avaz
You should disable MultipartAutoConfiguration like this below:
#EnableAutoConfiguration(exclude = MultipartAutoConfiguration.class)
Please see the DOC
I'm currently trying to implement Dart in an existing project which uses Spring MVC (4.0.0) and Thymeleaf (2.1.1) as template engine.
Currently I am deploying all my Dart resources to /dart as shown belown.
<link rel="import" th:href="#{/dart/wb-control-text.html}" />
<script type="application/dart" th:src="#{/dart/packages/polymer/init.dart}"></script>
<script th:src="#{/dart/packages/browser/dart.js}"></script>
Thymeleaf rewrites the urls to http://localhost:8080/context/dart/..., which is correct.
Dart works really great if you open a HTML file directly which has the packages folder directly beneath it. However, this is not the case in my project, which has friendly urls like /action/users/browse and you don't have access directly to the HTML file.
When a Dart library tries to import a package, I get a 404 error because it is looking for it in the wrong place (e.g. /dart/packages/polymer/packages/polymer/polymer.dart).
Do I need to provide a URL request handler or a filter that handles all **/packages/** requests (and just use paths relative to the current url)? Or is there an option in Dart where you can set where it should look for packages?
What is the solution (or workaround) for this?
Edit
I currently have a temporary solution that works, but it is dirty and I'm still looking for a cleaner solution.
I added the packages to my classpath and I created a DartPackagesFilter that streams the resources:
public class DartPackagesFilter extends OncePerRequestFilter
#Override
protected void doFilterInternal(final HttpServletRequest request,
final HttpServletResponse response,
final FilterChain filterChain)
throws ServletException, IOException {
final String uri = request.getRequestURI();
final int index = uri.indexOf("/packages/");
if ( index != -1 ){
final String resourceName = uri.substring(index);
writeResourceToResponse(response, resourceName);
return;
}
filterChain.doFilter(request, response);
}
private void writeResourceToResponse(final HttpServletResponse response, final String resourceName) throws IOException {
final ClassPathResource resource = new ClassPathResource(resourceName);
response.setContentType(resolveContentType(resourceName));
ChannelUtils.stream(resource.getInputStream(), response.getOutputStream());
}
private String resolveContentType(final String resourceName){
if ( resourceName.endsWith("dart") ){
return "application/dart";
} else if ( resourceName.endsWith("js")){
return "text/javascript";
}
throw new IllegalArgumentException("Resource must be a Dart or Javascript file!");
}
}
In web.xml:
<filter-mapping>
<filter-name>DartPackagesFilter</filter-name>
<url-pattern>*.dart</url-pattern>
<url-pattern>*.js</url-pattern>
</filter-mapping>
In my HTML file I refer to the packages relatively to the current URL:
<script src="packages/browser/dart.js"></script>
You will need to handle /packages/ requests separately, like your work-around. However, these hacks would only be needed for development when you are working with the actual .dart files.
When you deploy your app, you'll use either dart2js, dart2dart or, most likely, both. These tools produce a monolithic script file which does not rely on the external packages directory.
Since you only need the /packages/ directory for development, it is possible to set the the packages URL through a flag in Dartium. However, in my experience, this is an awkward solution since the flag applies to all Dart applications -- all Dart apps would need to fetch packages through the same URL scheme. It also makes it difficult to share your app with other Dartium installations.
I am doing a project for school. Subject of project is tshirt design. I am using jsf and primefaces. But I don't know well jsf and primefaces. I wanted save a base64 from html as image in jsf project. But when I had tried to following functions, NullPointerException. This function is called in a Servlet. A base64 is grabbed by this Servlet.
public static void save(String dataURL){
line85: ExternalContext external = FacesContext.getCurrentInstance().getExternalContext();
ServletContext servletContext = (ServletContext) external.getContext();
String filename = servletContext.getRealPath("cloud.png");
BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes;
try {
decodedBytes = decoder.decodeBuffer(dataURL.split("data:image/(png|jpg);base64,")[1]);
BufferedImage imag=ImageIO.read(new ByteArrayInputStream(decodedBytes));
ImageIO.write(imag, "png", new File(filename));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
23.Ara.2012 17:48:20 org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [org.soft.tshirt.servlets.DesignServlet] in context with path [/Tshirt] threw exception
java.lang.NullPointerException
at org.soft.tshirt.beans.ImageBean.save(ImageBean.java:85)
at org.soft.tshirt.servlets.DesignServlet.processRequest(DesignServlet.java:102)
at org.soft.tshirt.servlets.DesignServlet.doPost(DesignServlet.java:76)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
The current instance of the FacesContext is only available in the HTTP request thread which is been served through the FacesServlet who is the one responsible for creating the FacesContext. In other words, only when the request URL matches the URL pattern of the FacesServlet. In other words, only JSF pages, JSF managed beans (and thus not backing beans which you instantiate yourself like as is happening here!) and all other JSF artifacts like event/phase listeners, UI components, etc have the current instance of the FacesContext available to them.
In an arbitrary homegrown HTTP servlet, the FacesContext isn't available at all, for the simple reason that the FacesServlet is not been invoked beforehand. So the getCurrentInstance() method on it would return null. You know, trying to access/invoke any fields/method on null will cause NullPointerException. See also its javadoc.
In order to achieve the sole goal of obtaining the ServletContext, just invoke the inherited GenericServlet#getServletContext() method inside the servlet.
protected void doPost(...) {
String filename = getServletContext().getRealPath("cloud.png");
// ...
}
Pass that information instead to the method responsible for creating the file. You might want to extract that code into a different class which is reused between your servlet and backing bean.
Unrelated to the concrete problem, writing files to the expanded WAR folder is really a bad practice for the reasons mentioned in among others this answer: Uploaded image only available after refreshing the page.