How can I prevent Spring MVC from doing a redirect?

前端 未结 3 1156
野趣味
野趣味 2020-12-31 15:32

I want to handle an AJAX request that updates an entity. I don\'t really need it to return anything. The problem is that Spring MVC insists on sending a redirect to the same

3条回答
  •  甜味超标
    2020-12-31 16:31

    So,

    I took your code and created an application with that, tried sending a PUT request using browser plugin POSTMAN and got a response but no redirection. See if this works. I am attaching the complete classes, you can copy and directly use it in ur application.

    Here are the network headers:

    Remote Address:::1:8080
    Request URL:http://localhost:8080/test/api/testrun/hello
    Request Method:PUT
    Status Code:200 OK
    
    **Request Headers**
    Accept:*/*
    Accept-Encoding:gzip,deflate,sdch
    Accept-Language:en-GB,en-US;q=0.8,en;q=0.6,pl;q=0.4
    Authorization:Basic ZWFzeWwtbWFpbi1pbnQ6cHJnc3VzZXI=
    Cache-Control:no-cache
    Connection:keep-alive
    Content-Length:0
    Content-Type:application/json
    Host:localhost:8080
    Origin:chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm
    User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36
    
    **Response Headers**
    Cache-Control:private
    Content-Length:5
    Content-Type:text/plain;charset=ISO-8859-1
    Date:Tue, 10 Jun 2014 10:58:50 GMT
    Expires:Thu, 01 Jan 1970 05:30:00 IST
    Server:Apache-Coyote/1.1
    ConsoleSearchEmulationRendering
    

    And here is the code: Configuration to boot up spring

    @Configuration
    @EnableWebMvc
    @Profile("production")
    @ComponentScan(basePackages = "com", excludeFilters = { @ComponentScan.Filter(Configuration.class) })
    public class WebConfig extends WebMvcConfigurationSupport {
    
        @Override
        protected void configureContentNegotiation(
                ContentNegotiationConfigurer configurer) {
            configurer.favorPathExtension(false).favorParameter(true)
                    .parameterName("mediaType").ignoreAcceptHeader(true)
                    .useJaf(false).defaultContentType(MediaType.APPLICATION_JSON)
                    .mediaType("xml", MediaType.APPLICATION_XML)
                    .mediaType("json", MediaType.APPLICATION_JSON);
        }
    
        @Bean(name = "viewResolver")
        public InternalResourceViewResolver viewResolver() throws Exception {
            InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    
            viewResolver.setPrefix("/jsp/");
            viewResolver.setSuffix(".jsp");
    
            return viewResolver;
        }
    }
    

    Controller

    @Controller
    public class TestController {
    
        @RequestMapping(value = "/api/testrun/{testrunId}", method = RequestMethod.PUT, consumes = "application/json")
        @ResponseBody
        public String updateOverview(@PathVariable("testrunId") final String testrunId) {
            System.out.println(testrunId);
    
            return "hello";
        }
    }
    

    web.xml

    
    
    test
    
        
          contextClass
          
             org.springframework.web.context.support.AnnotationConfigWebApplicationContext
          
        
        
          contextConfigLocation
          com.WebConfig
        
        
            spring.profiles.active
            production
        
        
          org.springframework.web.context.ContextLoaderListener
        
    
        
          ui
          
             org.springframework.web.servlet.DispatcherServlet
          
          
             contextClass
             
                org.springframework.web.context.support.AnnotationConfigWebApplicationContext
             
          
          1
        
        
          ui
          /
         
    
    

提交回复
热议问题