Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringMVC控制器忽略“consumes”属性_Java_Json_Spring_Spring Mvc_Jackson - Fatal编程技术网

Java SpringMVC控制器忽略“consumes”属性

Java SpringMVC控制器忽略“consumes”属性,java,json,spring,spring-mvc,jackson,Java,Json,Spring,Spring Mvc,Jackson,我的控制器如下: @RestController @RequestMapping(value = "/v1/mail", consumes = {APPLICATION_JSON_VALUE}) @ResponseStatus(OK) public class MailController { private CoreOutRestAdapter coreAdapter; @Autowired public MailController(CoreOutRestAdap

我的控制器如下:

@RestController
@RequestMapping(value = "/v1/mail", consumes = {APPLICATION_JSON_VALUE})
@ResponseStatus(OK)
public class MailController {

    private CoreOutRestAdapter coreAdapter;

    @Autowired
    public MailController(CoreOutRestAdapter coreAdapter) {
        this.coreAdapter = coreAdapter;
    }

    @RequestMapping(method = POST)
    public void sendMail(@RequestBody @Validated Mail mail) {
        coreAdapter.sendMail(mail);
    }

}

类路径中的jackson数据绑定2.3.2。但如果我发送内容类型为application/json的POST请求,返回的响应将包含415 status Unsupported Media Type。我不明白为什么控制器忽略@RequestMapping注释中的consumes属性。我怎样才能解决这个问题?另外,您可以在github上找到的项目的其余部分是我的应用程序代码中的工作示例

import org.springframework.http.MediaType;
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;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/abc")
public class ABC {


    @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String getRequest(@RequestBody XYZ xyz) {

        return "success";
   }

我忘了用@EnableWebMvc注释配置。注释后,控制器工作正常。

应用程序中的值是多少?您可以尝试将其移动到方法级别。@Keerthivasan它是org.springframework.http.MediaType.APPLICATION\u JSON\u VALUE APPLICATION/JSON。我试过了,但没有改变。顺便说一句,它必须与类级别一起工作。也许可以尝试从sendMail将consumes={APPLICATION_JSON_VALUE}添加到@RequestMappingmethod@ArekWoźniak返回了相同的状态