I decided to make a project using Mustache + Spring Boot + REST API. Earlier, I used to write projects with the front-end used Java EE + Servlets + JSP. Now I decided to try to switch to Spring and write an application with a front-end. And I found a way to create a page on the front-end using Mustache, but if you use Mustache, then everything needs to be saved to the model, and I want to use REST API. I mean if it is possible? If it's possible I'll be happy if you show a piece of code your RestController and Mustache.
The question is 'if possible call endpoint from mustache?'.
Yes, You can create REST API using OpenAPI(https://support.smartbear.com/swaggerhub/docs/tutorials/openapi-3-tutorial.html) which uses the mustache file to create your code.
You need to add following dependencies,
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.8</version>
</dependency>
<dependency>
<groupId>org.openapitools</groupId>
<artifactId>jackson-databind-nullable</artifactId>
<version>0.2.2</version>
</dependency>
add the following plugin in the pom.xml for configuration(https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator-maven-plugin/README.md),
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>6.0.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<templateDirectory>${project.basedir}/templates
</templateDirectory>
<configOptions>
//add the configuration options here
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
By default it uses the mustache files internally to generate codes, you can change the mustache files from the git location(https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/JavaSpring/)/create your own with the name mentioned in git and place it in the location ${project.basedir}/templates it will generate your codes during the compile time and you can use them in your project.
Place the OpenAPI document in the resource folder where application.properties is placed
Related
My Spring boot app (v2.1.1.RELEASE) is packaged using below plugin and layout :
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<finalName>my-service-${project.version}</finalName>
<mainClass>com.my.app.MainClass</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
I am using below launch script :
java -Dloader.path=/path/to/config/dir/ -jar my-service-1.0.jar
In one of dependencies which my app has, below code is used to read the external xml config files (e.g. hbase-site.xml) :
URL url = ClassLoader.getSystemResource(filename);
which is why I am trying to make those files available using loader.path but the app still doesn't read the files present on the provided dir. Am I missing anything else?
Already referred : Spring Boot: Is it possible to use external application.properties files in arbitrary directories with a fat jar?
Spring Boot Executable Jar with Classpath
I think it should be the problem of getSystemResource, try to use getClassLoader().getResourceAsStream(path)
I have two projects one is Angular Project(Front End Application ) and another one is Spring boot (Rest Api's).
When I run both the projects individually everything works fine. But now I want to generate a single runnable jar file for both these projects in such a way that when i run the jar in the localhost:8081 it should up both the Angular module as well as spring boot module.
I've added the following plugin to my pom.xml file
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${build.directory}/classes/static/</outputDirectory >
<resources>
<resource>
<directory>../angular6-MyProject/dist</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
Quick answer
You need to copy static files to ${build.directory}/classes/META-INF/resources folder for the servlet container serve them as static files from inside a war/jar file (that's how https://www.webjars.org work).
Handling HTML directly from jar
In the Static Content section of Spring Boot documentation you can find
By default, Spring Boot serves static content from a directory called /static (or /public or /resources or /META-INF/resources) in the classpath or from the root of the ServletContext
The /META-INF/resources is the "standard" way (though not most intuitive) and it comes directly from the Java Servlet Specification 3.0 (from JavaEE version 6)
A Web application exists as a structured hierarchy of directories. The root of this hierarchy serves as the document root for files that are part of the application. For example, for a Web application with the context path
/catalog in a Web container, the index.html file at the base of the Web application hierarchy or in a JAR file inside WEB-INF/lib that includes the
index.html under META-INF/resources directory can be served to satisfy a request from /catalog/index.html.
Therefore, setting an appropriate path should do the job.
Treating Angular application as a dependency
The quoted JavaEE spec is also what webjars utilize. Webjars are client-side dependencies packaged into JAR archive files. The primary reason for webjars to exist is to avoid adding and managing client-side dependencies (like Angular, jQuery), which often results in hard to maintain codebases.
But if the Angular can be a webjar, your frontend can be as well. What I tend to do is to pack the Angular application as a jar file and treat it as a dependency.
Frontend application
Driven npm from Maven + create Jar file
<project>
<groupId>com.examplegroupId>
<artifactId>myapp-ui</artifactId>
<packaging>jar</packaging>
<build>
<plugins>
<!-- run the frontend (npm / bower stack) and update dependencies -->
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<configuration>
<installDirectory>target</installDirectory>
</configuration>
<executions>
<execution>
<id>install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<nodeVersion>v8.9.1</nodeVersion>
<npmVersion>5.5.1</npmVersion>
</configuration>
</execution>
<execution>
<id>npm install</id>
<goals>
<goal>npm</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<arguments>install</arguments>
</configuration>
</execution>
<execution>
<id>npm build</id>
<goals>
<goal>npm</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<arguments>run build</arguments>
</configuration>
</execution>
</executions>
</plugin>
<!-- copy everything as a webjar -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/target/classes/META-INF/resources</outputDirectory>
<resources>
<resource>
<directory>dist/</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Backend application
Use the dependency directly
<dependencies>
<dependency>
<groupId>com.example<groupId>
<artifactId>myapp-ui</artifactId>
</dependency>
</dependencies>
Notices
Some issues and observations I've seen with webjars:
I've seen some problems with webjars and web fonts (fonts not being load by the browser when served from inside the jar file)
There are ways to build webjars using npm tools and all packages I've seen require and use java underneath anyway. I haven't seen any native JS solution
Webjars themselves don't impact the performance of an application, however serving static content from Servlet container is always significantly less performant (regarding possible throughput) than serving it from say nginx
I followed this tutorial
and it's somehow similar to what Jakub mentioned in his answer. I usually run the mvn spring-boot:run command to launch it in one go. The key to setting up the project is in the pom.xml. Make sure that you are telling the build to copy the Angular dist/ files into the right place. In this case, it needs to copy the compiled JS from Angular's dist/ folder to the server/target/classes/resources so it can host the static content.
The entire build (mvn clean install) can take forever. So during development I just go to the src/main/web directory and run ng-serve from there so I can see my UI changes quickly.
Hope this helps.
The back-end server uses Spring Boot with Spring Web MVC for REST Controller and Spring Data JPA for interacting with MySQL database. Front-end side is made with Angular 11, HTTP Client & Router.
– Spring Boot exports REST APIs using Spring Web MVC & interacts with MySQL Database using Spring Data JPA.
– Angular Client sends HTTP Requests and retrieve HTTP Responses using axioms, shows data on the components. We also use Angular Router for navigating to pages.
Steps to follow to set up Angular 11 with Spring Boot Application.
Create Angular 11 Application to run and build successfully.
Create build according to environment like QA, PROD, LOCAL.
from dist. folder collect compiled files through node-js.
Angular runs on node server by using typescript. once you compile code and build angular project in dist. folder you will get .js files. java script files.
We can you as it is js files from dist folder to run any UI application.
Create Spring boot application and write REST apis and compile and build successfully.
create static folder in resource folder.
put static folder path inside application.properties like classpath:"resource/static/".
put all compiled js file in to static folder from dist folder.
create webConfig.java file in spring boot project to routing URL for index.html.
Create build nd compile successfully spring boot project.
if you run the project you will get UI from spring boot project.
Create jar from spring boot project.
Deploy on Azure and run the project successfully.
thank you you can visit my blog for in details.
https://draft.blogger.com/blog/post/edit/preview/8320325077779788397/7601438449518293236
I'm confused about the way I'm compelled to create a SOAP service in spring boot. I wrote many SOAP services in Java in the past, simply writing down the java code and not a line of XML. A very easy and error less approach.
All the tutorial I read for Spring Boot has the need to write an XSD document, form which maven will read and build the needed classes.
Is there a way, supported by Spring Boot, to bypass the XSD file and write directly the needed java classed instead?
Just to give you some example of what I read, here are some links to the tutorials I'm referring to:
https://www.concretepage.com/spring-boot/spring-boot-soap-web-service-example
https://howtodoinjava.com/spring-boot/spring-boot-soap-webservice-example/
https://www.javainuse.com/spring/springbootsoapwebservice
I believe that writing the XML code to generate the Java one is very error prone and a really difficult to maintain code, when you're developing big services or services with a lot of objects. All works fine if you have a small project, that is not my case.
What I did was to write the classes and then generate the xsd file using schemagen.
This is the configuration in maven pom I added:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>schemagen</id>
<goals>
<goal>schemagen</goal>
</goals>
</execution>
</executions>
<configuration>
<outputDirectory>${basedir}/src/main/resources/xsds/</outputDirectory>
<transformSchemas>
<transformSchema>
<uri>http://test/test-ws/MyTestSchema</uri>
<toPrefix>test</toPrefix>
<toFile>test.xsd</toFile>
</transformSchema>
</transformSchemas>
<sources>
<source>${basedir}/src/main/java/my/classes/</source>
</sources>
<verbose>true</verbose>
</configuration>
</plugin>
We have created a Yaml file using Swagger editor for our APIs specification which includes Base URL, endpoint, Request, Response and Header information etc.. Now I want to implement RESTful web service for these APIs. For that I am thinking of generating my Request and Response Java classes from this Yaml file and was looking for some kind of code generator, preferably a maven plugin/dependency which I could use in my Maven project. I came across this rest client with swagger which talks about using the swagger-codegen Maven plugin, but this is to generate the client which I believe is about generating the client code to consume these RESTful APIs, however my need is to generate classes to be used for service implementation. I will be using Java and Spring framework.
My question is what are the best practices for implementing the RESTful web services in Java when we have Yaml file (API spec created using Swagger editor) and which code generation tools/plugins are available to be used.
EDIT: Just came across this Server stub generator HOWTO, looking further into it.
Swagger-codegen maven plugin is a good option but I suggest you to use jhipster to generate your java project. It generates projects with latest tech stack including spring framework. You can select API-First development in your case. I used it and it was very efficient. You already have Yaml file. Put it in src/main/resources/swagger/api.yml and run
./mvnw generate-sources
All java codes will be generated.
Using swagger-codegen-maven plugin like the following
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.4.29</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${basedir}/src/main/resources/swagger/project.yaml</inputSpec>
<language>java</language>
<configOptions>
<sourceFolder>src/gen/java/main</sourceFolder>
<library>resteasy</library>
</configOptions>
</configuration>
</execution>
</executions>
or using openapi-generator-maven-plugin
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>6.3.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>
${project.basedir}/src/main/resources/swagger/project.yaml
</inputSpec>
<generatorName>spring</generatorName>
<apiPackage>where api package is to be rendered</apiPackage>
<modelPackage> model package </modelPackage>
<supportingFilesToGenerate>
Any supporting files needed </supportingFilesToGenerate>
<configOptions>
<delegatePattern>true</delegatePattern>
</configOptions>
</configuration>
</execution>
</executions>
Do you know a tutorial how to create a CXF soap web service from existing Java code and embed it in Tomcat, and also generate a wsdl file so that any .NET system would be able to generate client code easily?
I miss that WSDL creation point in, for example this
http://www.ibm.com/developerworks/library/ws-pojo-springcxf/
tutorial. No wsdl file is generated. But still it should be present in my case to provide system interoperability.
Do you know a tutorial how to create a CXF soap web service from existing Java code and embed it in Tomcat,
Embedding to Tomcat (to avoid using spring, opening own port): Servlet Transport
also generate a wsdl file
You java code 2 wsdl also maven plugin exists. But you can get the wsdl from working service by http://host:port/servicename?wsdl url and provide it ;)
To create wsdl file for the existing Java SOAP Service, you can use maven plugin. It will generated wsdl files on {project_home}/target/generated/wsdl/MyService.wsdl
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-java2ws-plugin</artifactId>
<version>${cxf.version}</version>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-simple</artifactId>
<version>${cxf.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>process-classes</id>
<phase>process-classes</phase>
<configuration>
<className>com.foo.MyService</className>
<genWsdl>true</genWsdl>
<verbose>true</verbose>
<frontend>jaxws</frontend>
<databinding>jaxb</databinding>
</configuration>
<goals>
<goal>java2ws</goal>
</goals>
</execution>
</executions>
</plugin>