HTTP状态400 – springboot + resttemplate的错误请求问题

时间:2019-06-28 07:04:10

标签: java resttemplate

Xx problem starting with springboot + resttemplate
Cannot find template location(s): [classpath:/templates/] (please add some templates, check your FreeMarker configuration, or set spring.freemarker.checkTemplateLocation=false)
and problems with Error parsing HTTP request header
 Note: further occurrences of HTTP request parsing errors will be logged at DEBUG level.

pojo
public class Lock {
    private String carNo;
    private String cardNumber;
    private String lockReason;
    @Override
    public String toString() {
        return "Lock{" +
                "carNo='" + carNo + '\'' +
                ", cardNumber='" + cardNumber + '\'' +
                ", lockReason='" + lockReason + '\'' +
                '}';
    }


    public String getCarNo() {
        return carNo;
    }

    public void setCarNo(String carNo) {
        this.carNo = carNo;
    }

    public String getCardNumber() {
        return cardNumber;
    }

    public void setCardNumber(String cardNumber) {
        this.cardNumber = cardNumber;
    }

    public String getLockReason() {
        return lockReason;
    }

    public void setLockReason(String lockReason) {
        this.lockReason = lockReason;
    }

}


public class Result {
    private Map<String,String> data;
    private String message;
    private int resultCode;
    private int status;
    @Override
    public String toString() {
        return "Result{" +
                "data=" + data +
                ", message='" + message + '\'' +
                ", resultCode=" + resultCode +
                ", status=" + status +
                '}';
    }





    public Map<String, String> getData() {
        return data;
    }

    public void setData(Map<String, String> data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getResultCode() {
        return resultCode;
    }

    public void setResultCode(int resultCode) {
        this.resultCode = resultCode;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }


}













@SpringBootApplication(exclude=DataSourceAutoConfiguration.class)
@ComponentScan("com.carlock.lock.controller")
public class App {

    @Bean 
    public RestTemplate restTemplate() {
        HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
        httpRequestFactory.setConnectionRequestTimeout(30 * 1000);
        httpRequestFactory.setConnectTimeout(30 * 3000);
        httpRequestFactory.setReadTimeout(30 * 3000);
        return new RestTemplate(httpRequestFactory);
    }

    public static void main(String[] args) {
        SpringApplication.run(App.class,args);
    }
}

通过依赖注入注入resttemplate     控制者     @RestController     @RequestMapping(“ / carlock”)     //允许跨域访问     @CrossOrigin(origins =“ *”,maxAge = 3600)     公共类LockController {             //注入RestTemplate             私有最终RestTemplate restTemplate;             @Autowired             公共LockController(RestTemplate restTemplate){                 this.restTemplate = restTemplate;             }

        /**
         * 
         *
         * @param lockstatus 
         * @return
         */
        @PostMapping(value = "/lock",produces = MediaType.APPLICATION_JSON_VALUE)
        public Result Carlock(@RequestBody(required=false) Lock lockstatus) {
            HttpEntity<Lock> entity = new HttpEntity<>(lockstatus);
            ResponseEntity<Result> resultResponseEntity = this.restTemplate.exchange(
                    "https://xxx-test.xxx.xin/xx/api/v2/lockCar/entity",
                    HttpMethod.POST, entity, Result.class);
            if (resultResponseEntity.getStatusCode() == HttpStatus.OK) {
                return resultResponseEntity.getBody();
            }
                return null;
        }


The expectation is to return a json string

0 个答案:

没有答案