Java 正在解决处理程序和请求方法的异常';把';不支持

Java 正在解决处理程序和请求方法的异常';把';不支持,java,jquery,ajax,spring,Java,Jquery,Ajax,Spring,我就是不能。4天来,我一直在努力使用与GET不同的方法发送ajax数据,我无法给出建议。我在SpringBoot中创建了一个项目。我已经创建了一个映射地址控制器 @PutMapping(value = "/changeEmail", consumes = MediaType.APPLICATION_JSON_VALUE) public boolean changeEmail( @RequestBody ChangeEmailDTO changeEmailDTO ) { Syste

我就是不能。4天来,我一直在努力使用与GET不同的方法发送ajax数据,我无法给出建议。我在SpringBoot中创建了一个项目。我已经创建了一个映射地址控制器

@PutMapping(value = "/changeEmail", consumes = MediaType.APPLICATION_JSON_VALUE)
public boolean changeEmail(
    @RequestBody ChangeEmailDTO changeEmailDTO
) {
    System.out.println("email: " + changeEmailDTO.getEmail());
    return true;
}
@Controller
public class PageNotFoundController implements ErrorController{
    @RequestMapping("/error")
    public ModelAndView showPageError() {
        return new ModelAndView("redirect:/signIn");
    }

    @Override
    public String getErrorPath() {
        return "/error";
    }
}
该控制器应接受ajaxa发送的电子邮件地址

function changeEmail() {
    console.log("Event");
    $.ajax({
        type: 'PUT',
        url: '/changeEmail',
        data: {
            email: JSON.strgify($('#email').val())
        },
        success: function (result) {
            console.log('function');
        }
    });
}
然而,唯一的效果是,在控制台崩溃了我

PUT http://localhost:8080/signIn net::ERR_TOO_MANY_REDIRECTS
send    @   jquery-3.2.1.min.js:4
ajax    @   jquery-3.2.1.min.js:4
changeEmail @   settings.js:58
submitHandler   @   settings.js:52
d   @   jquery.validate.min.js:4
(anonymous) @   jquery.validate.min.js:4
dispatch    @   jquery-3.2.1.min.js:3
q.handle    @   jquery-3.2.1.min.js:3
DTO只有一个字段

public class ChangeEmailDTO {
    @IsValidEmail
    @ExistsEmail(ifExistsReturn = false)
    @Getter @Setter private String email;
}
在尝试通过ajax发送数据后,它将我发送到错误控制器

@PutMapping(value = "/changeEmail", consumes = MediaType.APPLICATION_JSON_VALUE)
public boolean changeEmail(
    @RequestBody ChangeEmailDTO changeEmailDTO
) {
    System.out.println("email: " + changeEmailDTO.getEmail());
    return true;
}
@Controller
public class PageNotFoundController implements ErrorController{
    @RequestMapping("/error")
    public ModelAndView showPageError() {
        return new ModelAndView("redirect:/signIn");
    }

    @Override
    public String getErrorPath() {
        return "/error";
    }
}
使用浏览器工具显示发送和接收的内容: 正如我提到的,控制器只能正确地从ajax获取GET查询。每次它在错误控制器上翻转时。 另外,他们说如果我想直接从表单发送数据,那么任何查询都没有问题。 这是什么,我不知道

此外,当在Spring中禁用csrf()时,安全性将接收此类日志

    2017-07-26 21:07:15.465 DEBUG 3760 --- [nio-8080-exec-6] o.s.web.servlet.DispatcherServlet        : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2017-07-26 21:07:15.465 DEBUG 3760 --- [nio-8080-exec-6] o.s.web.servlet.DispatcherServlet        : Successfully completed request
2017-07-26 21:07:15.499 DEBUG 3760 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing PUT request for [/changeEmail]
2017-07-26 21:07:15.501 DEBUG 3760 --- [nio-8080-exec-7] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /changeEmail
2017-07-26 21:07:15.505 DEBUG 3760 --- [nio-8080-exec-7] .m.m.a.ExceptionHandlerExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
2017-07-26 21:07:15.506 DEBUG 3760 --- [nio-8080-exec-7] .w.s.m.a.ResponseStatusExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
2017-07-26 21:07:15.506 DEBUG 3760 --- [nio-8080-exec-7] .w.s.m.s.DefaultHandlerExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
2017-07-26 21:07:15.508 DEBUG 3760 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet        : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2017-07-26 21:07:15.508 DEBUG 3760 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet        : Successfully completed request
2017-07-26 21:07:15.509 DEBUG 3760 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing PUT request for [/error]
2017-07-26 21:07:15.510 DEBUG 3760 --- [nio-8080-exec-7] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /error
2017-07-26 21:07:15.511 DEBUG 3760 --- [nio-8080-exec-7] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public org.springframework.web.servlet.ModelAndView com.movie.database.app.controller.error.PageNotFoundController.showPageError()]
2017-07-26 21:07:15.526 DEBUG 3760 --- [nio-8080-exec-7] o.s.web.cors.DefaultCorsProcessor        : Skip CORS processing: request is from same origin
here
2017-07-26 21:07:15.527 DEBUG 3760 --- [nio-8080-exec-7] o.s.w.s.v.ContentNegotiatingViewResolver : Requested media types are [*/*] based on Accept header types and producible media types [*/*])
2017-07-26 21:07:15.528 DEBUG 3760 --- [nio-8080-exec-7] o.s.w.servlet.view.BeanNameViewResolver  : No matching bean found for view name 'redirect:/signIn'
2017-07-26 21:07:15.528 DEBUG 3760 --- [nio-8080-exec-7] o.s.w.s.v.ContentNegotiatingViewResolver : Returning redirect view [org.springframework.web.servlet.view.RedirectView: unnamed; URL [/signIn]]
2017-07-26 21:07:15.528 DEBUG 3760 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet        : Rendering view [org.springframework.web.servlet.view.RedirectView: unnamed; URL [/signIn]] in DispatcherServlet with name 'dispatcherServlet'
2017-07-26 21:07:15.530 DEBUG 3760 --- [nio-8080-exec-7] o.s.web.servlet.DispatcherServlet        : Successfully completed request
2017-07-26 21:07:15.534 DEBUG 3760 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing PUT request for [/signIn]
2017-07-26 21:07:15.535 DEBUG 3760 --- [nio-8080-exec-9] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /signIn
2017-07-26 21:07:15.536 DEBUG 3760 --- [nio-8080-exec-9] .m.m.a.ExceptionHandlerExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not supported
2017-07-26 21:07:15.536 DEBUG 3760 --- [nio-8080-exec-9] .w.s.m.a.ResponseStatusExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not supported
2017-07-26 21:07:15.536 DEBUG 3760 --- [nio-8080-exec-9] .w.s.m.s.DefaultHandlerExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not supported
2017-07-26 21:07:15.536  WARN 3760 --- [nio-8080-exec-9] o.s.web.servlet.PageNotFound             : Request method 'PUT' not supported
2017-07-26 21:07:15.536 DEBUG 3760 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet        : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling
2017-07-26 21:07:15.536 DEBUG 3760 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet        : Successfully completed request
2017-07-26 21:07:15.537 DEBUG 3760 --- [nio-8080-exec-9] o.s.web.servlet.DispatcherServlet        : DispatcherServlet with name 'dispatcherServlet' processing PUT request for [/error]
2017-07-26 21:07:15.538 DEBUG 3760 --- [nio-8080-exec-9] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /error
2017-07-26 21:07:15.539 DEBUG 3760 --- [nio-8080-exec-9] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public org.springframework.web.servlet.ModelAndView com.movie.database.app.controller.error.PageNotFoundController.showPageError()]
2017-07-26 21:07:15.539 DEBUG 3760 --- [nio-8080-exec-9] o.s.web.cors.DefaultCorsProcessor        : Skip CORS processing: request is from same origin
最重要的错误是

2017-07-26 21:07:15.506 DEBUG 3760 --- [nio-8080-exec-7] .w.s.m.s.DefaultHandlerExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
2017-07-26 21:07:15.536 DEBUG 3760 --- [nio-8080-exec-9] .m.m.a.ExceptionHandlerExceptionResolver : Resolving exception from handler [null]: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not supported
2017-07-26 21:07:15.536  WARN 3760 --- [nio-8080-exec-9] o.s.web.servlet.PageNotFound             : Request method 'PUT' not supported

您的错误消息告诉您您的问题:
Content-type'application/x-www-form-urlencoded;字符集=UTF-8'不受支持

添加
contentType:'application/json;charset=utf-8',
到您的ajax调用。您还需要对整个JSON字符串调用
JSON.stringify
。查看下面复制/粘贴的

var obj = { "name":"John", "age":30, "city":"New York"};
var myJSON = JSON.stringify(obj);

您的错误消息告诉您您的问题:
Content-type'application/x-www-form-urlencoded;字符集=UTF-8'不受支持

添加
contentType:'application/json;charset=utf-8',
到您的ajax调用。您还需要对整个JSON字符串调用
JSON.stringify
。查看下面复制/粘贴的

var obj = { "name":"John", "age":30, "city":"New York"};
var myJSON = JSON.stringify(obj);

电子邮件是否按照json的要求使用引号?你的价值在引号里吗?你的JSON需要精确。引号呢?在这里“email:JSON.stringify($(“#email”).val()”,我直接从文本框发送数据。您的整个JSON字符串需要成为
JSON.stringify的参数,而不仅仅是值
{“email”:“$('#email').val()”}
应该是json.stringify中的内容,而不仅仅是
$('#email')。val()
我不会为您编写代码。看看您在通话中发送的数据需要是JSON。这将不会在“
data
字段:
data:{email:data},
我爱你”中生成有效的json。一切正常。谢谢最后一个问题。为什么我必须在Spring Security中禁用scrf()?电子邮件是否按照json的要求使用引号?你的价值在引号里吗?你的JSON需要精确。引号呢?在这里“email:JSON.stringify($(“#email”).val()”,我直接从文本框发送数据。您的整个JSON字符串需要成为
JSON.stringify的参数,而不仅仅是值
{“email”:“$('#email').val()”}
应该是json.stringify中的内容,而不仅仅是
$('#email')。val()
我不会为您编写代码。看看您在通话中发送的数据需要是JSON。这将不会在“
data
字段:
data:{email:data},
我爱你”中生成有效的json。一切正常。谢谢最后一个问题。为什么我必须在Spring Security中禁用scrf()?可能重复的