JHipster - Removing # HASH from URL permanently - java

I can see all the urls in jhipster are having #, how to remove it properly, I don't want to show # in my application's urls, I created application in angular 4

We do have this tip in our docs although I have never tried it myself :
https://jhipster.github.io/tips/010_tip_configuring_html_5_mode.html
The tip is only for angularjs 1. You can try to combine it's advice with the official angular docs on routing : https://angular.io/guide/router
If you manage to make it work please do a PR on our website : https://github.com/jhipster/jhipster.github.io

I spent many hours to achieve this and finally has paid off. You can follow these steps:
Remove on angular app avery use of hash location strategy in every RouterModule.forRoot...:
{useHash: false}
Go to java app to the config folder and create a new java class to manage the error pages to redirect to index.html:
#Configuration
public class ErrorPageConfig implements ErrorPageRegistrar {
#Override
public void registerErrorPages(ErrorPageRegistry registry) {
registry.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/index.html"));
}
}
This page was very helpful
https://www.baeldung.com/spring-cloud-angular

Related

Customize contents in swagger-ui.html

I am using spring boot 2.1.6.RELEASE and swagger 2.9.2, everything is fine except that I want to simplify the content.
First, I want to remove the base URL under title:
[ Base URL: localhost:7777/ ]
http://localhost:7777/v2/api-docs
And, I want the API blocks and Models block to be opened on visiting, not until I click the name.
And, I want the select a spec list on the top banner to be removed or hidden.
I don't know if there is a way to do these with java API, I can't find any solution other place.
The picture I tried to upload:
Seems that I am not allowed uploading picture yet, don't blame me if the picture above is unavailable.
here i find a class helping to configure swagger UI:
springfox.documentation.swagger.web.UiConfiguration
and here is my usage:
#Bean
public UiConfiguration uiConfig() {
return UiConfigurationBuilder.builder()
.deepLinking(false)
.displayOperationId(false)
.defaultModelsExpandDepth(1)
.defaultModelExpandDepth(1)
.defaultModelRendering(ModelRendering.MODEL)
.displayRequestDuration(true)
.docExpansion(DocExpansion.LIST)
.filter(false)
.maxDisplayedTags(null)
.operationsSorter(OperationsSorter.METHOD)
.showExtensions(false)
.tagsSorter(TagsSorter.ALPHA)
.validatorUrl(null)
.build();
}

Play Framework: Images not accessible in production, despite trying the "route + custom action" solution

We have a web application comprising a client in Angular2 and a server in Play Framework 2.6. This application should allow users to upload their own images. Similarly to many users here, we faced the problem of not being able to access the images in public/assets in production mode, and so we tried the solutions that have been provided to those users, with no success.
We created the following route in the conf/routes file:
GET /files/*file controllers.AdminExerciseCtrl.serve(file)
leading to the following custom action:
public Result serve(String file) {
Boolean DEVELOPMENT = Boolean.parseBoolean(ConfigFactory.load().getString("development"));
String path;
if(DEVELOPMENT) path = "public/";
else path = ConfigFactory.load().getString("root/");
return ok(new java.io.File(path + file));
}
i.e., we are saving the uploaded images to root/files/images.
We tried two way of accessing this route:
Using #routes throws an error because of the # character
Using the route in the src field (where resource.resourcePath is a variable holding the path to the image file, e.g., images/pic.jpg), the route simply isn't found (404).
<img *ngIf="resource" class="thumbnail" [src]="'/files/' + resource.resourcePath">
We are starting to despair since this project is considerably large and uploading images is a core feature. Any ideas of what we could be missing? Anything we should be doing in the client regarding the routes?
Thanks in advance.

Play-Rest API with Java - basic response

I'm new in the server-side , and I'm trying to learn how to use play in rest api with java and restangular. I made a project for java in the intellij. I want the GET request to return an html page and not html.scala page.
how do I change this function that it will return the app/views/index.html instead the app/views/index.html.scala
also if someone have a good website to learn from, it will help a lot
the function in java :
package controllers;
import play.*;
import play.mvc.*;
import views.html.*;
public class Application extends Controller {
public static Result index() {
return ok(index.render("Your new application is ready."));
}
}
the routes page :
# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~
# Home page
GET / controllers.Application.index
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.at(path="/public", file)
Play use twirl as a template engine. I am not sure if you can easely change it to some other template engine but I think you can. Any way, as I see you are looking for a way just output simple HTML file. You can do this with default Assets controller:
this string in your route
GET /assets/*file controllers.Assets.at(path="/public", file)
would handle any files in the public. So if you will add to the public directory the simple /html/hello.html file then Play will render it by the url /assets/html/hello.html. You can change URL as you like in the rote file
I found a similar question with good answer with a lot of examples.
you can find it here.
The easiest way is to make a GET request for the / and to send the destination also, like this:
# Home page
GET / controllers.Assets.at(path="/public",file="index.html")

Netflix Zuul filters pulled from Cassandra not working

I have been using Zuul in combination with other Netflix OSS softs like Eureka, Hystrix, etc. At first, I was able to create my own filter and make it work from the FileSystem. Then, I wanted to use ZuulFilterDAOCassandra API to be able to pull filters from a Cassandra database. The problem is that I'm not able to make it work.
I have translated the Java filter I used from the FileSystem to a Groovy filter, and added it to Cassandra using the ZuulFilterDAO.addFilter() method, and activate it using ZuulFilterDAO.setFilterActive().After that, I started the ZuulFilterPoller to start pulling the filters from the database, if it already had any. For the database table model, I assumed it by looking at the FilterInfo object and its attributes, and from the ZuulFilterDAOCassandra.addFilter()method.
Here's a more complete section of code :
ZuulFilterDAO dao = new ZuulFilterDAOCassandra(keyspace);
try {
FilterInfo fi = dao.addFilter(code, "pre", "testFilter", "false", "1");
dao.setFilterActive(fi.getFilterName(), 1); // 1 is the filter revision
ZuulFilterPoller.start(dao);
} catch (Exception e) {
// Logging
}
// Show every active filter
List<FilterInfo> allActiveFilters = dao.getAllActiveFilters();
for (FilterInfo fi : allActiveFilters) {
LOG.info("FilterName: " + fi.getFilterName()); // outputs : FilterName: testFilter
}
In the Zuul console, I also get the following :
adding filter to diskFilterInfo{filter_id='zuul-server:testFilter:pre',
filter_name='testFilter', filter_type='pre', revision=1, creationDate=/*date*/,
isActive=true, isCanary=false, application_name=zuul-server}
filter written testFilter.groovy
I can then edit the testFilter.groovy at the root of my project, which contains the code I have mentioned in the dao.addFilter() method. I know the code is working, since it worked when pulling filters from the FS. And, when adding a filter, there's a verification of the code done by FilterVerifier.
And when I send my http requests to my application, Zuul doesn't filter them anymore. Am I missing something ?
Try using the FilterFileManager. It's my understanding that ZuulFilterPoller saves the filters from Cassandra to disk, and the FilterFileManager puts them to work.

Thymeleaf and Static Resources with fingerprint

I have tried to add fingerprint suffix to some of my static files (Ex. app.min.js / style.min.css) using ResourceResolvers and ResourceTransformers
http://spring.io/blog/2014/07/24/spring-framework-4-1-handling-static-web-resources
I have config the ResourceHandler like this
#Configuration
#EnableWebMvc
public class ResourceResolverConfiguration extends WebMvcConfigurerAdapter {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("bower_components/**/*.js", "/assets/**", "/build/**")
.addResourceLocations("/bower_components/", "/assets/", "/build/","classpath:/META-INF/webapp/build/")
.resourceChain(true)
.addResolver(
new VersionResourceResolver()
.addContentVersionStrategy("/**")
);
}
}
and in my main controller I have add some debug log.
logger.debug("js = '{}'" + this.resourceUrlProvider.getForLookupPath("/build/app.js"));
logger.debug("css = '{}'" + this.resourceUrlProvider.getForLookupPath("/build/styles/style.css"));
After run the web application, from the debug log, there is fingerprint in each file like
app-5d2c76ad6517f26d252d5cc93a4fc7d2.js
and I can access this file directly, (ie. via localhost:8080/build/app-5d2c76ad6517f26d252d5cc93a4fc7d2.js)
However, when I click view source at the web browser, it is still an original file without any fingerprint.
which in my layout.html I load the script/link like this.
<script th:src="#{/build/app.js}"></script>
<link th:href="#{/build/styles/style.css}" rel="stylesheet"></link>
I use Thymeleaf for template engine.
What should the configuration or code be to make Thymeleaf include the fingerprint files or did I miss something ?
Thank you very much.
Make sure you're including the appropriate encoding filter, which is what the #{} syntax uses to rewrite URLs:
#Bean
public ResourceUrlEncodingFilter resourceUrlEncodingFilter() {
return new ResourceUrlEncodingFilter();
}
Try this to render the fingerprint along with the filename
<script th:src="${#mvcResourceUrlProvider.getForLookupPath('/build/app.js')}" />
The above results in:
<script src="/build/app-16e85c31092c68733df3c729b831dcfd.js"></script>
I know this is late, but I was looking for the same answer for how to render urls to work with addContentVersionStrategy and this came up, so hopefully it helps someone else.

Categories