I wanted to implement Jawr for minification and obfuscation of CSS and JS resources. I am using a Spring MVC project where the configurations are done using Java code using annotations and XML is not used. Now able to find a documentation for the same. Can some one suggest me the configuration or a reference link.
jawr dependencies to your pom:
<dependency>
<groupId>net.jawr</groupId>
<artifactId>jawr-core</artifactId>
<version>3.6</version>
</dependency>
<dependency>
<groupId>net.jawr.extensions</groupId>
<artifactId>jawr-spring-extension</artifactId>
<version>3.6</version>
</dependency>
Build a controller like this:
#Controller
public class JsController extends JawrSpringController
{
public JsController ()
{
setType("js");
setConfigLocation("/jawr.properties");
}
#RequestMapping("/js/**")
public void javascript ( HttpServletRequest request, HttpServletResponse response ) throws Exception
{
handleRequest(request, response);
}
}
Related
We had an integration tests such as the one that follows that used to work:
#ActiveProfiles("local")
#WithMockUser("j_unit_user_http_test")
#RunWith(SpringRunner.class)
#SpringBootTest(
classes = { Application.class },
webEnvironment = RANDOM_PORT
)
#Transactional
#Rollback
#AutoConfigureMockMvc()
public abstract class HttpTest {
static {
//reads and sets a dependency related props
PropertiesReader propertiesReader = new PropertiesReader();
propertiesReader.readDependencyProperties().forEach(System::setProperty);
}
#Autowired
private MockMvc mockMvc;
#PersistenceContext
private EntityManager em;
#Test
public void createDashboard() {
// POST is a utility method that wraps "mockMvc.perform(post(url))", I've omitted it here for brevity.
var postResult = POST("/api/dashboards", Map.of("name", "wonderland"));
var newDashboard = extractJson(postResult);
assertTrue(newDashboard.get("id").isInt());
}
}
Among the changes we made the significant ones that seem to be causing the errors are:
Upgrading spring-boot from '2.3.0' to '2.5.6'
Setting the environment properties needed by some of our dependencies in the static void main class of our app:
public class Application {
public static void main(String[] args) {
// reads and sets dependency related props
PropertiesReader propertiesReader = new PropertiesReader();
propertiesReader.readDependencyProperties().forEach(System::setProperty);
}
}
The error we get is:
java.lang.StackOverflowError
at java.base/java.lang.Throwable.getOurStackTrace(Throwable.java:828)
at java.base/java.lang.Throwable.getStackTrace(Throwable.java:820)
at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:55)
at ch.qos.logback.classic.spi.ThrowableProxy.<init>(ThrowableProxy.java:60) // several frames of this follow
My guess is that the mockMvc is not getting configured correctly, because if I use it without #SpringBootTest and #AutoConfigureMvc, the tests work. Any idea what could be wrong?
The above issue has been fixed.
In the commit when the tests started failing, among other changes, the spring-boot version was changed from 2.3.x to 2.5.x
Turns out in version 2.4 spring-boot removed JUnit 5's vintage engine.
As pointed out in their release notes, ideally, i should migrate the tests to junit 5, but, in the meantime, adding the following in build.gradle helps:
testImplementation("org.junit.vintage:junit-vintage-engine") {
exclude group: "org.hamcrest", module: "hamcrest-core"
}
Equivalent changes in pom.xml would be:
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
Using Apache-Camel ESB, trying to upload a xlsx file to Spring Rest Web application. Upload fails from apache-camel ESB. But upload works fine from Postman. Shared code snippets below.
Processor Code in Router of Camel looks like
from("file://data/PASInput").process(new Processor() {
#Override
public void process(Exchange exchange) throws Exception {
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
String filename = (String) exchange.getIn().getHeader(Exchange.FILE_NAME);
File file = exchange.getIn().getBody(File.class);
multipartEntityBuilder.addPart("file",
new FileBody(file, ContentType.MULTIPART_FORM_DATA, filename));
ByteArrayOutputStream out = new ByteArrayOutputStream();
multipartEntityBuilder.build().writeTo(out);
InputStream inputStream = new ByteArrayInputStream(out.toByteArray());
exchange.getOut().setBody(inputStream);
}
}).to("http://localhost:8080/Pastel/api/convertor/pas/pastel")
.log(LoggingLevel.ERROR, "RESPONSE BODY ${body}").end();
Pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>2.21.0.fuse-000077-redhat-1</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-cxf</artifactId>
<version>2.21.0.fuse-000077-redhat-1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-http</artifactId>
<version>2.21.0.fuse-000077-redhat-1</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-http4</artifactId>
<version>2.17.2</version>
</dependency>
Error
org.apache.camel.http.common.HttpOperationFailedException: HTTP operation failed invoking http://localhost:8080/Pastel/api/convertor/pas/pastel with statusCode: 500
at org.apache.camel.component.http.HttpProducer.populateHttpOperationFailedException(HttpProducer.java:274)
at org.apache.camel.component.http.HttpProducer.process(HttpProducer.java:183)
at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61)
at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:148)
at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548)
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
at org.apache.camel.processor.Pipeline.process(Pipeline.java:138)
at org.apache.camel.processor.Pipeline.process(Pipeline.java:101)
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
at org.apache.camel.component.file.GenericFileConsumer.processExchange(GenericFileConsumer.java:452)
at org.apache.camel.component.file.GenericFileConsumer.processBatch(GenericFileConsumer.java:219)
at org.apache.camel.component.file.GenericFileConsumer.poll(GenericFileConsumer.java:183)
at org.apache.camel.impl.ScheduledPollConsumer.doRun(ScheduledPollConsumer.java:174)
at org.apache.camel.impl.ScheduledPollConsumer.run(ScheduledPollConsumer.java:101)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
When we hit the webservice using postman, there are no errors. Able to upload the servers successfully. Spring mvc code,
#RequestMapping(value = "/pas/pastel", method = RequestMethod.POST)
#ResponseBody
public void convertPASToPastel(HttpServletRequest request, HttpServletResponse response,
#RequestParam(value = "file") final MultipartFile pasFile) {
try {
System.out.print("Here");
}
}
You can probably see this error message in your Spring backend log:
org.springframework.web.multipart.MultipartException: Current request is not a multipart request.
You need to set correct ContentType header. Please refer this similar question for solution, if you want to implement it in this way.
But you can get out this mess, if you switch co camel-http4 component (you already have this component in pom.xml). This component contains logic for converting HttpEntity to InputStream. Then you can set HttpEntity directly to exchange body.
Then your route will look something like this:
from("file://data/PASInput").process(new Processor() {
#Override
public void process(Exchange exchange) throws Exception {
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
String filename = exchange.getIn().getHeader(Exchange.FILE_NAME, String.class);
File file = exchange.getIn().getBody(File.class);
multipartEntityBuilder.addPart("file",
new FileBody(file, ContentType.MULTIPART_FORM_DATA, filename));
exchange.getOut().setBody(multipartEntityBuilder.build());
}
}).to("http4://localhost:8080/Pastel/api/convertor/pas/pastel")
.log(LoggingLevel.ERROR, "RESPONSE BODY ${body}").end();
And just a note. Never mix component versions, always use for components the same version as Apache Camel version. Otherwise you can see upredictable results. And why you have annotation #ResponseBody in Spring controller, when the method is void? You don`t need that.
I am trying to test google cloud storage in local mvn jetty server.
defining the following java servlet I get the following error during servlet initiallization.
#WebServlet(name = "receiveImage", value = "receiveImage")
#SuppressWarnings("serial")
#MultipartConfig()
public class receiveImage extends HttpServlet {
private static final String BUCKET_NAME = "testbucket";
private static Storage storage = null;
#Override
public void init() {
storage = StorageOptions.defaultInstance().service();
}
HTTP ERROR 500
Problem accessing /receiveImage. Reason:
Server Error
Caused by:
java.lang.IllegalAccessError: tried to access method com.google.cloud.ServiceOptions.getFromServiceLoader(Ljava/lang/Class;Ljava/lang/Object;)Ljava/lang/Object; from class com.google.cloud.HttpServiceOptions
at com.google.cloud.HttpServiceOptions.(HttpServiceOptions.java:154)
at com.google.cloud.storage.StorageOptions.(StorageOptions.java:69)
at com.google.cloud.storage.StorageOptions.(StorageOptions.java:27)
at com.google.cloud.sto
i define the following maven dependency in pom file:
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>0.4.0</version>
</dependency>
Appreciate for helps.
I could resolve the issue:
there is a conflict between these two dependencies:
dependency>
<groupId>com.google.cloud</groupId>
<artifactId>gcloud-java-datastore</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-storage</artifactId>
<version>0.5.1</version>
</dependency>
I just changed the first dependency to
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-datastore</artifactId>
<version>0.5.1</version>
</dependency>
I have a spring boot project that is structured like below:
client
build.gradle
interface
build.gradle
service
tests
main
java
resources
build.gradle
So there are three subprojects: interface,service and client.
The controllers are inside service>main>controller and the swagger is configured inside the build.gradle which is inside the service project.
When i do localhost:8080/swagger-ui.html ,i get:
No mapping found for HTTP request with URI [/swagger-ui.html] in DispatcherServlet with name 'dispatcherServlet'.
I am able to get json when i hit this url:
http://localhost:8080/v2/api-docs
I got this working,
I had a #EnableWebMVC on top of a class.Removing it fixed it.
Thanks #Sampada for your help
first, add below dependencies in spring boot pom file, then add #EnableSwagger annotation on the application class and then add webappconfig class
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.1</version>
</dependency>
#Configuration
#EnableWebMvc
public class ApplicationWebMvcConfig implements WebMvcConfigurer {
#Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer
configurer) {
configurer.enable();
}
}
I am working on AspectJ AOP implementation on spring MVC application. I have written Aspect java class where I am trying to intercept join points for all the methods of one of the packages say com.xyz.services. But AOP always failed to interecept the methods of that package. Aspect is defined as follows -
#Pointcut("execution(* com.xyz.services..*.*(..))")
public void logBefore() {
}
#Before("logBefore()")
public void logHere(JoinPoint joinPoint) {
System.out.println("In logHere ....");
logger.info("logBefore is running ....");
logger.info("hijacked ::::" + joinPoint.getSignature().getName());
logger.info("joinPoint.getSignature().getDeclaringTypeName() ::::"
+ joinPoint.getSignature().getDeclaringTypeName());
logger.info("joinPoint.getSignature().getModifiers() ::::"
+ joinPoint.getSignature().getModifiers());
logger.info("******************************************************");
}
I have enabled AOP in application-context.xml as follows -
<aop:aspectj-autoproxy proxy-target-class="true">
<aop:include name='loggingAspect' />
</aop:aspectj-autoproxy>
When ever I am calling webservice the methods in the com.xyz.services get called but the Aspect method never get called.
I tried with different pointcuts but the aspect code never did not execute.
#Pointcut("execution(public * com.xyz.services.ManagerServiceImpl.*(..))")
public void logBefore() {
System.out.println("In Logbefore");}
#Pointcut("execution(public * com.xyz.services.*.*(..))")
public void logBefore() {
System.out.println("In Logbefore");
}
I have added cglib dependency on pom.xml to enable cglib based proxies.
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
Can anyone help me out why this aspects are not working as expected?
have you configured correctly the class with the annotation ?
#EnableAspectJAutoProxy(proxyTargetClass = true)