Announcement Announcement Module
Collapse
No announcement yet.
Spring MVC: no body written in response Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring MVC: no body written in response

    Hi there
    I'm using this environment:
    • Spring 3.1
    • Tomcat 7
    • Dojo 1.6.1 (as client framework)
    • Hibernate 4.4.1

    I have written this method in my controller:
    Code:
    	@RequestMapping(method = {RequestMethod.POST})
    	public  ResponseEntity<Utente> saveUser(@RequestBody Utente user, HttpServletRequest request, HttpServletResponse response,final WebRequest webReq) throws Exception {
    
    		try {
    
    			this.entityMgr.saveEntity(user);
    			//String url = request.getRequestURI().endsWith("/")?request.getRequestURI():request.getRequestURI()+"/";
    			String url = request.getRequestURL().toString().endsWith("/")?request.getRequestURL().toString():request.getRequestURL()+"/";
    			String location = url+user.getIdUtente();
    			HttpHeaders headers = new HttpHeaders();
    			headers.setLocation(new URI(location));
    			return new ResponseEntity<Utente>(user, headers, HttpStatus.CREATED);
    		} catch (Exception e) {
    
    			String message = "Error in method updateUser; error message: " + e.getMessage();
    			logger.fatal(message, e);
    			response.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
    			throw e;
    		}
    Now when i submit a form all works pretty good. The entity id successully saved and i saw this logs:
    Code:
    2012-04-11 17:57:07,956 DEBUG [org.springframework.web.servlet.mvc.method.annotation.HttpEntityMethodProcessor] Written [[it.olegna.optical.model.Utente@1626343, it.olegna.optical.model.Utente@6300f0, it.olegna.optical.model.Utente@1d2bbf2, it.olegna.optical.model.Utente@31421e, it.olegna.optical.model.Utente@1ebde71]] as "application/json" using [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter@8e96fc]
    2012-04-11 17:57:07,956 DEBUG [org.springframework.web.servlet.DispatcherServlet] Null ModelAndView returned to DispatcherServlet with name 'pubServlet': assuming HandlerAdapter completed request handling
    2012-04-11 17:57:07,956 DEBUG [org.springframework.web.servlet.DispatcherServlet] Successfully completed request
    But by monitoring the response by using firebug and fiddler i realized that there is nothing written in response body.
    Here there are the request and response headers (and on these headers i should have no problems):
    REQUEST HEADERS
    Code:
    Accept application/json,application/javascript
    Accept-Encoding	gzip, deflate
    Accept-Language	en-us,it;q=0.5
    Cache-Control	no-cache
    Content-ID	</Ottico/opt/userList/b03105b5776b@client>
    Content-Length	29
    Content-Type	application/json; charset=UTF-8
    Cookie	JSESSIONID=D8CB24B2FEA43A56E1EF01A10119651D
    Host	localhost:8080
    Pragma	no-cache
    Proxy-Connection	keep-alive
    Referer	http://localhost:8080/Ottico/opt/users
    Transaction	commit
    User-Agent	Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko/20100101 Firefox/11.0
    X-Requested-With	XMLHttpRequest
    RESPONSE HEADERS
    Code:
    Content-Length	196
    Content-Type	application/json
    Date	Wed, 11 Apr 2012 16:06:28 GMT
    Etag	"0a2dd11370aec0dc44acbb2063746eaac"
    Location	http://localhost:8080/Ottico/opt/userList/322
    Server	Apache-Coyote/1.1
    Now in red i highlighted the fact that the content lenght should be 196 byte but, after more or less 20 seconds, the POST finishes and nothing is written in the response body; i'm not able in finding where i'm wrong.

    Can anybody tell me the reason of this problem?
    Thank you
    Angelo

  • #2
    Hi
    I was investigating on this topic and i realized that the method successfully write the new object in response.
    But the issue is related to the fact that i'm using the spring ETag support. In fact in my web.xml i have the following filter configured:
    Code:
     	
    <filter>
    		<filter-name>eTagFilter</filter-name>
    		<filter-class>org.springframework.web.filter.ShallowEtagHeaderFilter</filter-class>
     	</filter>	 		
    	<filter-mapping>
    		<filter-name>eTagFilter</filter-name>
    		<servlet-name>pubServlet</servlet-name>
     	</filter-mapping>
    As we can see the filter is always active. If I don't use the filter all works like a charm.
    Now I'ld like to mantain the ETag filter and find a way to tell the filter that the content i'm writing in the response is a new content.

    I saw that this filter checks the If-None-Match HTTP Header.
    I noticed that in the request i receive this header is not present.
    How can i tell the filter to not return HTTP Code 304 but HTTP 201 and the new content to the client?
    Note that i also tried this:
    Code:
    @SuppressWarnings("unchecked")
    @RequestMapping(method = {RequestMethod.POST})
    public  ResponseEntity<Utente> saveUser(@RequestBody Utente user, HttpServletRequest request, HttpServletResponse response,final WebRequest webReq, @RequestHeader(value="If-None-Match", defaultValue="valoreDefault") String ifNoneMatch) throws Exception {
    
    		try {
    			Enumeration<String> names = request.getHeaderNames();
    			while (names.hasMoreElements()) {
    				String name = (String) names.nextElement();
    				logger.info("Nome header: "+name+" valore: "+request.getHeader(name));
    			}
    			this.entityMgr.saveEntity(user);
    			//String url = request.getRequestURI().endsWith("/")?request.getRequestURI():request.getRequestURI()+"/";
    			String url = request.getRequestURL().toString().endsWith("/")?request.getRequestURL().toString():request.getRequestURL()+"/";
    			String location = url+user.getIdUtente();
    			HttpHeaders headers = new HttpHeaders();
    			headers.setLocation(new URI(location));
    			headers.setIfNoneMatch(String.valueOf(user.getIdUtente())+user.getNome());
    			return new ResponseEntity<Utente>(user, headers, HttpStatus.CREATED);
    		} catch (Exception e) {
    
    			String message = "Error in method updateUser; error message: " + e.getMessage();
    			logger.fatal(message, e);
    			response.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
    			throw e;
    		}
    
    	}
    But also in this way it doesn't work.
    Did anybody meet this kind of issue?
    Any suggestion is welcome.

    Angelo
    Last edited by craig1980; Apr 12th, 2012, 09:57 AM.

    Comment

    Working...
    X