Skip to content

Instantly share code, notes, and snippets.

@vamdt vamdt/ForwardService.java
Last active Apr 20, 2018

Embed
What would you like to do?
SpringMVC forward request
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);
}
}
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;
}
}
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");
}
}
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
You can’t perform that action at this time.