Mvc denetleyicimde bir aramayı tüketmeye çalışıyorum, ancak her yaptığımda 302 http durumuna sahip boş bir beden döndürür. Ayrıca bahar güvenliği ile bahar önyüklemeyi kullanıyorum. https al.Boş gövdeyi ve durumu veren şablon dinlenme 302
buradan kod örnekleri takip ettim: Bu işin http://websystique.com/springmvc/spring-mvc-4-restful-web-services-crud-example-resttemplate/ ve Get list of JSON objects with Spring RestTemplate ancak hiçbiri
birisi
teşekkür ederiz bana doğru yönde işaret edebilir,
DİNLENME
@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);
}
}
Denetleyici
@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;
}
}
***** benim sorunu gidermek için sadece url seç yollarında bir yönlendirme yapmak yerine için kodumu modifiye "/ *"
@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;
}
Bunu http - https'den yönlendirme yaptığım için geri arama şablonu çağrısını etkilediğini öğrendim. Düzeltmek için, yalnızca seçili URL'ler için yeniden yönlendirmek üzere 'EmbeddedServletContainerFactory' kodumu değiştirdim. Bunu yaptığımda, o zamanki şablon şablon kodumu bıraktığımda ve işe yaradı. Yardım için teşekkürler – user2184653