Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring Rest模板提供空正文和状态302_Spring_Rest_Spring Mvc_Spring Security_Spring Boot - Fatal编程技术网

Spring Rest模板提供空正文和状态302

Spring Rest模板提供空正文和状态302,spring,rest,spring-mvc,spring-security,spring-boot,Spring,Rest,Spring Mvc,Spring Security,Spring Boot,我试图在我的mvc控制器中使用rest调用,但是每次使用rest调用时,它都返回一个空正文,http状态为302。此外,我还使用spring引导和spring安全性来获取https 我在这里介绍了以下代码示例: 然而,这些都不起作用 有人能给我指一下正确的方向吗 谢谢, 休息 http状态302通常由错误的url设置引起 首先,确保调用了public ResponseEntity getAllPosts(){}方法(只需在其中打印List结果) 如果正确调用了它,并且可以在public mode

我试图在我的mvc控制器中使用rest调用,但是每次使用rest调用时,它都返回一个空正文,http状态为302。此外,我还使用spring引导和spring安全性来获取https

我在这里介绍了以下代码示例: 然而,这些都不起作用

有人能给我指一下正确的方向吗

谢谢,

休息


http状态302通常由错误的url设置引起

首先,确保调用了
public ResponseEntity getAllPosts(){}
方法(只需在其中打印
List
结果)

如果正确调用了它,并且可以在
public model和view getAll(){}
中获取返回值

问题应该是
publicmodelandview getAll(){}
方法的定向设置

检查web.xml或spring配置中是否有错误。注意重定向到视图的配置和DispatcherServlet的url映射


如果调用了
public ResponseEntity getAllPosts(){}
,但无法获取返回值,那么应该是
public ResponseEntity getAllPosts(){}
方法的定向设置问题


请检查您的spring配置和web.xml。可能的原因通常是配置和web.xml中的通配符被误用,或者只是未被注意到的错误映射。

http状态302通常是由错误的url设置引起的

首先,确保调用了
public ResponseEntity getAllPosts(){}
方法(只需在其中打印
List
结果)

如果正确调用了它,并且可以在
public model和view getAll(){}
中获取返回值

问题应该是
publicmodelandview getAll(){}
方法的定向设置

检查web.xml或spring配置中是否有错误。注意重定向到视图的配置和DispatcherServlet的url映射


如果调用了
public ResponseEntity getAllPosts(){}
,但无法获取返回值,那么应该是
public ResponseEntity getAllPosts(){}
方法的定向设置问题


请检查您的spring配置和web.xml。可能的原因通常是配置和web.xml中的通配符被误用,或者只是未被注意到的错误映射。

我发现,因为我正在执行从http到https的重定向,所以会影响rest模板调用。为了解决这个问题,我修改了
EmbeddedServletContainerFactory
的代码,使其仅重定向选定的URL。当我这样做的时候,我留下了我的rest模板代码。感谢您的帮助,我发现因为我正在执行从http到https的重定向,所以会影响rest模板调用。为了解决这个问题,我修改了
EmbeddedServletContainerFactory
的代码,使其仅重定向选定的URL。当我这样做的时候,我留下了我的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;

    }
 }
 @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;
  }