Spring HTTP.DELETE返回代码415不支持的mediatype

时间:2013-01-11 02:53:12

标签: spring hibernate http

@ResponseBody
@RequestMapping(method = RequestMethod.DELETE)
public String deleteState(@RequestBody String message) {
        System.out.println("controller");
    int resp = stateService.delete(message);

    return resp == 1 ? "State deleted successfully : HTTP 200"
            : "delete operation failed : HTTP 400";

}

当我发布json { stateName: VA}时,它会返回415错误,任何人都可以提出建议吗?

@Override
@Transactional
public int delete(Object message) {
    System.out.println("service");
    JSONObject jsonObj = null;
    try {
        jsonObj = new JSONObject((String) message);
        Parameters p = new Parameters();
        p.property = "state_name";
        p.value = (String) jsonObj.getString("stateName");
        System.out.println("service");
        return stateDAO.delete(p);
    } catch (Exception e) {
        return 0;
    }

}

这就是我在服务中所做的......

更新:这是我的Spring配置:

<context:annotation-config />
<!-- Scans the classpath of this application for @Components to deploy as 
    beans -->
<context:component-scan base-package="com.tutorial.jquery" />

<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />

<bean id="messageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:messages" />
    <property name="defaultEncoding" value="UTF-8" />
</bean>

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    p:location="/WEB-INF/spring/jdbc.properties" />

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close" p:driverClassName="org.postgresql.Driver"
    p:url="jdbc:postgresql://localhost:5432/processdb" p:username="postgres"
    p:password="postgres" />


<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
    <property name="configurationClass">
        <value>org.hibernate.cfg.AnnotationConfiguration</value>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

<tx:annotation-driven />

<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- >bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" 
    /> <property name="order" value="0" /> <property name="prefix"> <value>/WEB-INF/views/</value> 
    </property> <property name="suffix"> <value>.html</value> </property> </bean -->


<bean
    class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="order" value="2" />
    <property name="mediaTypes">
        <map>
            <entry key="json" value="application/json" />

        </map>
    </property>

    <property name="defaultViews">
        <list>
            <!-- JSON View -->
            <bean
                class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
            </bean>

        </list>
    </property>

</bean>

<!-- If no extension matched, use JSP view -->
<bean id="htmlView"
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="order" value="1" />
    <property name="prefix">
        <value>/views/</value>
    </property>
    <property name="suffix">
        <value>.html</value>
    </property>
</bean>

我更新了方法以使其正常工作:

    @ResponseBody
@RequestMapping(method = RequestMethod.DELETE)
public String deleteState(HttpServletRequest request,
        HttpServletResponse response) {
    Map<String, String[]> parameters = request.getParameterMap();
    //System.out.print("RequestParam: " + state_Name);
    int resp = stateService.delete(parameters.get("state_name")[0]);

    return resp == 1 ? "State deleted successfully : HTTP 200"
            : "delete operation failed : HTTP 400";
}

这解决了这个问题 谢谢!

1 个答案:

答案 0 :(得分:1)

试试这个,

变化

@RequestBody String message

@RequestParam String stateName