我正在尝试在我的mvc控制器中使用rest调用,但每次执行此操作都会返回一个http状态为302的null主体。此外,我使用带有spring security的spring boot来获取https。
我已经关注了此处的代码示例:http://websystique.com/springmvc/spring-mvc-4-restful-web-services-crud-example-resttemplate/ 和Get list of JSON objects with Spring RestTemplate但是这些都不起作用
有人可以指出我正确的方向吗
谢谢,
REST
@RequestMapping(value = "/api/*")
@RestController
public class PostApiController {
static final Logger logger = LogManager.getLogger(PostApiController.class.getName());
private final PostService postService;
@Inject
public PostApiController(final PostService postService) {
this.postService = postService;
}
//-------------------Retrieve All Posts--------------------------------------------------------
@RequestMapping(value = "post", method = RequestMethod.GET)
public ResponseEntity<List<Post>> getAllPosts() {
List<Post> posts = postService.findAllPosts();
if(posts.isEmpty()){
return new ResponseEntity<List<Post>>(HttpStatus.NO_CONTENT);//You many decide to return HttpStatus.NOT_FOUND
}
return new ResponseEntity<List<Post>>(posts, HttpStatus.OK);
}
}
控制器
@Controller
public class PostController {
static final Logger logger = LogManager.getLogger(PostController.class.getName());
public static final String REST_SERVICE_URI = "http://localhost:8080/api"; //"http://localhost:8080/api";
private final PostService postService;
@Inject
public PostController(final PostService postService) {
this.postService = postService;
}
@SuppressWarnings("unchecked")
@RequestMapping(value = "/getAll")
// public String create(@Valid Post post, BindingResult bindingResult, Model
// model) {
public ModelAndView getAll() {
// if (bindingResult.hasErrors()) {
// return "mvchome";
// }
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<List<Post>> responseEntity = restTemplate.exchange(REST_SERVICE_URI+"/post",HttpMethod.GET, null, new ParameterizedTypeReference<List<Post>>() {});
// ResponseEntity<Post[]> responseEntity = restTemplate.getForEntity(REST_SERVICE_URI+"/post", Post[].class);
List<Post> postsMap = responseEntity.getBody();
MediaType contentType = responseEntity.getHeaders().getContentType();
HttpStatus statusCode = responseEntity.getStatusCode();
// List<LinkedHashMap<String, Object>> postsMap = restTemplate.getForObject(REST_SERVICE_URI+"/post", List.class);
// String s= REST_SERVICE_URI+"/post";
// logger.info(s);
if(postsMap!=null){
for(Post map : postsMap){
logger.info("User : id="+map.getUid());
}
}else{
logger.info("No user exist----------");
}
//List<Post> postList = postService.findAllPosts();
ModelAndView mav = new ModelAndView("mvchome");
mav.addObject("postsList", postsMap);
Post newpost = new Post();
mav.addObject("post", newpost);
return mav;
}
}
*****修复我的问题我修改了我的代码,只是在选择网址路径上进行重定向而不是&#34; / *&#34;
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory tomcat =
new TomcatEmbeddedServletContainerFactory() {
@Override
protected void postProcessContext(Context context) {
SecurityConstraint securityConstraint = new SecurityConstraint();
securityConstraint.setUserConstraint("CONFIDENTIAL");
SecurityCollection collection = new SecurityCollection();
//used to be just collection.addPattern("/*"); now I changed it to specify which path I want it to redirect
collection.addPattern("/mvchome/*");
collection.addPattern("/home/*");
securityConstraint.addCollection(collection);
context.addConstraint(securityConstraint);
}
};
tomcat.addAdditionalTomcatConnectors(createHttpConnector());
return tomcat;
}
答案 0 :(得分:0)
http状态302通常是由错误的网址设置引起的。
首先,确保调用public ResponseEntity<List<Post>> getAllPosts() {}
方法(只在其中打印List<Post>
结果)。
如果它被正确调用,您可以在public ModelAndView getAll() {}
内获得返回值。
问题应该是public ModelAndView getAll() {}
方法的指示设置。
检查您的web.xml或spring配置是否出错。请注意重定向到视图的配置以及调度程序servlet的url映射。
如果调用public ResponseEntity<List<Post>> getAllPosts() {}
但您无法获得返回值,那么应该是指导public ResponseEntity<List<Post>> getAllPosts() {}
方法设置的问题。
检查你的spring配置和web.xml。可能的原因通常是在配置和web.xml中滥用通配符,或者只是忽略了错误的映射。