我在Spring网络应用程序中遇到来自Tomcat服务器(v.8,已尝试过7)的resposne中的HTTP状态406问题。响应的描述是: "此请求标识的资源只能根据请求生成具有不可接受的特征的响应"接受"报头"
index.jsp显示正确,但results.jsp没有按预期显示。
我已经尝试过堆栈中的所有解决方案,包括importxml,jackson等的导入。你能帮帮我吗?
这是我的基本控制器:
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>
分派器:
<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>
和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>
答案 0 :(得分:1)
感谢Fran Montero我得到了这项工作。真的很感激,伙计。谢谢。
这就是我的调度员的样子:
<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>