Setup:
Intellij IDE community
Spring Boot 2.4.0
Spring rest docs
Hi,
I use spring rest docs in all my services. The problem is that in one of them, the custom snippets are ignored and I just don't know why. Although if i'm sure that it's a mistake of mine I just can't find it.
Example snippet
One of the snippets I want to customize is the http request.
So I placed a file http-request.snippet under src/test/resources/org/springframework/restdocs/templates/asciidoctor
Content:
[source,http,options="nowrap"]
----
{{method}} {{path}} HTTP/1.1
----
So I would expect that the requestbody is not documented in this snippet but instead it is still included.
Like I said all my snippets are ignored.
UPDATE:
I've debugged StandardTemplateResourceResolver class. In fact, my templates are ignored.
I looked at the path and as you can see the method getFormatSpecificCustomTemplate should resolve my snippets but it doesn't.
Is there something else I have to configure when I add a new resource folder and I add it to the classpath?
SOLVED
I found the problem by looking at the asciidoc path in my explorer. The problem was that the creation of the direktories went wrong. Instead of creatin nested subdirectories for org/springframework/.. there was one folder with the whole directory structure as one name. Sorry for the confusion but in intellij the structure looked right.
Related
Hi I am new to Swagger and Spring. I want to know how can I edit the swagger-ui.html page. I am using Pojo files to provide information related to parameters. What can I do to make this look better and proper? The reason I think this is happening is because I have used many Objects inside many objects. My assumption is, this should be solved using html and CSS but are getting it from swagger built-in dependencies, so can't be edited.
Assuming springdoc there is a documentation part for you: https://springdoc.org/#customizing-swagger-static-resources
You can customize swagger documentation static resources located in
META-INF/resources/webjars/swagger-ui/{swagger.version}/. The list of
resources includes: ... swagger-ui.css
And there is a code snippet.
I want to add a new menu to spring batch admin,
I have gone through the docs but not helpful, can anyone please help me with an example also where to put FTL files for that menu.
Thanks in advance
Your question is far too open ended.
HERE is an example of how to customize spring-batch admin.
Be advised that spring batch admin is at end of life and the last milestone release has quite a few issues in its POM dependencies. You'll need liberal use of maven exclude elements to get it to compile without a ton of dependency conflicts.
End Of Life Announcement
Per the above you should look into "Spring Cloud Data Flow" instead of spring-batch-admin. I have not used this yet, but the maven issues in the milestone release were a huge pain. Honestly I never did get it to work the way I wanted.
All that said your FTL files go in the webapp/WEB-INF/web directory tree.
Good luck
To add a menu in SBA, you need to develop following components:
A class that extends the BaseMenu (Class implementing the SBA menu interface).
A Controller class handling the request issued on click of you menu
A View bean
A Freemarker template implementing the view.
Template files are looked at WEB-INF/web. Place your template file under WEB-INF/web/ftl.
I’ve been trying to set up a Spring MVC application from scratch, using Maven, in IntelliJ Idea. I know there are probably some nice Archetypes that can do this for you, but I really want to understand what’s going on. Here’s what I want to do:
Create a simple web application that shows displays “Hello World” under https://localhost:8080/, using an embedded Tomcat (with the Maven plugin). So, one controller, one request mapping, and one template. I’ve been able to get the spring application to boot (the Spring logo appears in the console) and Tomcat seems to run as well (the site responds). The problem is, I haven’t been able to get the request to map to my RequestMap method in my Controller. I’m quite sure that the controller isn’t the problem, but that it’s some configuration/setup issue.
Here’s the thing: I’ve been looking at tutorials, StackOverflow, Spring documentation, etc. for hours now, but I haven’t found a source that really explains how to configure a Spring MVC Maven project. Everyone seems to have a different opinion on what XML files you need, what they should be named, and where they should be located. The consensus seems to be that you need a web.xml file in a folder named WEB-INF, but even there, everyone has a different opinion on where that folder should go. I appreciate that there are multiple different ways to do this, and that there’s no “right” way, but in my experience, there are definitely many “wrong” ways =).
My question right now isn’t necessarily how to get my current project running (I don’t mind starting over), but what kinds of config files are out there (web, spring, app-config, servlet, …), what they do, which ones you need, where they need to go, what they should be named, how they connect, etc. Some people also use the Maven Compiler plugin, and others don’t, and nobody says why :D. Essentially: How do I let Spring know where to find controllers, templates, etc., and how to run and deploy all of this on the embedded Tomcat. I would like to find a source that explains the entire core-ecosystem of Spring MVC in a unified way. I have found many sources that provide “how-to” tutorials, but with little to no explanation (like “add the following servlet.xml file to your WEB-INF folder”). So, if your setup slightly differs from the tutorial (e.g. because you are using IntelliJ instead of Eclipse, Tomcat instead of Jetty, embedded Tomcat, a slightly different folder structure) nothing works, and, most importantly, you have no clue why.
Has anyone learned these setup-basics with anything better than copy/pasting or trial & error? :D
Easiest way to start is by using Spring Boot. Go to following link:
Spring initializr
Choose "Web" for "Selected Dependencise" and click "Generate Project". Download of maven project should start after that. Then import that project in IntelliJ IDEA as a Maven project. And that is a good starting point. You'll get main class that you can run and embeded Tomcat will start at 8080 port. Than you can add some controller, template etc...
Also good guides
I am trying to use Spring with: gradle, annotations (not xml), springboot, embedded server. Followed the guides of the side; they are great. Now I wanted to try it myself. I am creating a simple MVC (however I do not understand the difference between MVC and web-MVC entirely) - and want to add 'security'.
This example 'insecuremvc' (is maven, is war; instead of gradle/jar; but it is available). I could make it run.
However, where are the sources? I wanted to see them to learn it. It runs in my localhost, magically if said so, only by poms and downloaded jars - while, I could not find the jar of insecuremvc or such.
A similar question did not help. Is there anything fundamental that I have missed?
You wont have sources located in a WAR file as it means a web-archive. You can use the below link to get your sources which are in Groovy.
Source code
If there is anything else, let me know.
I'm having a stupid configuration issue with Ibatis in my Spring project. Please don't jump on me about how all this was setup, I'm just following the "in house project structure policy".
So here is the structure, we have the "src/main/resources/META-INF/" folder that contains all of our config files used by the application, and then there is a "src/test/resources/META-INF/" that contains only the config files that have different settings to run unit testing.
Well in our case that's only one file, the src/main/resources/META-INF/spring/application-config.xml became the src/test/resources/META-INF/spring/test-application-config.xml. I'm am not going to outline the small differences between the two, because that part works fine.
The test-application-config.xml imports the src/main/resources/META-INF/spring/data-access-config.xml file just fine, which in turns use the src/main/resources/META-INF/ibatis/sqlmap-config.xml successfully... After that is when it goes to Hell.
See up until now we're using Spring to find the next config files in the classpath, but when we hit sqlmap-config.xml we leave the spring framework for the ibatis framework I believe, which loads the resource files defined inside it relative to the classpath (that's taken from the doc, whatever that means).
Inside the sqlmap-config.xml are defined a few resource files we're using that live inside the src/main/resources/META-INF/ibatis/mapping folder.
They are referenced like this:
<sqlMapConfig><sqlMap resource="/META-INF/ibatis/mapping/MyObject.xml"/></sqlMapConfig>
That works fine when I run the app normally, but when I run my JUnit test cases I get an IO exception stating that it can't find the file /META-INF/ibatis/mapping/MyObject.xml.
I've tried to change the path in the sqlmap-config.xml to "mapping/MyObject.xml" but that didn't help. I've also tried to use the Spring classpath prefix "classpath:META-INF/ibatis/mapping/MyObject.xml", didn't work either.
Anyone would have any idea on how to set that Ibatis properly so it works for both the app and the junit?
Thanks.
To solve this problem, I removed all the the Ibatis files and folders from the src/test/resources/META-INF folder.
The sqlmap-config.xml in src/main/resources/META-INF/ibatis/mapping file now maps like this:
<sqlMapConfig><sqlMap resource="META-INF/ibatis/mapping/MyObject.xml"/></sqlMapConfig>
Please note that compared to my initial post the leading "/" is gone... I think that's what made the difference here.
Hopes this helps anyone running into similar issues.
Just to see whether what you are saying is actually the problem.. you might want to place your mappings (MyObject.xml) in the same folder as sqlmap-config.xml. I say this because I've had my fair share of spring + ibatis + unit testing problems. (see resolved question asked by me)
Also, you might be getting IO exception because the mappings file does not exist outside the container (when you run tests).
You should also post definition for bean created from SqlMapClientFactoryBean. This should have configLocation property that contains path to sqlMapConfig xml
I had the same problem and could not find a (quick) solution that explained what exactly could be going wrong. Hence my answer.
As Spring documentation for Ibatis says:
Remember that iBATIS loads resources from the class path, so be sure
to add the 'Account.xml' file to the class path.
In your case by adding META-INF to your webproject build path i.e. if you used Eclipse, set <classpathentry kind="src" path="META-INF"/> in your projects' .classpath (This will be visible under Navigator view in Eclipse)