Last active
April 20, 2018 17:20
-
-
Save vamdt/40729ec4a9ddedd7fa729e3782ca3326 to your computer and use it in GitHub Desktop.
SpringMVC forward request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vamdt.fox.fox.service; | |
import org.springframework.beans.factory.BeanFactoryUtils; | |
import org.springframework.beans.factory.InitializingBean; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.context.ApplicationContext; | |
import org.springframework.stereotype.Service; | |
import org.springframework.util.Assert; | |
import org.springframework.web.method.HandlerMethod; | |
import org.springframework.web.servlet.ModelAndView; | |
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; | |
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import java.util.List; | |
import java.util.Map; | |
@Service | |
public class ForwardService implements InitializingBean { | |
@Autowired | |
private ApplicationContext applicationContext; | |
private RequestMappingHandlerMapping handlerMapping; | |
private RequestMappingHandlerAdapter handlerAdapter; | |
@Override | |
public void afterPropertiesSet() throws Exception { | |
Map<String, RequestMappingHandlerAdapter> handlerAdapterMap = | |
BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, RequestMappingHandlerAdapter.class, true, false); | |
if (!handlerAdapterMap.isEmpty()) { | |
this.handlerAdapter = handlerAdapterMap.values().iterator().next(); | |
} | |
Map<String, RequestMappingHandlerMapping> handlerMappingMap = | |
BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, RequestMappingHandlerMapping.class, true, false); | |
if (!handlerMappingMap.isEmpty()) { | |
this.handlerMapping = handlerMappingMap.values().iterator().next(); | |
} | |
} | |
public ModelAndView forward(String mappingName, HttpServletRequest request, HttpServletResponse response) throws Exception { | |
List<HandlerMethod> handlerMethods = this.handlerMapping.getHandlerMethodsForMappingName(mappingName); | |
Assert.notEmpty(handlerMethods, "mapping name " + mappingName + " has not handler method!"); | |
Assert.isTrue(handlerMethods.size() < 2, "mapping name " + mappingName + " has more than one handler method!"); | |
HandlerMethod handlerMethod = handlerMethods.get(0).createWithResolvedBean(); | |
return handlerAdapter.handle(request, response, handlerMethod); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vamdt.fox.fox; | |
import com.vamdt.fox.fox.service.ForwardService; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Controller; | |
import org.springframework.web.bind.annotation.PostMapping; | |
import org.springframework.web.bind.annotation.ResponseBody; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
@Controller | |
public class HelloController { | |
@Autowired | |
private ForwardService forwardService; | |
@PostMapping("/hello") | |
@ResponseBody | |
public String homeIsHome(HttpServletRequest request, HttpServletResponse response) throws Exception { | |
forwardService.forward("PC#pong", request, response); | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.vamdt.fox.fox; | |
import com.alibaba.fastjson.JSONObject; | |
import org.springframework.stereotype.Controller; | |
import org.springframework.web.bind.annotation.PostMapping; | |
import org.springframework.web.bind.annotation.RequestBody; | |
import org.springframework.web.bind.annotation.ResponseBody; | |
@Controller | |
public class PingController { | |
@PostMapping("/pong") | |
@ResponseBody | |
public String pong(@RequestBody JSONObject jsonObject) { | |
return jsonObject.getString("name"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
curl -i -X POST -H "Content-Type:application/json" -d '{"name":"dahai"}' 'http://localhost:8080/hello' | |
--- | |
> POST /hello HTTP/1.1 | |
> User-Agent: curl/7.35.0 | |
> Host: localhost:8080 | |
> Accept: */* | |
> Content-Type:application/json | |
> Content-Length: 16 | |
> | |
* upload completely sent off: 16 out of 16 bytes | |
< HTTP/1.1 200 | |
HTTP/1.1 200 | |
< Content-Type: text/plain;charset=UTF-8 | |
Content-Type: text/plain;charset=UTF-8 | |
< Content-Length: 5 | |
Content-Length: 5 | |
< Date: Fri, 20 Apr 2018 17:09:48 GMT | |
Date: Fri, 20 Apr 2018 17:09:48 GMT | |
< | |
* Connection #0 to host localhost left intact | |
dahai |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment