Getting 415 Unsupported Media Type in spring boot - spring-boot

Method in controller
#RequestMapping(value = "/reactEmployee", method = RequestMethod.POST, consumes = "application/json")
public String addEmployeereact(#RequestBody EmployeeEntity employeeEntity)
{
employeeManager.addEmployee(employeeEntity);
return "redirect:/";
}
Json input from Postman
Json input and 415 error in Postman

After looking at github link I found that jackson needs to be specified in pom.xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.10.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.10.1</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.12.4</version>
</dependency>

Related

method level custom annotation doesn't work spring boot

I am trying to define custom annotation(LogMe) that should run before and after methods that are decorated with the annotation.
The annotation works fine for the spring identified methods - the ones defined with #GetMapping etc. however, the annotation on my custom written methods doesn't invoke AOP.
I have defined annotation as follows:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
#Target(ElementType.METHOD)
#Retention(RetentionPolicy.RUNTIME)
public #interface LogMe {
My Aspect is like this:
#Configuration
#EnableAspectJAutoProxy
#Aspect
public class LogMeAspect {
#Pointcut("#annotation(com.api.logging.aspect.LogMe)")
public void loggableMethods() {}
#Around("loggableMethods()")
public Object serviceResponseTimeAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println(">>>>>>>>>>>REACHING TO PJP:"+joinPoint.getSignature().getName());
Object obj = joinPoint.proceed();
return obj;
}
}
spring.factories defined as follows (I am trying to access this aspect from dependency).
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.api.logging.aspect.LoggingAspect,\
com.api.logging.aspect.LogMeAspect
usage of the annotation:
public class Utils {
#LogMe
public String testing(int i, int j, String str) {
System.out.println("in testing");
testing2();
return i+j+str;
}
#LogMe
public void testing2() {
System.out.println("in testing 2");
}
I have the following dependencies for my aspect module.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>2.4.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.5</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>1.18.20</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.9.6</version>
</dependency>
<dependency>
<groupId>ch.qos.logback.contrib</groupId>
<artifactId>logback-json-classic</artifactId>
<version>${logback.contrib.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback.contrib</groupId>
<artifactId>logback-jackson</artifactId>
<version>${logback.contrib.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.12.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.12.3</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.7.4</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>6.4</version>
</dependency>
I have following dependencies for the module from where I am calling the aspect. of course, one of it is the aspect dependency - spring-boot-api-logging
<!-- core -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- matrix -->
<!-- kafka -->
<!-- tests -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- database -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- logging-->
<dependency>
<groupId>org.api.commons.logging</groupId>
<artifactId>spring-boot-api-logging</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>

How to setup Swagger with Spring Boot Camel for REST messaging

I'm following Swagger Java example, but can't make it work with Spring Boot Camel.
I'm running Spring Boot Camel 3.4.0, and have next dependencies in pom.xml:
<!-- Spring Boot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- Camel -->
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-stream-starter</artifactId>
</dependency>
<!-- REST -->
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-rest-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-servlet-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-jackson-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-jaxb-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-netty-http-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-http-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-jetty-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-undertow-starter</artifactId>
</dependency>
<!--<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-rest-swagger-starter</artifactId>
</dependency>-->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-swagger-java</artifactId>
<version>3.4.0</version>
</dependency>
My Router.java is next:
String listenAddress = "192.168.0.100";
int listenPort = 8080;
restConfiguration()
.component("netty-http")
.scheme("http")
.host(listenAddress)
.bindingMode(RestBindingMode.auto)
.dataFormatProperty("prettyPrint", "true")
.port(listenPort)
.contextPath("/")
// add swagger api-doc out of the box
.apiContextPath("/api-doc")
.apiProperty("api.title", "User API").apiProperty("api.version", "1.2.3")
// and enable CORS
.apiProperty("cors", "true");
// this user REST service is json only
rest("/user").description("User rest service")
.consumes("application/json").produces("application/json")
.get("/{id}").description("Find user by id").outType(User.class)
.param().name("id").type(path).description("The id of the user to get").dataType("int").endParam()
.log("Swagger REST header id: ${header.id}");
If trying to GET http://192.168.0.100:8080/api-doc I'm getting 404.
This route above should print log in Camel terminal when using REST GET with http://192.168.0.100:8080/user/123 or am I wrong? Can't see what's missing.
The context path by default is set to /camel/, which means if rest of your configuration is correct, you should be able to see your api-docs at http://192.168.0.100:8080/camel/api-doc
To override it, you need to set the following property in your application.properties file.
camel.component.servlet.mapping.context-path= /*
For me adding the configuration :
In routes
String listenAddress = "localhost";
int listenPort = 8003;
restConfiguration()
.component("servlet")
.scheme("http")
.host(listenAddress)
.bindingMode(RestBindingMode.auto)
.dataFormatProperty("prettyPrint", "true")
.port(listenPort)
.contextPath("/")
// add swagger api-doc out of the box
.apiContextPath("/api-doc")
.apiProperty("api.title", "User API").apiProperty("api.version", "1.2.3")
// and enable CORS
.apiProperty("cors", "true");
In application.properties
server.port=8003
camel.component.servlet.mapping.context-path=/**
URI
http://localhost:8003/api-doc
I am using camel version : 3.9.0
This solution works fine !!!!

Forward to JSP within Spring Controller after form submission

Using Spring #Controller, #RequestMapping and #ModelAttribute, I'd like to achieve a basic form submission flow in which the user is forwarded to a new JSP with attributes set. Spring provides different ways to achieve this, but I have received various errors.
Example 1
Based on tutorial: https://www.baeldung.com/spring-mvc-form-tutorial
form.html
<form action="/submitForm" method="POST">
<input type="text"id="field1" name="field1">
<!-- other input fields -->
<button type="submit">Submit</button>
</form>
success.jsp
<p>Thanks for signing up ${userName}!!</p>
MyController.java
#Controller
public class MyController{
#RequestMapping(
value = "/submitForm",
method = RequestMethod.POST,
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public String post(#ModelAttribute SignupRequest request, ModelMap model){
// At this point, the SignupRequest is populated correctly
model.addAttribute("userName", request.getUserName());
return "success";
}
}
Results
Using return "success" - the result is HTTP 404 Not Found
Using return "success.jsp", the result is HTTP 405 Request method
'POST' not supported
Using return "redirect:/success.jsp", the client is redirected,
but attributes are not set, and ${userName} is visible.
Example 2
Based on the accepted answer here: Redirect after POST method in spring MVC
MyController.java
#Controller
public class MyController{
#RequestMapping(
value = "/submitForm",
method = RequestMethod.POST,
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public ModelAndView post(#ModelAttribute SignupRequest request){
// At this point, the SignupRequest is populated correctly
ModelAndView mAV = new ModelAndView("redirect:/success.jsp");
mAV.addObject("userName", request.getUserName());
return mAV;
}
}
Result
the client is redirected, but attributes are not set, and ${userName} is visible.
What is the correct way to do this?
Thanks!
EDIT
Additional details
Using SpringBoot with embedded Tomcat. JSP file located in src>main>resources>public. The raw JSP is being served. I believe the project is not treating JSP as it should. Adding POM deps.
<dependencies>
<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>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-email</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.2.11</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.23.1-GA</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
#Controller
public class MyController{
#RequestMapping(
value = "/submitForm",
method = RequestMethod.POST,
consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public RedirectView post(#ModelAttribute SignupRequest request, RedirectAttributes ra){
// At this point, the SignupRequest is populated correctly
RedirectView rw = new RedirectView();
rw.setUrl("success.jsp");
ra.addFlashAttribute("userName", request.getUserName());
return rw;
}
}

Spring Boot attributes of the Model are not loaded on JSP page

I'm desperate, i'm following all the tutorials and the documentations, and i succesfully create a little hello controller with spring boot, but when i'm try to use spring boot on my project, all the attributes of the spring Model/Modelview are not loaded and i don't know why.
Here my pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
<groupId>com.github.p4535992</groupId>
<artifactId>springMVC12</artifactId>
<version>1.6.10</version>
<packaging>war</packaging>
<name>springMVC12</name>
<description>Demo project for Spring Boot using JSPs</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<!-- Generic properties -->
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.outputEncoding>UTF-8</project.build.outputEncoding>
<java.version>1.8</java.version>
<!-- Version of the Maven properties -->
<maven-eclipse-plugin.version>2.9</maven-eclipse-plugin.version>
<maven-compiler-plugin.version>2.3.2</maven-compiler-plugin.version>
<maven-source-plugin.version>2.3</maven-source-plugin.version>
<maven-javadoc-plugin.version>2.3</maven-javadoc-plugin.version>
<maven-war-plugin.version>2.6</maven-war-plugin.version>
<maven-exec-plugin.version>1.1.1</maven-exec-plugin.version>
<maven-tomcat7-plugin.version>2.2</maven-tomcat7-plugin.version>
<maven-dependency-plugin.version>2.10</maven-dependency-plugin.version>
<mysql.version>5.1.6</mysql.version>
<javax.servlet.jstl.version>1.2</javax.servlet.jstl.version>
<!-- Json-->
<json-path.version>2.0.0</json-path.version>
<com.fasterxml.jackson.core.version>2.5.3</com.fasterxml.jackson.core.version>
<!-- Logging -->
<spring-boot-starter-logging.version>1.2.4.RELEASE</spring-boot-starter-logging.version>
<!-- Test -->
<junit.version>4.12</junit.version>
<com.github.dandelion.version>0.10.1</com.github.dandelion.version>
<!-- Apache and Commons -->
<commons-fileupload.version>1.3.1</commons-fileupload.version>
<commons-io.version>2.4</commons-io.version>
<commons-codec.version>1.10</commons-codec.version>
<!-- UTILITY Github -->
<com.github.p4535992.utility.version>1.6.10</com.github.p4535992.utility.version>
<!--<com.github.p4535992.gate-basic.version>1.6.5</com.github.p4535992.gate-basic.version>-->
<com.github.p4535992.ExtractInfo.version>1.6.10</com.github.p4535992.ExtractInfo.version>
<start-class>com.github.p4535992.mvc.JspDemoApplication</start-class>
<!-- <tomcat.version>7.0.52</tomcat.version>-->
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.github.p4535992</groupId>
<artifactId>extractInfo</artifactId>
<version>${com.github.p4535992.ExtractInfo.version}</version>
<exclusions>
<exclusion>
<groupId>com.github.p4535992</groupId>
<artifactId>utility</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>*</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.p4535992</groupId>
<artifactId>utility</artifactId>
<version>${com.github.p4535992.utility.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>*</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!-- Other Spring boot dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!--<scope>provided</scope>-->
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<!-- <scope>provided</scope>-->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<!-- <scope>provided</scope>-->
</dependency>
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>-->
<!--<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>-->
<!-- Github dependency -->
<dependency>
<groupId>com.github.p4535992</groupId>
<artifactId>extractInfo</artifactId>
</dependency>
<dependency>
<groupId>com.github.p4535992</groupId>
<artifactId>utility</artifactId>
</dependency>
<!-- MySQLDatabase -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- ===================== -->
<!-- Needed for JSON View -->
<!-- ===================== -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${com.fasterxml.jackson.core.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${com.fasterxml.jackson.core.version}</version>
</dependency>
<!-- Need for get json response with controller -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${com.fasterxml.jackson.core.version}</version>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>${json-path.version}</version>
<scope>test</scope>
</dependency>
<!-- Dandelion -->
<dependency>
<groupId>com.github.dandelion</groupId>
<artifactId>datatables-jsp</artifactId>
<version>${com.github.dandelion.version}</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- Apache and Commons -->
<!-- NOTE: commons-dbcp replace from tomcat-jdbc -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>${commons-fileupload.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<!-- Needed for PDF View -->
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>4.2.1</version>
</dependency>
<!-- Needed for XLS View -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.10-beta2</version>
</dependency>
<!-- Needed for RSS View-->
<dependency>
<groupId>com.rometools</groupId>
<artifactId>rome</artifactId>
<version>1.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- ****************************************-->
<!-- Copy dependency jar to a m2 folder -->
<!-- ****************************************-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${user.dir}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Here my MapController.java:
#Controller
public class MapController {
private static final org.slf4j.Logger logger =
org.slf4j.LoggerFactory.getLogger(MapController.class);
#Autowired
private MapService mapService;
#Autowired
private FileService fileService;
private Marker marker;
private List<Marker> arrayMarker = new ArrayList<>();
private String arrayMarker2 ="";
//List<Marker> supportArray = new ArrayList<>();
private Integer indiceMarker = 0;
/*#RequestMapping(value="/map",method= RequestMethod.GET)
public String loadMap1(Model model){
String html = mapService.getResponseHTMLString();
return "riconciliazione2/mappa/map";
}
#RequestMapping(value="/map",method = RequestMethod.POST)
public String result(#RequestParam(required=false, value="urlParam")String url,Model model){
System.out.println("url: " + url);
return "home";
}*/
#RequestMapping(value="/map",method= RequestMethod.GET)
public String loadMap2(Model model){
//String html = mapService.getResponseHTMLString();
//Site siteForm = new Site();
//model.addAttribute("siteForm",siteForm);
if(!arrayMarker.isEmpty()) model.addAttribute("arrayMarker",arrayMarker);
else model.addAttribute("arrayMarker",null);
arrayMarker2 = JsonUtilities.writeListToJsonArray(arrayMarker);
if(arrayMarker2 != null) model.addAttribute("arrayMarker2",arrayMarker2);
else model.addAttribute("arrayMarker2","");
if(marker!=null)model.addAttribute("marker",marker);
else model.addAttribute("marker",null);
model.addAttribute("indiceMarker",indiceMarker);
model.addAttribute("urlParam",null);
String html = mapService.getResponseHTMLString();
model.addAttribute("HTML",html);
return "riconciliazione2/mappa/leafletMap";
}
/*#RequestMapping("*")
public String hello(HttpServletRequest request,Model model) {
System.out.println(request.getServletPath());
String MAIN = mapService.homeMain();
model.addAttribute("MAIN",MAIN);
return "main";
}*/
#RequestMapping("/")
public String homeMain(Model model){
return "main";
}
#RequestMapping(value="/main",method= RequestMethod.GET)
public String homeMain2(Model model){
//String MAIN = mapService.homeMain();
//model.addAttribute("MAIN",MAIN);
return "main";
}
//---------------------------------------------------------
// NEW GET METHOD
//---------------------------------------------------------
#RequestMapping(value="/map13",method= RequestMethod.GET)
public String loadMap13(){
Model model = new ExtendedModelMap();
//String html = mapService.getResponseHTMLString();
//Site siteForm = new Site();
//model.addAttribute("siteForm",siteForm);
if(!arrayMarker.isEmpty()) model.addAttribute("arrayMarker",arrayMarker);
else model.addAttribute("arrayMarker",null);
arrayMarker2 = JsonUtilities.writeListToJsonArray(arrayMarker);
if(arrayMarker2 != null) model.addAttribute("arrayMarker2",arrayMarker2);
else model.addAttribute("arrayMarker2",null);
if(marker!=null)model.addAttribute("marker",marker);
else model.addAttribute("marker",null);
model.addAttribute("indiceMarker",indiceMarker);
model.addAttribute("urlParam",null);
//String html = mapService.getResponseHTMLString();
//model.addAttribute("HTML",html);
//model.addAttribute("supportArray",supportArray);
return "riconciliazione2/mappa/leafletMap5";
}
//---------------------------------------------------------
// NEW POST METHOD
//---------------------------------------------------------
#RequestMapping(value="/map3",method = RequestMethod.POST)
public String result4(#RequestParam(required=false, value="urlParam")String url,
#RequestParam(required=false,value="arrayParam")List<String> arrayParam
//#ModelAttribute(value="arrayParam") MarkerList arrayParam
//#ModelAttribute(value="markerParam")Marker markerFromJS
){
if(arrayParam!= null && !arrayParam.isEmpty()){
for(String smarker : arrayParam){
if(StringUtilities.isNullOrEmpty(smarker))continue;
marker = new Marker();
try {
marker = JsonUtilities.fromJson(smarker,Marker.class);
} catch (IOException ioe) {
ioe.printStackTrace();
}
arrayMarker.add(marker);
indiceMarker++;
}
}
if(!StringUtilities.isNullOrEmpty(url)) {
String[] splitter;
if (url.contains(",")) {
splitter = url.split(",");
url = splitter[0];
}
System.out.println("url: " + url);
marker = new Marker();
marker = mapService.createMarkerFromGeoDocument(url);
// = new Marker("City",url,"43.3555664", "11.0290384");
//model.addAttribute("marker",marker); //no need is get from the HTTTP GET COMMAND
arrayMarker.add(marker);
indiceMarker++;
}
return "redirect:/map13";
}
Here the link to my JSP page: JSPPage
I don't know why but the attribute arrayMarker2 is always empty, and that give me nuts.
You can find the full code of the project to this link springMVC12.
Ty in advance for any help.
Ok, i solved by myself but i not sure how.
I just replace all the Model of springframework with the ModelAndView Object.

jersey skip filtering on specific resource

I have REST API application running in Jetty + Jersey, all the requests will be filtered by some security filter,which is provided some excluded paths defined in config file. With old version Jersey 2.5 the API works fine, now i upgraded Jersey to 2.22.1, the excluded paths are subjected to filtering too and failed because no authentication token were provided.
here is my filter:
#Provider
#Priority(Priorities.AUTHENTICATION)
public class AuthenticationFilter extends AbstractAuthenticationFilter implements ContainerRequestFilter, DynamicFeature {
...
#Override
public void filter(ContainerRequestContext requestContext) throws IOException {
if (getExcludeUrlPatterns() != null) {
for (String urlPattern : getExcludeUrlPatterns()) {
Pattern p = Pattern.compile(urlPattern);
Matcher m = p.matcher(requestContext.getUriInfo().getPath()); <-- here went wrong
if (m.matches()) {
// skip filtering
}
}
}
// continue filtering.
}
}
here is my resource:
#Path("/version")
public class VersionResource {
...
#GET
#ResponseClass(AppVersion.class)
public Response getVersion() {
AppVersion version =
new AppVersion()
.withBuildPlan(BUILD_PLAN_SETTING)
.withBuildNumber(BUILD_NUMBER_SETTING)
.withBuildTimestamp(BUILD_TIMESTAMP_SETTING)
return Response.ok(version).build();
}
}
Excluded url paths:
/status,/status/?.*,/version,..
as I debugged the code, i noticed for an excluded request context "/myresources/version", requestContext.getUriInfo().getPath() return 'version', however the Pattern is like '/version', thus it doesn't match the pattern and continue the authentication process, and further failed. anyone understand why it works like this?
I use Jersey 2.22.1 and jetty-maven-plugin 9.2.1.v20140609, here is my pom.xml
<jersey.version>2.22.1</jersey.version>
<jetty.version>9.2.1.v20140609</jetty.version>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.connectors</groupId>
<artifactId>jersey-jetty-connector</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.connectors</groupId>
<artifactId>jersey-apache-connector</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-webapp</artifactId>
<version>${jetty.version}</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>${jetty.version}</version>
</dependency>

Resources