运行Spring WebApp时的代码状态406

时间:2018-01-14 17:38:15

标签: java spring spring-mvc tomcat ant

我的项目文件夹中有这些文件

的build.xml

version="1.0"?>

    

<property name="src.dir" value="src"/>
<property name="web.dir" value="war"/>
<property name="build.dir" value="${web.dir}/WEB-INF/classes"/>
<property name="test.dir" value="test"/>
<property name="name" value="HelloWorld"/>

<path id="master-classpath">
    <fileset dir="${web.dir}/WEB-INF/lib">
        <include name="*.jar"/>
    </fileset>
    <!-- We need the servlet API classes: -->
    <!--  * for Tomcat 5/6 use servlet-api.jar -->
    <!--  * for other app servers - check the docs -->
    <fileset dir="${appserver.lib}">
        <include name="servlet*.jar"/>
    </fileset>
    <pathelement path="${build.dir}"/>
</path>

<target name="usage">
    <echo message=""/>
    <echo message="${name} build file"/>
    <echo message="-----------------------------------"/>
    <echo message=""/>
    <echo message="Available targets are:"/>
    <echo message=""/>
    <echo message="build     --> Build the application"/>
    <echo message="deploy    --> Deploy application as directory"/>
    <echo message="deploywar --> Deploy application as a WAR file"/>
    <echo message="start-webapp   --> Start application in Tomcat"/>
    <echo message="stop-webapp    --> Stop application in Tomcat"/>
    <echo message="deploy-webapp     --> Deploy application in Tomcat"/>
    <echo message="undeploy-webapp      --> Undeploy application in Tomcat"/>
    <echo message="list      --> List Tomcat applications"/>
    <echo message=""/>
</target>

<target name="build" description="Compile main source tree java files">
    <mkdir dir="${build.dir}"/>
    <javac destdir="${build.dir}"  debug="true"
           deprecation="false" optimize="false" failonerror="true">
        <src path="${src.dir}"/>
        <classpath refid="master-classpath"/>
    </javac>
</target>

<target name="buildtests" description="Building All TestCases">
    <mkdir dir="${build.dir}"/>
    <javac destdir="${build.dir}" source="1.5" target="1.5" debug="true"
           deprecation="false" optimize="false" failonerror="true">
        <src path="${test.dir}"/>
        <classpath refid="master-classpath"/>
    </javac>
</target>

<target name="clean" description="Cleaning All build files">
    <delete>
        <fileset dir="${build.dir}">
            <include name="**/*.class"/>
        </fileset>
    </delete>
</target>

<target name="tests" depends="build,buildtests" description="Running All TestCases">
    <junit printsummary="on" fork="false" haltonfailure="false" failureproperty="tests.failed"
           showoutput="true">
        <classpath refid="master-classpath"/>
        <formatter type="brief" usefile="false"/>

        <batchtest>
            <fileset dir="${build.dir}">
                <include name="**/*Tests.*"/>
            </fileset>
        </batchtest>
    </junit>

    <fail if="tests.failed">
        tests.failed=${tests.failed}
        ***********************************************************
        ***********************************************************
        ****  One or more tests failed!  Check the output ...  ****
        ***********************************************************
        ***********************************************************
    </fail>
</target>

<target name="deploy" depends="build" description="Deploy application">
    <copy todir="${deploy.path}/${name}" preservelastmodified="true">
        <fileset dir="${web.dir}">
            <include name="**/*.*"/>
        </fileset>
    </copy>
</target>

<target name="deploywar" depends="build" description="Deploy application as a WAR file">
    <war destfile="${name}.war"
         needxmlfile="false">
        <fileset dir="${web.dir}">
            <include name="**/*.*"/>
        </fileset>
    </war>
    <copy todir="${deploy.path}" preservelastmodified="true">
        <fileset dir=".">
            <include name="*.war"/>
        </fileset>
    </copy>
</target>

<!-- ============================================================== -->
<!-- Tomcat tasks - remove these if you don't have Tomcat installed -->
<!-- ============================================================== -->

<path id="catalina-ant-classpath">
    <!-- We need the Catalina jars for Tomcat -->
    <!--  * for other app servers - check the docs -->
    <fileset dir="${appserver.lib}">
        <include name="catalina-ant.jar"/>
        <include name="tomcat-coyote.jar"/>
        <include name="tomcat-util.jar"/>
    </fileset>
    <fileset dir="${appserver.home}/bin">
        <include name="tomcat-juli.jar"/>
    </fileset>
</path>

<taskdef name="catalina-deploy" classname="org.apache.catalina.ant.DeployTask" classpathref="catalina-ant-classpath"/>
<taskdef name="catalina-list" classname="org.apache.catalina.ant.ListTask" classpathref="catalina-ant-classpath"/>
<taskdef name="catalina-reload" classname="org.apache.catalina.ant.ReloadTask" classpathref="catalina-ant-classpath"/>
<taskdef name="catalina-findleaks" classname="org.apache.catalina.ant.FindLeaksTask" classpathref="catalina-ant-classpath"/>
<taskdef name="catalina-resources" classname="org.apache.catalina.ant.ResourcesTask" classpathref="catalina-ant-classpath"/>
<taskdef name="catalina-start" classname="org.apache.catalina.ant.StartTask" classpathref="catalina-ant-classpath"/>
<taskdef name="catalina-stop" classname="org.apache.catalina.ant.StopTask" classpathref="catalina-ant-classpath"/>
<taskdef name="catalina-undeploy" classname="org.apache.catalina.ant.UndeployTask" classpathref="catalina-ant-classpath"/>

<target name="start-webapp" description="Start application in Tomcat">
    <catalina-start url="${tomcat.manager.url}"
                    username="${tomcat.manager.username}"
                    password="${tomcat.manager.password}"
                    path="/${name}" />
</target>

<target name="stop-webapp" description="Stop application in Tomcat">
    <catalina-stop url="${tomcat.manager.url}"
             username="${tomcat.manager.username}"
             password="${tomcat.manager.password}"
             path="/${name}" />
</target>

<target name="deploy-webapp" description="Deploy application in Tomcat">
    <catalina-deploy url="${tomcat.manager.url}"
            username="${tomcat.manager.username}"
            password="${tomcat.manager.password}"
            path="/${name}"
            war="file:${deploy.path}/${name}.war"/>
</target>

<target name="undeploy-webapp" description="Undeploy Tomcat application">
    <catalina-undeploy url="${tomcat.manager.url}"
           username="${tomcat.manager.username}"
           password="${tomcat.manager.password}"
           path="/${name}"/>
</target>

<target name="list" description="List Tomcat applications">
    <catalina-list url="${tomcat.manager.url}"
          username="${tomcat.manager.username}"
          password="${tomcat.manager.password}"/>
</target>

<target name="reload" description="Reload Application in Tomcat">
    <catalina-reload url="${tomcat.manager.url}"
                     username="${tomcat.manager.username}"
                     password="${tomcat.manager.password}"
                     path="/${name}"/>
</target>

<!-- End Tomcat tasks -->

AppIntializer.java

package HelloWorld.web.config;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class AppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
    ctx.register(AppConfig.class);
    ctx.setServletContext(servletContext);

    Dynamic dynamic = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
    dynamic.addMapping("/");
    dynamic.setLoadOnStartup(1);
}
}

AppConfig.java

package HelloWorld.web.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "HelloWorld.web")
public class AppConfig {
}

Person.java

package HelloWorld.web.POJO;


public class Person {

private String id;

private String name;

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
}

HelloController.java

package HelloWorld.web.controller;

import HelloWorld.web.POJO.Person;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/data")
public class HelloController {

protected final Log logger = LogFactory.getLog(getClass());

@RequestMapping(value = "/persons",  method = RequestMethod.GET, headers="Accept=application/json")
public Person handleRequest() {
    Person person = new Person();
    person.setId("1");
    person.setName("Kuch");
    return person;
}
}

lib文件夹包含spring,jackson,servlet-api,common-logging jar

commons-logging-1.2-javadoc.jar
    commons-logging-1.2.jar
    hamcrest-core-1.3.jar
    jackson-core-2.9.3.jar
    junit-4.12.jar
    servlet-api.jar
    spring-aop-5.0.2.RELEASE-javadoc.jar
    spring-aop-5.0.2.RELEASE-sources.jar
    spring-aop-5.0.2.RELEASE.jar
    spring-aspects-5.0.2.RELEASE-javadoc.jar
    spring-aspects-5.0.2.RELEASE-sources.jar
    spring-aspects-5.0.2.RELEASE.jar
    spring-beans-5.0.2.RELEASE-javadoc.jar
    spring-beans-5.0.2.RELEASE-sources.jar
    spring-beans-5.0.2.RELEASE.jar
    spring-context-5.0.2.RELEASE-javadoc.jar
    spring-context-5.0.2.RELEASE-sources.jar
    spring-context-5.0.2.RELEASE.jar
    spring-context-indexer-5.0.2.RELEASE-javadoc.jar
    spring-context-indexer-5.0.2.RELEASE-sources.jar
    spring-context-indexer-5.0.2.RELEASE.jar
    spring-context-support-5.0.2.RELEASE-javadoc.jar
    spring-context-support-5.0.2.RELEASE-sources.jar
    spring-context-support-5.0.2.RELEASE.jar
    spring-core-5.0.2.RELEASE-javadoc.jar
    spring-core-5.0.2.RELEASE-sources.jar
    spring-core-5.0.2.RELEASE.jar
    spring-expression-5.0.2.RELEASE-javadoc.jar
    spring-expression-5.0.2.RELEASE-sources.jar
    spring-expression-5.0.2.RELEASE.jar
    spring-instrument-5.0.2.RELEASE-javadoc.jar
    spring-instrument-5.0.2.RELEASE-sources.jar
    spring-instrument-5.0.2.RELEASE.jar
    spring-jcl-5.0.2.RELEASE-javadoc.jar
    spring-jcl-5.0.2.RELEASE-sources.jar
    spring-jcl-5.0.2.RELEASE.jar
    spring-jdbc-5.0.2.RELEASE-javadoc.jar
    spring-jdbc-5.0.2.RELEASE-sources.jar
    spring-jdbc-5.0.2.RELEASE.jar
    spring-jms-5.0.2.RELEASE-javadoc.jar
    spring-jms-5.0.2.RELEASE-sources.jar
    spring-jms-5.0.2.RELEASE.jar
    spring-messaging-5.0.2.RELEASE-javadoc.jar
    spring-messaging-5.0.2.RELEASE-sources.jar
    spring-messaging-5.0.2.RELEASE.jar
    spring-orm-5.0.2.RELEASE-javadoc.jar
    spring-orm-5.0.2.RELEASE-sources.jar
    spring-orm-5.0.2.RELEASE.jar
    spring-oxm-5.0.2.RELEASE-javadoc.jar
    spring-oxm-5.0.2.RELEASE-sources.jar
    spring-oxm-5.0.2.RELEASE.jar
    spring-test-5.0.2.RELEASE-javadoc.jar
    spring-test-5.0.2.RELEASE-sources.jar
    spring-test-5.0.2.RELEASE.jar
    spring-tx-5.0.2.RELEASE-javadoc.jar
    spring-tx-5.0.2.RELEASE-sources.jar
    spring-tx-5.0.2.RELEASE.jar
    spring-web-5.0.2.RELEASE-javadoc.jar
    spring-web-5.0.2.RELEASE-sources.jar
    spring-web-5.0.2.RELEASE.jar
    spring-webflux-5.0.2.RELEASE-javadoc.jar
    spring-webflux-5.0.2.RELEASE-sources.jar
    spring-webflux-5.0.2.RELEASE.jar
    spring-webmvc-5.0.2.RELEASE-javadoc.jar
    spring-webmvc-5.0.2.RELEASE-sources.jar
    spring-webmvc-5.0.2.RELEASE.jar
    spring-websocket-5.0.2.RELEASE-javadoc.jar
    spring-websocket-5.0.2.RELEASE-sources.jar
    spring-websocket-5.0.2.RELEASE.jar

当我尝试点击http://localhost:8080/HelloWorld/data/persons时 它给出了这个错误 HTTP状态406 - 不可接受

输入状态报告

描述根据请求中收到的主动协商头字段,目标资源没有用户代理可接受的当前表示,并且服务器不愿意提供默认表示。 Apache Tomcat / 8.5.24

请尝试解决此问题。

1 个答案:

答案 0 :(得分:0)

感谢帮助它解决了我添加jackson-all-1.0.9并将对象转换为字符串。