我是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);
}
}
我不明白,我在这里做了什么错误,请看看代码,并告诉我,我做了什么错误, 任何帮助将不胜感激
您展示包括JSON转换一个相当完整的应用程序。它在上次修改之前是否有效,如果是,那么修改是什么?如果不是的话,你应该尝试把它解决到显示问题的简单代码。 –
控制器代码? – NimChimpsky
@SergeBallesta,我开始用代码库级别编写代码并达到服务级别,它很好,当我来到控制器的时候,我不得不实现这些调度程序servlet文件,网页描述符等,所以你可以这样说是我实施控制器并将其与服务级别连接起来所迈出的重要一步。所以你问了最后的修改。因此,执行控制器是我所做的最后一次修改。 –