2014-06-21 44 views
-1

我是Spring MVC的新蜜蜂,并且正在尝试一些东西来制作它。但当我试图测试控制器时,我陷入了一个困境。它提供了不受支持的媒体类型,我已阅读并根据文档。我在我的Dispatcher Servlet Context文件中加入了一个表达式。Spring MVC(Unsupported Media Type)

<mvc:annotation-driven /> 

应该自动初始化我需要到我的对象传递给控制器​​在JSON的形式MappingJackson2HttpMessageConverter和我在我的行家依赖性的形式类路径有这些库。

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

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

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

所以现在,我有MVC命名空间的东西,在类路径消息变换JSON库,但仍它抛出error.this是我的测试文件。

PersonControllerTest.java

package com.prateekj.controllers; 

import com.fasterxml.jackson.databind.ObjectMapper; 
import com.prateekj.maker.PersonMaker; 
import com.prateekj.model.Person; 
import com.prateekj.services.PersonService; 
import org.junit.Before; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.http.MediaType; 
import org.springframework.test.context.ContextConfiguration; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 
import org.springframework.test.context.web.WebAppConfiguration; 
import org.springframework.test.web.servlet.MockMvc; 
import org.springframework.test.web.servlet.setup.MockMvcBuilders; 
import org.springframework.web.context.WebApplicationContext; 

import static com.natpryce.makeiteasy.MakeItEasy.a; 
import static com.natpryce.makeiteasy.MakeItEasy.make; 
import static com.natpryce.makeiteasy.MakeItEasy.with; 
import static org.mockito.Mockito.verify; 
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; 
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; 

@RunWith(SpringJUnit4ClassRunner.class) 
@WebAppConfiguration 
@ContextConfiguration("classpath:/configuration/Beans.xml") 
public class PersonControllerTest { 

    @Autowired 
    private WebApplicationContext wac; 

    private MockMvc mockMvc; 

    @Autowired 
    private PersonService personService; 

    private Person person; 

    private Integer DEFAULT_ID = 2; 

    @Before 
    public void setUp(){ 
    this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); 
    } 

    @Test 
    public void shouldAddTheUser() throws Exception { 
    person = make(a(PersonMaker.Person, with(PersonMaker.id, (Integer)null))); 

    mockMvc.perform(put("https://stackoverflow.com/users/add").contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(person))) 
     .andExpect(status().isCreated()); 

    verify(personService).savePerson(person); 
    } 
} 

Beans.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:jpa="http://www.springframework.org/schema/data/jpa" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www. springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 

    <jpa:repositories base-package="com.prateekj.repositories"/> 
    <context:component-scan base-package="com.prateekj.services"/> 
    <context:component-scan base-package="com.prateekj.controllers"/> 

    <bean id="configurationEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor"> 
    <property name="config"> 
     <bean class="org.jasypt.encryption.pbe.config.SimpleStringPBEConfig"> 
     <property name="algorithm" value="PBEWithMD5AndDES"/> 
     <property name="password" value="password"/> 
     </bean> 
    </property> 
    </bean> 

    <bean id="propertyConfigurer" 
     class="org.jasypt.spring31.properties.EncryptablePropertyPlaceholderConfigurer"> 
    <constructor-arg ref="configurationEncryptor"/> 
    <property name="locations"> 
     <list> 
     <value>classpath:properties/persistence.properties</value> 
     </list> 
    </property> 
    </bean> 


    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
    <property name="entityManagerFactory" ref="entityManagerFactory"/> 
    </bean> 

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
    <property name="dataSource" ref="dataSource"/> 
    <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/> 
    <property name="persistenceUnitName" value="mysql-core"/> 
    </bean> 

    <bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
    <property name="database" value="MYSQL"/> 
    <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect"/> 
    <property name="showSql" value="false"/> 
    </bean> 

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> 
    <property name="driverClass" value="${com.prateekj.jdbc.driver}"/> 
    <property name="jdbcUrl" value="${com.prateekj.jdbc.url}"/> 
    <property name="user" value="${com.prateekj.jdbc.user}"/> 
    <property name="password" value="${com.prateekj.jdbc.password}"/> 
    </bean> 


</beans> 

web.xml

<web-app version="2.5" 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_2_5.xsd"> 



    <servlet> 
    <servlet-name>toDo</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>classpath:configuration/mvc-dispatcher-config.xml</param-value> 
    </init-param> 
    </servlet> 

    <servlet-mapping> 
    <servlet-name>toDo</servlet-name> 
    <url-pattern>/</url-pattern> 
    </servlet-mapping> 


</web-app> 

mvc-dispatcher-config.xml

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     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/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 

    <mvc:annotation-driven /> 

</beans> 

PersonController.java

package com.prateekj.controllers; 

import com.prateekj.model.Person; 
import com.prateekj.services.PersonService; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.http.HttpStatus; 
import org.springframework.http.ResponseEntity; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestBody; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

@Controller 
@RequestMapping(value = "/users") 
public class PersonController { 

    private PersonService personService; 

    @Autowired 
    public PersonController(PersonService personService){ 
    this.personService = personService; 
    } 

    @RequestMapping(value = "/add", method = RequestMethod.PUT) 
    public ResponseEntity<Void> addUser(@RequestBody Person person){ 
    personService.savePerson(person); 
    return new ResponseEntity<Void>(HttpStatus.CREATED); 
    } 
} 

我不明白,我在这里做了什么错误,请看看代码,并告诉我,我做了什么错误, 任何帮助将不胜感激

+0

您展示包括JSON转换一个相当完整的应用程序。它在上次修改之前是否有效,如果是,那么修改是什么?如果不是的话,你应该尝试把它解决到显示问题的简单代码。 –

+0

控制器代码? – NimChimpsky

+0

@SergeBallesta,我开始用代码库级别编写代码并达到服务级别,它很好,当我来到控制器的时候,我不得不实现这些调度程序servlet文件,网页描述符等,所以你可以这样说是我实施控制器并将其与服务级别连接起来所迈出的重要一步。所以你问了最后的修改。因此,执行控制器是我所做的最后一次修改。 –

回答

0

您在配置文件中缺少多个步骤。

web.xml中,您永远不会启动根应用程序上下文。你需要的东西,如:

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

,当你不使用默认的/WEB-INF/applicationContext.xml

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath:/configuration/Beans.xml</param-value> 
</context-param> 

它无关,与当前的错误,但它应该很快给你带来其他问题。

您想使用JSON,并在您的pom.xml中正确声明它。但是你忘了告诉Spring框架你需要一个JSON转换器。 =>你的原因Unsupported Media Type。您应该添加这样的事情你web-dispatcher-config.xml

<!-- Configure to plugin JSON as request and response in method handler --> 
<beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> 
    <beans:property name="messageConverters"> 
     <beans:list> 
      <beans:ref bean="jsonMessageConverter"/> 
     </beans:list> 
    </beans:property> 
</beans:bean> 

<!-- Configure bean to convert JSON to POJO and vice versa --> 
<beans:bean id="jsonMessageConverter" 
    class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> 
</beans:bean> 

如果你是新的Spring MVC的,我强烈建议你遵循的教程。我只是用Google搜索,并迅速发现2(我目前的职位会使用第二次的提取):

相关问题