HTTP 400 on on使用Spring 4 MVC和MappingJackson2HttpMessageConverter定义的Restful post服务

时间:2015-03-18 22:57:59

标签: spring rest spring-mvc http-status-code-400

我尝试使用Spring 4 MVC创建一个安静的控制器,并通过点击下面提到的RestFul Post Web服务来测试它,但是在我将MappingJackson2HttpMessageConverter添加到我的后面时,将HTTP / 1.1 400 Bad Request作为响应调度员servlet.xml中。响应正确如果我使用' @RequestBody字符串资源'而不是' @RequestBody资源资源' for ResourceController post方法参数。我已经将MappingJackson2HttpMessageConverter添加到了dispatcher-servlet.xml,因为spring没有检测到jackson并且在点击这个帖子服务时抛出HTTP 415。

网址:http://localhost:8080/rma/api/resources/1

标题:

User-Agent: Fiddler
Host: localhost:8080
Content-Length: 171
Accept: application/json
Content-Type: application/json

请求正文:

{
"firstName" : "first",
"middleName" : "middle",
"lastName" : "last",
"email": "first@last.com",
"dateOfBirth": "09-10-1988",  
"titleRole" : "software engineer"
}

调度-servlet.xml中

<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        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:annotation-config />
        <context:component-scan base-package="com.pkg.controller" />
        <mvc:annotation-driven/>

        <bean
    class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
            <property name="messageConverters">
                <list>
                    <bean
                        class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
                </list>
            </property>
        </bean>
        <bean id="resourceDao" class="com.pkg.dao.ResourceDaoImpl">
            <property name="dataSource" ref="dataSource" />
        </bean>

        <bean id="dataSource"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
            <property name="url" value="jdbc:mysql://localhost/rma?"></property>
            <property name="username" value="root"></property>
            <property name="password" value="root"></property>
        </bean>
    </beans>

ResourceController.class

@RestController
@RequestMapping("/resources")
public class ResourceController {

@Autowired
private final ResourceDao resourceDao; 

    @Autowired
    public ResourceController(ResourceDao resourceDao) {
        this.resourceDao = resourceDao;
    }

    //get resources for a resource id
    @RequestMapping(value ="/{hiringManager}", method = RequestMethod.GET, produces="application/json")
    public List<Resource> getResources(@PathVariable String hiringManager){
        return resourceDao.getResources(hiringManager);
    }

    //post a new resource for a resource id
    @RequestMapping(value ="/{hiringManager}",method = RequestMethod.POST, consumes="application/json",produces="application/json")
    public  ResponseEntity<Resource>   addResources(@PathVariable String hiringManager, @RequestBody Resource resource){
        System.out.println("In controller" + hiringManager + " > Adding " +resource.toString());
        return new ResponseEntity<Resource>(resource,HttpStatus.CREATED);
    }


}

WebApplicationInitializer.class

public class ApplicationInitializer  implements WebApplicationInitializer {



    public void onStartup(ServletContext servletContext)
            throws ServletException {

        //Registering Dispatcher Servlet
        try{
            ServletRegistration.Dynamic registration = servletContext.addServlet("dispatcher", new DispatcherServlet());
            registration.setLoadOnStartup(1);
            registration.addMapping("/api/*"); // URL mapping
        } catch(Exception e){
            e.printStackTrace();
        }


    }



}

Resource.class

@Entity
public class Resource{

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String firstName;
    private String middleName;
    private String lastName;
    private String email;
    @Temporal(TemporalType.DATE)
    private Date dateOfBirth;
    private String titleRole;
    private String hiringManagerId;

    public Resource(){

    }

    public int getId() {
        return id;
    }

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

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Date getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(Date dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    public String getTitleRole() {
        return titleRole;
    }

    public void setTitleRole(String titleRole) {
        this.titleRole = titleRole;
    }

    public String getHiringManagerId() {
        return hiringManagerId;
    }

    public void setHiringManagerId(String hiringManagerId) {
        this.hiringManagerId = hiringManagerId;
    }

    public String getMiddleName() {
        return middleName;
    }

    public void setMiddleName(String middleName) {
        this.middleName = middleName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

}

的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>rma</groupId>
    <artifactId>rma</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <dependencies>
        <!-- Spring Core Dependency -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.4.RELEASE</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- Spring MVC Dependency -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.4.1</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.4.1</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.4.1</version>
        </dependency>


        <!-- Logger Dependency -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>1.5.8</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.5.8</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.5.8</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.14</version>
        </dependency>
        <!-- Unit Testing Dependency -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <!-- Connection Pooling Dependency -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.3</version>
        </dependency>
        <!-- ORM Dependency -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.8.Final</version>
        </dependency>
        <!-- Database Dependency -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.34</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

0 个答案:

没有答案