Tuesday, 15 April 2014

java - Spring MVC RequestMapping is being forwarded -



java - Spring MVC RequestMapping is being forwarded -

i have rest service based on spring mvc.

this code:

public class sitescontroller { @requestmapping(value="/rest/sites/{id}", method=requestmethod.get) @responsebody public sitedto getsite(@pathvariable string id) { integer siteid = integer.parseint(id); site site = cms.getsite(siteid); sitedto siteresult = new sitedto(site); homecoming siteresult; } @requestmapping(value="/rest/sites", method=requestmethod.get) public sitesresult getsites(@requestparam integer companyid) { collection<site> sites = cms.getsites(cms.getcompany(companyid)); sitesresult sitesresult = new sitesresult(sites); homecoming sitesresult; } }

(i skipped code doesn't apply problem)

when go url /rest/sites/1 returning info expect, when go /rest/sites?companyid=1 404 page: http status 404 - /rest/rest/sites.

the log showing code in getsitesfunction run, after log showing following: org.springframework.web.servlet.view.jstlview forwarding resource [rest/sites] in internalresourceview 'rest/sites'

why redirected instead of executed?

update

found problem. because didn't have @responsebody above method, dispatcher forwarded request. more info here, key thing if method annotated @responsebody, homecoming type written response http body. homecoming value converted declared method argument type using httpmessageconverters.

because method homecoming type sitesresult not 1 of supported homecoming types, spring add together returned object model using class name , seek render view named value of request mapping, why trying render /rest/sites. it's not doing http forward, dispatcher forwards servlets render view (eg. jsp).

if want homecoming specific view, homecoming string containing name.

instead

@requestmapping(value="/rest/sites", method=requestmethod.get) public sitesresult getsites(@requestparam integer companyid) { collection<site> sites = cms.getsites(cms.getcompany(companyid)); sitesresult sitesresult = new sitesresult(sites); homecoming sitesresult; }

do this

@requestmapping(value="/rest/sites", method=requestmethod.get) public string getsites(@requestparam integer companyid, model model) { collection<site> sites = cms.getsites(cms.getcompany(companyid)); sitesresult sitesresult = new sitesresult(sites); model.addattribute("sitesresult", sitesresult); string myview = "myview"; homecoming myview; }

java spring rest spring-mvc

No comments:

Post a Comment