Spring JSON request: HTTP Status 406 - json

I know, this popped up repeatedly on stackoverflow. Unfortunately none of the solutions provided solved my problem.
I am trying to have my server respond with with Data in JSON format in the simplest possible way, using Jackson message converter.
My configuration:
My pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>whatever</artifactId>
<name>whatever</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
<java-version>1.7</java-version>
<org.springframework-version>3.1.1.RELEASE</org.springframework-version>
<org.aspectj-version>1.6.10</org.aspectj-version>
<org.slf4j-version>1.6.6</org.slf4j-version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
...
<!-- Jackson Mapper for JSON response -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.5</version>
</dependency>
...
</dependencies>
<build>
...
</build>
</project>
jackson-mapper-asl-1.9.5.jar and jackson-core-asl-1.9.5.jar are on the build path.
My /whatever/src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<context:component-scan base-package="com.mycompany.whatever" />
<annotation-driven />
...
</beans:beans>
My controller:
package com.mycompany.whatever;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
#Controller
public class AParticularUserController implements UserController {
#RequestMapping(value = "/demo/sayhello/{name}", method = RequestMethod.GET)
public #ResponseBody GreeterDemo sayHello(#PathVariable String name) {
return new GreeterDemo(name);
}
}
GreeterDemo is a very simple Demo class:
package com.mycompany.whatever;
public class GreeterDemo {
private String name;
public GreeterDemo(String name) {
this.name = name;
}
}
When I make a GET request against my development Server (Tomcat v7.0) http://localhost:8080/whatever/demo/sayhello/SomeName I always receive the error message:
HTTP Status 406 -
type Status report
message
description The resource identified by this request is only capable of
generating responses with characteristics not acceptable according to
the request "accept" headers.
Apache Tomcat/7.0.37
The request parameters seem to be correct though:
Accept:application/json
Accept-Charset:UTF-8,*;q=0.5
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-GB,en;q=0.8,de-DE;q=0.6,de;q=0.4,fr-FR;q=0.2,fr;q=0.2
Connection:keep-alive
Host:localhost:8080
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.70 Safari/537.17
respectively
Accept:*/*
Accept-Charset:UTF-8,*;q=0.5
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-GB,en;q=0.8,de-DE;q=0.6,de;q=0.4,fr-FR;q=0.2,fr;q=0.2
Connection:keep-alive
Host:localhost:8080
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.70 Safari/537.17
Any ideas...?

The issue is that the GreeterDemo type is missing getters(based on discussions in the comments to the question) and Jackson refuses to accept it as a valid type that it can convert into json, once the getter is added the conversion to json proceeds as expected.

Related

Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required. The problem I cannot solve

My question :
When I didn't use the #MapperScan in the Application Boot class, it has this kind of problems:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.reggie.mapper.EmployeeMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1812) ~[spring-beans-6.0.3.jar:6.0.3]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1371) ~[spring-beans-6.0.3.jar:6.0.3]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1325) ~[spring-beans-6.0.3.jar:6.0.3]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:709) ~[spring-beans-6.0.3.jar:6.0.3]
... 34 common frames omitted
When I add the #MapperScan, the problem changes to this
Caused by: java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required
at org.springframework.util.Assert.notNull(Assert.java:204) ~[spring-core-6.0.3.jar:6.0.3]
at org.mybatis.spring.support.SqlSessionDaoSupport.checkDaoConfig(SqlSessionDaoSupport.java:122) ~[mybatis-spring-2.0.5.jar:2.0.5]
at org.mybatis.spring.mapper.MapperFactoryBean.checkDaoConfig(MapperFactoryBean.java:73) ~[mybatis-spring-2.0.5.jar:2.0.5]
at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44) ~[spring-tx-6.0.3.jar:6.0.3]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1797) ~[spring-beans-6.0.3.jar:6.0.3]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1747) ~[spring-beans-6.0.3.jar:6.0.3]
... 44 common frames omitted
My pom is
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>reggie</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>reggie</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>16</source>
<target>16</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
My Service is
package com.example.reggie.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.reggie.entity.Employee;
public interface EmployeeService extends IService<Employee> {
}
My service implementation is
package com.example.reggie.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.reggie.entity.Employee;
import com.example.reggie.mapper.EmployeeMapper;
import com.example.reggie.service.EmployeeService;
import org.springframework.stereotype.Service;
#Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
}
My controller is
package com.example.reggie.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.reggie.common.R;
import com.example.reggie.entity.Employee;
import com.example.reggie.service.EmployeeService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#Slf4j
#RestController
#RequestMapping("/employee")
public class EmplyeeController {
#Autowired
private EmployeeService employeeService;
/**
*
* #param request
* #param employee
* #return
*/
#PostMapping("/login")
public R<Employee> login(HttpServletRequest request , #RequestBody Employee employee){
String passwaord=employee.getPassword();
passwaord=DigestUtils.md5DigestAsHex(passwaord.getBytes());
LambdaQueryWrapper<Employee> queryWrapper=new LambdaQueryWrapper<>();
queryWrapper.eq(Employee::getUsername,employee.getUsername());
Employee emp=employeeService.getOne(queryWrapper);
if (emp==null){
return R.error("Failed");
}
if (!emp.getPassword().equals(passwaord)){
return R.error("Failed");
}
if (emp.getStatus()==0){
return R.error("Forbiidden.");
}
request.getSession().setAttribute("employee",emp.getId());
return R.success(emp);
}
}
I don't know why.Is this the problem between Mybaties and SpringBoot?
I hope to know that the reason of this problem.
I got the same problem, and i saw the answer by ave, i realized the version maybe not been supported any more for my Java17, so i changed the mybatis-spring-boot-starter from 2.2.1 to 3.0.1. Then the problem was solved.
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.1</version>
</dependency>

How do you run static content for using ( SpringBoot 2.x + weblogic + *.war ) from a classpath an application?

I have a weblogic server that I run into Inlellij Idea for audit work of an application.
I made web-app based on SpringBoot 2.x. I am using Intellij Idea 2019 and there I configure a deploy my webapplication on weblogic 12.x.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.weblogic</groupId>
<artifactId>weblogic</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>weblogic</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<version.apache.maven.plugins>3.8.1</version.apache.maven.plugins>
<version.mapstruct>1.3.0.Final</version.mapstruct>
<version.apache.common.lang3>3.9</version.apache.common.lang3>
<version.apache.commons.text>1.8</version.apache.commons.text>
<version.apache.commons.beanutils>1.9.4</version.apache.commons.beanutils>
<version.datasource.proxy>1.6</version.datasource.proxy>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<!--исключаем из компиляции встроенный Tomcat-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<archive>
<manifest>
<addDefaultImplementationEntries>false</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
src/main/webapp/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
</web-app>
src/main/webapp/WEB-INF/weblogic.xml
<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-web-app xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-web-app"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.oracle.com/weblogic/weblogic-web-app
http://xmlns.oracle.com/weblogic/weblogic-web-app/1.9/weblogic-web-app.xsd">
<wls:weblogic-version>12.2.1.4.0</wls:weblogic-version>
<wls:context-root>/api</wls:context-root>
<wls:container-descriptor>
<wls:prefer-application-packages>
<wls:package-name>org.slf4j.*</wls:package-name>
<wls:package-name>org.springframework.*</wls:package-name>
<wls:package-name>javax.persistence.*</wls:package-name>
</wls:prefer-application-packages>
</wls:container-descriptor>
</wls:weblogic-web-app>
src/main/webapp/index.html
When I run weblogic by Intellij Idea I get index.html by url - http://localhost:7001/api
So, It works.
But I need more complex static content and I want to place it into an application classpath.
I made so…
src/main/resources/templates/pages/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello Weblogic!!!</title>
<link type="text/css" href="/static/css/style.css" rel="stylesheet"/>
</head>
<body>
<div class="st">
Hello Weblogic!!!
</div>
</body>
</html>
src/main/resources/static/css/style.css
.st{
color: red;
font-size: 35px;
}
And then…
src/main/java/com/weblogic/weblogic/mvc/config/MyMvcConfig.java
package com.weblogic.weblogic.mvc.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
#Configuration
public class MyMvcConfig implements WebMvcConfigurer {
#Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(
"/static/css/**",
"/templates/pages/**")
.addResourceLocations(
"classpath:/",
"classpath:/static/css/",
"classpath:/templates/pages/");
}
}
But I get … Error 500--Internal Server Error
java.lang.NullPointerException at
weblogic.servlet.internal.ServletResponseImpl.sendContentError(ServletResponseImpl.java:738)
at
weblogic.servlet.internal.ServletResponseImpl.sendError(ServletResponseImpl.java:796)
at
weblogic.servlet.internal.ServletResponseImpl.sendError(ServletResponseImpl.java:713)
at
org.springframework.boot.web.servlet.support.ErrorPageFilter$ErrorWrapperResponse.sendErrorIfNecessary(ErrorPageFilter.java:349)
at
org.springframework.boot.web.servlet.support.ErrorPageFilter$ErrorWrapperResponse.getWriter(ErrorPageFilter.java:363)
at
org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration$StaticView.render(ErrorMvcAutoConfiguration.java:227)
at
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1373)
at
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1118)
at
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1057)
at
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
at
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
who has any ideas how to fix this ?
You must use "template engine" – Thymeleaf. It doesn't work any other way (I would like to use pure (html + bootstrap), but it's not clear how to configure it).
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>web.spring.boot.jar</groupId>
<artifactId>web-spring-boot-jar</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>web-spring-boot-jar</name>
<packaging>war</packaging>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
The structure of the static resources
src/main/resources/static/css/style.css
src/main/resources/templates/index.html
src/main/resources/templates/pages/login/login.html
*.properties
src/main/resources/application.properties
These settings don't work.
spring.mvc.servlet.path=/api
server.servlet.context-path=/api
server.servlet.contextPath=/api
These settings could not be applied.
Configuration files - *.xml
src/main/webapp/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
</web-app>
If this file is missing, then the deployment occurs with errors ( in the development environment, you can see this in the console).
src/main/webapp/WEB-INF/weblogic.xml
<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-web-app
xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-web-app"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
https://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd
http://xmlns.oracle.com/weblogic/weblogic-web-app
https://xmlns.oracle.com/weblogic/weblogic-web-app/1.9/weblogic-web-app.xsd">
<wls:context-root>rst</wls:context-root>
<wls:container-descriptor>
<wls:prefer-application-packages>
<wls:package-name>org.slf4j</wls:package-name>
</wls:prefer-application-packages>
</wls:container-descriptor>
</wls:weblogic-web-app>
<wls:context-root>rst</wls:context-root>
Here you need to be careful with choosing a name for the root context of the application (for example, if you chose a name for the context - /web, - > there will be an error, since this name is reserved somewhere).
Entry point
#SpringBootConfiguration
#EnableAutoConfiguration
#ComponentScan
public class WebSpringBootJarApplication extends SpringBootServletInitializer implements WebApplicationInitializer {
public static void main(String[] args) {
SpringApplication.run(WebSpringBootJarApplication.class, args);
}
#Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(WebSpringBootJarApplication.class);
}
}
#SpringBootConfiguration
#EnableAutoConfiguration
#ComponentScan
You must have these 3 annotations, otherwise nothing will work.
The documentation says that #SpringBootConfiguration, replaces annotations:
#Configuration,
#EnableAutoConfiguration
#ComponentScan
#SpringBootConfiguration - indicates that this class provides an application
SpringBoot
It can also be used as an alternative to the #Configuration annotation, because the configuration can be found automatically.
#EnableAutoConfiguration - enables automatic configuration of Spring ApplicationContext by scanning class path components and registering beans that match different conditions.
That is, this annotation tries to guess and configure the beans that you may need.
Auto-configuration of classes, usually applied based on classpath
your application and on those beans that you have defined.
When using the #SpringBootConfiguration annotation, the context configuration is enabled automatically and the annotation is added, which will not have any additional effect…
But what I see here is quite different…
Without this annotation, nothing works... That is, we don't get the requested resources.
#ComponentScan - Directive configures component scanning for the classes marked with #Configuration.
Provides support in parallel using the configuration described in XML format in the element:
... But I didn't even specify the packages to scan....
And without specifying this annotation, I also can't access resources... ???
Why is that? I don't know…
What do I need to implement WebApplicationInitializer , - I don't know…
https://docs.spring.io/spring-boot/docs/current/reference/html/howto.html#howto-weblogic
Contollers
src/main/java/web/spring/boot/jar/webspringbootjar/controller/index/IndexController.java
IndexController
#Controller
public class IndexController {
#RequestMapping(value="/", method= RequestMethod.GET)
public String index() {
return "index";
}
}
src/main/java/web/spring/boot/jar/webspringbootjar/controller/login/LoginController.java
LoginController
#Controller
public class LoginController {
#GetMapping(value = {"/log"})
public ModelAndView showUsersPage() {
return new ModelAndView("pages/login/login.html");
}
}
The creation of an artifact (Intellij Idea)
- Project Structure/Artifacts
- Alt + Insert
- Web Application: Exploded -> From module
There is the path to the directory with your project will be specified.
You can also configure
- Project Structure/Facets
And then…
Run\Edit Configuration
Here you configure - weblogic…
URL to the app(in my case)
http://localhost:7001/rst/
The same access will be available when you migrate .*war from the development environment and deploy it to an external weblogic server.
If you start an external server, you get an error
This means that you need to run maven and it must will make a *.war-file…
Also It doesn't work...
#Configuration
#EnableWebMvc
#ComponentScan(basePackageClasses = { ServletContextConfig.class })
public class ServletContextConfig implements WebMvcConfigurer {
#Bean
public WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>
webServerFactoryCustomizer() {
return factory -> factory.setContextPath("/rootweb");
}
}
I suppose we only can use xml-configuration for servlet-context here.
If someone can add something, please specify what is wrong....
Can who has what ideas, about my issues which arose during this messages ....

how do I get this simple angularjs / springmvc application to work?

I am trying to create a simple angularjs/springmvc "rest" application... -so far, with no success.
In this example, I want to obtain a string value from a spring controller method - i.e., "getUser()" and, then, display it on a jsp page - i.e., "home.jsp".
after typing "http://localhost:8084/angtest02/home" in the browser, the initial screen displays "user: "...
Then, when I modify the URL to be "http://localhost:8084/angtest02/home/user" - i.e., in order to invoke the spring controller method, the screen still only displays "user: " (i.e., instead of the desired "user: Joseph Blow")...
-- But, additionally, presents a dialog that asks: "Do you want to open or save user.json (11 bytes) from localhost"...
...Which is NOT what I want.
Rather, my intention is to have the "{{user}}" variable replaced with the value returned by the spring controller method: "getUser()".*
The image, below, is just to illustrate the location of the "{{user}}" variable on the page.
Note: the spring controller method (i.e., "getUser()") is apparently invoked, but, seemingly, not the "userService" (i.e., located in app.js)
Below, is my code...
index.jsp (redirects to home.jsp)
<%
response.sendRedirect("home");
%>
home.jsp
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!doctype html>
<html>
<head>
<title>home page</title>
<meta charset="UTF-8">
<script type="text/javascript" src="${pageContext.request.contextPath}/webjars/angularjs/1.4.4/angular.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/webjars/angularjs/1.4.4/angular-route.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/webjars/angularjs/1.4.4/angular-resource.js"></script>
<script type="text/javascript" src="resources/js/app.js"></script>
</head>
<body ng-app="mainApp">
<div ng-controller="appController">
<h1>user: {{user}}</h1>
</div>
</body>
</html>
app.js
/* global angular */
var mainApp = angular.module('mainApp', []);
mainApp.controller('appController', function ($scope, userService) {
userService.getUser().success(function (data) {
$scope.user = data;
});
});
mainApp.factory('userService', function ($http) {
var getUser = function () {
return $http.get('/home/user');
};
return {
getUser: getUser
};
});
UserRestController.java
package aaa.bbb.ccc.war;
import javax.servlet.http.HttpSession;
import org.apache.logging.log4j.LogManager;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#Controller
public class UserRestController
{
private static final org.apache.logging.log4j.Logger LOG = LogManager.getLogger("UserRestController");
#RequestMapping(value = "/home/user", produces = {"application/json"}, method = RequestMethod.GET)
public ResponseEntity<String> getUser()
{
LOG.info("_______getUser()_______entering...");
return new ResponseEntity<>("Joesph Blow", HttpStatus.OK);
}
//***seems I need this "init" method, in order to "land" on the "home.jsp" page, initially...
#RequestMapping(value = "/home", method = RequestMethod.GET)
public String home(HttpSession session)
{
LOG.info("_______home()_______entering...");
return "home";
}
}
FUTHER DETAILS BELOW (IF NEEDED)...
web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<filter>
<filter-name>charEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>charEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
WEB-INF/spring/applicationContext.xml
</param-value>
</context-param>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
WEB-INF/spring/applicationContext.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
project folder structure looks like this
angtest02
|
+-src
|
+-main
|
+-java
| |
| +-aaa.bbb.ccc.war
| |
| +-UserRestController.java
|
+-resources
| |
| +-log4j2.xml
|
+-webapp
|
+-META-INF
| |
| +-context.xml
|
+-resources
| |
| +-js
| |
| +-app.js
|
+-WEB-INF
|
+-spring
| |
| +-applicationContext.xml
|
+-views
|
+-home.jsp
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:ws="http://jax-ws.dev.java.net/spring/core"
xmlns:wss="http://jax-ws.dev.java.net/spring/servlet"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://jax-ws.dev.java.net/spring/core http://jax-ws.dev.java.net/spring/core.xsd
http://jax-ws.dev.java.net/spring/servlet http://jax-ws.dev.java.net/spring/servlet.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="aaa.bbb.ccc.**" />
<mvc:annotation-driven/>
<context:annotation-config/>
<tx:annotation-driven/>
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<mvc:resources mapping="/static/**" location="/static/" />
<mvc:resources mapping="/webjars/**" location="/webjars/" />
<mvc:resources mapping="/assets/**" location="/assets/" />
<mvc:resources mapping="/resources/**" location="/resources/" />
</beans:beans>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>aaa.bbb.ccc</groupId>
<artifactId>angtest02</artifactId>
<version>1</version>
<packaging>war</packaging>
<name>angtest02</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<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>2.3.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.3.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hibernate4</artifactId>
<version>2.3.4</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>datatables</artifactId>
<version>1.10.7</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>angularjs</artifactId>
<version>1.4.4</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>ng-grid</artifactId>
<version>2.0.14</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>2.1.4</version>
</dependency>
</dependencies>
<build>
<finalName>${project.name}-${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
</plugin>
</plugins>
</build>
</project>
You controller is trying to produce a json but the format seems to be not a json.
This should solve your issue.
#Controller
public class UserRestController
{
#RequestMapping(value = "/home/user", method = RequestMethod.GET)
public #ResponseBody String getUser()
{
return "Joesph Blow";
}
...
}
Or make the controller a RestController
#RestController
public class UserRestController{
#RequestMapping(value = "/home/user", method = RequestMethod.GET)
public ResponseEntity<String> getUser()
{
return new ResponseEntity<>("Joesph Blow", HttpStatus.OK);
}
...
}
SOLVED: The app now seems to be working as I had originally intended...
See changes below...
home.jsp - changed as follows...(used map object... -unable to use display simple string)...
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!doctype html>
<html>
<head>
<title>home page</title>
<script type="text/javascript" src="${pageContext.request.contextPath}/webjars/angularjs/1.4.4/angular.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/webjars/angularjs/1.4.4/angular-route.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/webjars/angularjs/1.4.4/angular-resource.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/resources/js/app.js"></script>
</head>
<body ng-app="mainApp">
<div ng-controller="appController">
<h2>test that requests data from rest controller...</h2>
<p>User: {{user.name}}</p>
</div>
</body>
</html>
app.js - changed as follows...
/* global angular */
var mainApp = angular.module('mainApp', []);
mainApp.controller("appController", [ '$scope', '$http',
function($scope, $http)
{
$http.get('/angtest02/user.json').
success(function(data) {
$scope.user = data;
});
}]);
UserRestController.java - changed as follows...
package aaa.bbb.ccc.war;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.apache.logging.log4j.LogManager;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
#Controller
public class UserRestController
{
private static final org.apache.logging.log4j.Logger LOG = LogManager.getLogger("UserRestController");
#RequestMapping(value = "/user", method = RequestMethod.GET, produces = {"application/xml", "application/json"})
#ResponseStatus(HttpStatus.OK)
#ResponseBody
public Map<String, Object> getUser()
{
LOG.info("_______getUser_______entering...");
//plain string did not work... //String user = "Joseph Blow";
Map<String,Object> user = new HashMap<>();
user.put("name", "Joseph Blow");
return user;
}
#RequestMapping(value = "/home", method = RequestMethod.GET)
public String home(HttpSession session)
{
LOG.info("_______home_______entering...");
return "home";
}
}
applicationContext.xml - changed as follows...
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:ws="http://jax-ws.dev.java.net/spring/core"
xmlns:wss="http://jax-ws.dev.java.net/spring/servlet"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://jax-ws.dev.java.net/spring/core http://jax-ws.dev.java.net/spring/core.xsd
http://jax-ws.dev.java.net/spring/servlet http://jax-ws.dev.java.net/spring/servlet.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="aaa.bbb.ccc.war" />
<!-- big change here was adding this "contentManager"... -->
<mvc:annotation-driven content-negotiation-manager="contentManager"/>
<beans:bean id="contentManager"
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<beans:property name="favorPathExtension" value="true"/>
<beans:property name="ignoreAcceptHeader" value="true" />
<beans:property name="defaultContentType" value="text/html" />
<beans:property name="useJaf" value="false"/>
<beans:property name="mediaTypes">
<beans:map>
<beans:entry key="html" value="text/html" />
<beans:entry key="json" value="application/json" />
<beans:entry key="xml" value="application/xml" />
</beans:map>
</beans:property>
</beans:bean>
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<mvc:resources mapping="/static/**" location="/static/" />
<mvc:resources mapping="/webjars/**" location="/webjars/" />
<mvc:resources mapping="/assets/**" location="/assets/" />
<mvc:resources mapping="/resources/**" location="/resources/" />
</beans:beans>

Hibernate, JPA, No Persistence provider for EntityManager named

I'm working on a Swing application with JPA and Hibernate. But every time I try to call following code to get EntityManager
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
EntityManagerFactory emf = Persistence.createEntityManagerFactory("abcd");
EntityManager em = emf.createEntityManager();
I get following exceptions:
Caused by: javax.persistence.PersistenceException: No Persistence provider for EntityManager named abcd: Provider named org.hibernate.jpa.HibernatePersistenceProvider threw unexpected exception at create EntityManagerFactory:
javax.persistence.PersistenceException
javax.persistence.PersistenceException: Unable to build entity manager factory
and
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
Path to persistence.xml is my.jar/meta-inf/persistence.xml
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="abcd" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="hibernate.connection.url" value="xxx"/>
<property name="hibernate.connection.password" value="xxx"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.connection.user" value="xxx"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
</properties>
</persistence-unit>
</persistence>
Dependencies:
All dependencies except junit are in a classpath
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.10.Final</version>
<scope>compile</scope>
</dependency>
It seems, it cannot find your persistence.xml file. The correct folder for the persistence.xml file is META-INF and not meta-inf.

Spring MVC - problems while returning an object as #ResponseBody (famous Error 406, unsolved)

I've gone through absolutely every single post here related to the subject, trying all suggested here options... and after a couple of days, it still doesn't work for me.
In short, what I want to do is:
Send data from html form to the Spring MVC controller using jquery and $.post function with JSON type.
On the controller side I have a method which should return an object with one file of String type.
1st point works properly, I manage to send a POST request and receive it properly on the controller side. What doesn't work is the answer from controller back to jsp and the function defined for $.post(). I debugged it on web browser side and I can see that the Content-Type of response is not "application/json" (hence the Error 406, I believe).
Here's what I've got:
JSP:
<%# taglib uri="http://www.springframework.org/tags/form"
prefix="springForm"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet"
href="//code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css"/>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.3/jquery-ui.js"></script>
</head>
<body>
<springForm:form method="POST" commandName="signInDto"
action="/EquipmentManager/signIn.do" id="add-user-form">
<p>
Login:
<springForm:input path="login" />
<springForm:errors path="login" cssClass="error" />
</p>
<p>
Password:
<springForm:password path="password" />
<springForm:errors path="password" cssClass="error" />
</p>
<p>${errorMessage}
<p />
<input type="submit" value="Sign In" />
</springForm:form>
<script type="text/javascript">
function collectFormData(fields) {
var data = {};
for (var i = 0; i < fields.length; i++) {
var $item = $(fields[i]);
data[$item.attr('name')] = $item.val();
}
return data;
}
$(document).ready(
function() {
var $form = $('#add-user-form');
$form.bind('submit', function(e) {
// Ajax validation
var $inputs = $form.find('input');
var data = collectFormData($inputs);
$.post('/EquipmentManager/signInValidate', data,
function(response) {
$form.find('.control-group').removeClass(
'error');
$form.find('.help-inline').empty();
$form.find('.alert').remove();
if (response.value == 'FAIL') {
alarm(response.value);
} else {
$form.unbind('submit');
$form.submit();
}
}, 'json');
e.preventDefault();
return false;
});
});
</script>
</body>
</html>
Controller:
#RequestMapping(value = "/signInValidate", method = RequestMethod.POST)
public #ResponseBody TestObject signInValidate(#Valid SignInDto signInDto,
BindingResult bindingResult) {
TestObject testObject = new TestObject();
if (bindingResult.hasErrors()) {
testObject.setValue("FAIL");
logger.info("FAIL");
} else {
testObject.setValue("SUCCESS");
logger.info("SUCCESS");
}
return testObject;
}
Bean:
public class TestObject {
private String value;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
Dispatcher Servlet:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.equipment.controller" />
<mvc:annotation-driven />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/view/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.esocphotoclub</groupId>
<artifactId>EquipmentManager</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>EquipmentManager Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.8.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.3.Final</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.11</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
<build>
<finalName>EquipmentManager</finalName>
</build>
</project>
I've already tried different versions of jackson-*, explicit defining of ContentNegotiatingViewResolver, explicit specification of produces="application/json" in #RequestMapping, TestObject serialisation, and few other things I've found here. None of them work for me.
To give you as much info as possible, all works fine if I don't request 'json' type in $.post and in the controller I return a simple string. That's not what I want to do though. What I want is to submit a request for validation purpose and receive an object, not a simple string.
Any hint I'll very much appreciate, perhaps there's something I still haven't try.
Many thanks in advance!
I hope you have all jackson mapping and if that is the case you are missing jsonview. Can you try the below. All we are doing is setting multiple view resolvers.
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean id="jsonViewResolver" class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="order" value="1"/>
<property name="location" value="/WEB-INF/views.xml"/>
</bean>
Create a file called views.xml in you WEB-INF folder and add the below entry.
<bean name="jsonView" class="org.springframework.web.servlet.view.json.JsonView"/>
It took me a few days, but I finally managed to sort this out by trial and error method.
What I did was simply add another jackson dependency in the POM file: jackson-databind and now it works just fine.