简体   繁体   中英

Spring: HTTP Status 406 in Tomcat response

I'm facing problem with HTTP Status 406 in a resposne from Tomcat server (v. 8, tried 7 as well) on a Spring webapp. The description of a response is: "The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers."

index.jsp is displayed correctly but results.jsp is not presented as expected.

I have tried every solution from stack including imports of fasterxml, jackson etc. Could you please help me out?

This is my base controller:

import org.slf4j.LoggerFactory;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import backEnd.Core;

@Controller
public class BaseController {

private static final String VIEW_INDEX = "index";   
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(BaseController.class);

@RequestMapping(value = "/", method = RequestMethod.GET)
public String welcome() {
    return VIEW_INDEX;
}

@RequestMapping(value = "/calculate", headers="Accept=*/*", method = RequestMethod.GET)
public @ResponseBody ModelAndView calculate(@RequestParam(value = "origin") String origin,
        @RequestParam(value = "destination") String destination,
        @RequestParam(value = "numberOfPax") String numberOfPax,
        @RequestParam(value = "fuelPrice") String fuelPrice,
        @RequestParam(value = "consumption") String consumption) {
    String cost = null;
    String distanceWithRoute = null;
    if (origin != null) {
        if (origin.contains(" ")) {
            origin = origin.replace(" ", "+");
        }
        if (destination != null) {
            if (destination.contains(" ")) {
                destination = destination.replace(" ", "+");
            }
            // response.reset();
            try {
                distanceWithRoute = Core.getDistance(origin, destination);
                cost = String.valueOf(Core.getCost(numberOfPax, consumption, fuelPrice));                   
                logger.info("ReqOri: " + destination);
                logger.info("ReqDest: " + destination);
                logger.info("Response: " + distanceWithRoute + cost);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }   
    ModelAndView resultsView = new ModelAndView();
    resultsView.setViewName("calculate");
    resultsView.addObject("distanceWithRoute", distanceWithRoute);
    resultsView.addObject("costAttribute", cost);
    return resultsView;
}
}

Web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<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_3_0.xsd"
  version="3.0">

<display-name>Trip cost calculator</display-name>

<servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>
                    org.springframework.web.servlet.DispatcherServlet
            </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>

<listener>
    <listener-class>
               org.springframework.web.context.ContextLoaderListener
            </listener-class>
</listener>
</web-app>

Dispatcher:

<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"
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">

<context:component-scan base-package="com.kmajewski.controller" />

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/pages/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>
</beans>

calculate.jsp:

<html>
<head>
    <meta http-equiv='Content-Type'  content='text/html; charset=UTF-8'>
</head>
<body>
    <h1>Trip cost calculator</h1><br>
    ${distanceWithRoute} <br>
    Price per passenger: <b> ${costAttribute} </b>      
</body>
</html>

And index.jsp:

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
    <meta http-equiv='Content-Type'  content='text/html; charset=UTF-8'>
</head>
<body>
    <form:form method="GET" action="calculate">
            Origin: <input type='text' name='origin'><br>
            Destination: <input type='text' name='destination'><br>
            Number of passangers: <input type='text' name='numberOfPax'><br>
            Fuel price: <input type='text' name='fuelPrice'><br>
            Avg. fuel consumption: (L/100km): <input type='text' name='consumption'><br><br>
            <input type='submit' value='Calculate'>
            </form:form>
</body>
</html>

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.kmajewski</groupId>
 <artifactId>tripCalculator</artifactId>
 <packaging>war</packaging>
 <version>1.0-SNAPSHOT</version>
 <name>tripCalculator Maven Webapp</name>
<url>http://maven.apache.org</url>
 <dependencies>
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20150729</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.2.2.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>4.2.2.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.2.2.RELEASE</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.1.3</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jcl-over-slf4j</artifactId>
    <version>1.7.12</version>
</dependency>
<dependency>
    <groupId>jstl</groupId>
    <artifactId>jstl</artifactId>
    <version>1.2</version>
</dependency>
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
</dependency>   
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.9.13</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.6.3</version>
</dependency>   
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.6.3</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.6.3</version>
</dependency>           
</dependencies>
<build>
<finalName>tripCalculator</finalName>
<plugins>
       <!-- Eclipse project -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-eclipse-plugin</artifactId>
        <version>2.10</version>
        <configuration>
               <!-- Always download and attach dependencies source code -->
            <downloadSources>true</downloadSources>
            <downloadJavadocs>false</downloadJavadocs>
            <!-- Avoid type mvn eclipse:eclipse -Dwtpversion=2.0 -->
            <wtpversion>2.0</wtpversion>
        </configuration>
      </plugin>
      <!-- Set JDK Compiler Level -->
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.3</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
        </configuration>
      </plugin>
      <!-- For Maven Tomcat Plugin -->
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
            <path>/tripCalculator</path>
        </configuration>
      </plugin>
</plugins>
</build>
</project>

Thanks to Fran Montero I have got this work. Really appreciate it, buddy. Thank you.

This is how my dispatcher looks like:

<beans xmlns="http://www.springframework.org/schema/beans"  
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 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.kmajewski.controller" />
<mvc:annotation-driven />


<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/pages/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM