REST API가 있습니다. 나는 고객을 만들고있다. 휴식 서비스 인증을 위해 스프링 보안을 구성했습니다. API의 내 REST API를 컨트롤러봄 mvc RestTemplate postForObject가 REST API에 매개 변수를 보내지 않습니다.
@RequestMapping(value = "/login", method = RequestMethod.POST, headers="content-type=application/json")
public @ResponseBody
UserDetails loginUser(WebRequest request)
throws LoginOrPasswordException {
logger.info("login user");
logger.info(request);
String username = request.getParameter("username");
logger.info(username);
UserDetails uDetails = userRepository.login(username);
System.out.println(uDetails.getPassword());
return uDetails;
}
응용 프로그램 컨텍스트
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<mvc:annotation-driven />
<context:component-scan base-package="com.zayats." />
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</list>
</property>
</bean>
<!-- Http Json MessageConverter -->
<bean id="jsonConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
</bean>
<import resource="jdbc-config.xml" />
다음 restTemplate
HashMap<String, String> params = new HashMap<String, String>();
params.put("username", username);
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<?> httpEntity = new HttpEntity<Object>(params, requestHeaders);
logger.info("Create map and doing request.");
UserDetails matchingUser = restTemplate.postForObject(
"http://localhost:8080/ApiFamilyShoplist/user/login", httpEntity,
UserDetails.class);
나머지 템플릿 설정과 클라이언트 방법입니다
,536,<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<property name="messageConverters">
<list>
<bean id="jsonConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
</bean>
</list>
</property>
</bean>
API 컨트롤러에서 postForObject를 사용하여 API를 호출 할 때 매개 변수가 수신되지 않습니다. 데이터를 API로 보내는 방법에 대해 조언 해주세요. 감사합니다. .
어떤 방법으로'@ RequestBody'입니까? API 컨트롤러에서? –
@ user2391060 요청 매개 변수 (POST 또는 GET)를 가져 오려고하지만 JSON을 게시하고 있습니다. HttpServletRequest 엔티티에는 매개 변수가 없습니다. 따라서 Spring에게 요청 본문을 JSON 객체로 변환해야한다고 알려줄 필요가있다. @RequestBody Map params는 컨트롤러 메소드의 매개 변수 여야합니다. –
제 편을 좀보세요. – Avi