2016-12-07 1 views
0

스프링 4로 REST 서비스에서 POST 메소드를 테스트하려고합니다. MockMvcBuilders.standaloneSetup과의 통합 테스트에서 제대로 작동하지만 Tomcat 8.5.8 및 왜 작동하지 않는지 나는 이해할 수 없다. 나는이 메시지가 나타납니다 :스프링 4, REST 서비스가 Tomcat에서 415 오류를 반환합니다.

       HTTP Status 415 - 

    type Status report 

    message 

    description The server refused this request because the request entity 
    is in a format not supported by the requested resource for the 
    requested method. 
    ________________ 

을하지만 톰캣 서버 (와이어 샤크에서 추출)이 요청 수신 : 그것은 좋은 것 같다

POST /app-1.0-SNAPSHOT/v1.0/concentrators/ HTTP/1.1 
User-Agent: curl/7.35.0 
Host: x.x.x.x:8080 
Accept: */* 
Content-Type: application/json 
Content-Length: 528 {"street":"street","num":"num","code":"code","town":"town" 
,"descriptionRoom":"descriptionRoom","btPwd":"btPwd" 
,"gprsPwd":"gprsPwd","gprsLogin":"gprsLogin","gprsApn":"gprsApn" 
,"gprsPin":"gprsPin","motherboardId":"motherboardId","logLevel":0 
,"frameSendPeriod":0,"frameCapturePeriod":0,"mQTTServerPwd":"mQTTServerPwd" 
,"mQTTServerUser":"mQTTServerUser","mQTTServerIp":"mQTTServerIp" 
,"ftpServerPwd":"ftpServerPwd","ftpServerUser":"ftpServerUser" 
, "ftpServerIp":"ftpServerIp","numSerie" :"numSerie","idConcentrator":-1,"infoCameras":[]} 

실수 어디 있는지 모르겠어요.

내 REST 서비스는 다음과 같습니다 또한

@RequestMapping(value = "/", method = RequestMethod.POST, consumes = { "application/json" }, produces = { "application/json" }) 
public ResponseEntity<String> post(@RequestBody ConfigurationConcentrator configurationConcentrator) { 
    HttpHeaders headers = new HttpHeaders(); 
    headers.setContentType(MediaType.APPLICATION_JSON); 
    try { 
     configureConcentrator.create(configurationConcentrator); 
    } catch (IncorrectDBOperation | ErrorSendMessageException e) { 
     return new ResponseEntity<String>(e.getMessage(), headers, HttpStatus.BAD_REQUEST); 
    } 
    return new ResponseEntity<String>(HttpStatus.CREATED); 
} 

, 그것은 잭슨 종속성 (데이터 바인딩, 코어 및 주석)를 확인하고 그들은 .war 파일에 추가됩니다.

것 같다
curl -X POST -H "Content-Type: application/json" --data @addConcentrator2.json http://x.x.x.x:8080/app-1.0-SNAPSHOT/v1.0/concentrators/ 

이, 그것은 정확하게 요격이 필요가있는 해결할 수없는,하지만 난 문제가 표시되지 않습니다 :

@RequestMapping(value = "/{id}", method = RequestMethod.GET, consumes = { "application/json" }, produces = { "application/json" }) 
    public @ResponseBody ResponseEntity<ConfigurationConcentrator> get(@PathVariable("id") String id) { 
.... 
    } 

    @RequestMapping(value = "/", method = RequestMethod.POST, consumes = { "application/json" }, produces = { "application/json" }) 
    public ResponseEntity<String> post(@RequestBody ConfigurationConcentrator configurationConcentrator) { 
.... 

    } 

    @RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = { "application/json" }, produces = { "application/json" }) 
    public ResponseEntity<String> put(@PathVariable("id") String id, @RequestBody ConfigurationConcentrator configurationConcentrator) { 
.... 

    } 

    @RequestMapping(value = "/{id}/cameras/{idcamera}", method = RequestMethod.PUT, consumes = { "application/json" }, produces = { "application/json" }) 
    public ResponseEntity<String> putCameras(@PathVariable("id") String id, @PathVariable("idcamera") String idCamera, 
.... 

    } 

    @RequestMapping(value = "/{id}/cameras", method = RequestMethod.POST, consumes = { "application/json" }, produces = { "application/json" }) 
    public ResponseEntity<String> postCameras(@PathVariable("id") String id, @RequestBody List<ConfigurationCamera> configurationCameras) { 
.... 
    } 

    @RequestMapping(value = "/{id}/initialconfiguration", method = RequestMethod.GET, consumes = { "application/json" }, produces = { "application/json" }) 
    public ResponseEntity<StatusInitialConfiguration> getInitialConfiguration(@PathVariable("id") String numSerie) { 
.... 


    } 

갱신 1 : 나는 업데이트 을 이 내 curl 명령이다 curl 명령을 Accept 헤더와 함께 사용하십시오.

curl -X POST -H "Content-Type: application/json" -H "Accept: application/json" --data @addConcentrator2.json http://x.x.x.x:8080/app-1.0-SNAPSHOT/v1.0/concentrators/ 

UPDATE2 :

구성 집중

0 : JSON을 매핑

클래스 :

06:36:41.011 [http-nio-8080-exec-8] DEBUG org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping - Mapping [/v1.0/concentrators/1/camera 
s] to HandlerExecutionChain with handler [[email protected]] and 2 interceptors 
06:36:41.011 [http-nio-8080-exec-8] DEBUG org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver - Resolving exception from han 
dler [[email protected]]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported 
06:36:41.011 [http-nio-8080-exec-8] DEBUG org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver - Resolving exception from handler [gas 
[email protected]]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported 
06:36:41.011 [http-nio-8080-exec-8] DEBUG org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Resolving exception from handler [gasnat 
[email protected]]: org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported 
06:36:41.011 [http-nio-8080-exec-8] DEBUG org.springframework.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'dispatc 
her': assuming HandlerAdapter completed request handling 
06:36:41.011 [http-nio-8080-exec-8] DEBUG org.springframework.web.servlet.DispatcherServlet - Successfully completed request 

갱신 3 : 내가 생각, 두 개의 인터셉터를 감지하는

package gasnatural.models; 

import java.util.ArrayList; 
import java.util.List; 

public class ConfigurationConcentrator { 
    public static class InfoCamera { 
     public Long idCamera; 
     public String numSerie; 
    } 

    private String street = null; 
    private String num = null; 
    private String code = null; 
    private String town = null; 

    private String descriptionRoom = null; 

    private String btPwd = null; 
    private String gprsPwd = null; 
    private String gprsLogin = null; 
    private String gprsApn = null; 
    private String gprsPin = null; 
    private String motherboardId = null; 
    private int logLevel = -1; 
    private int frameSendPeriod = -1; 
    private int frameCapturePeriod = -1; 
    private String mQTTServerPwd = null; 
    private String mQTTServerUser = null; 
    private String mQTTServerIp = null; 
    private String ftpServerPwd = null; 
    private String ftpServerUser = null; 
    private String ftpServerIp = null; 
    private String numSerie = null; 

    private Long idConcentrator; 
    private List<InfoCamera> infoCameras; 

    public ConfigurationConcentrator() { 

    } 

    public ConfigurationConcentrator(String street, String num, String town, String code, String descriptionRoom, String btPwd, String gprsPwd, 
      String gprsLogin, String gprsApn, String gprsPin, String motherboardId, int logLevel, int frameSendPeriod, int frameCapturePeriod, 
      String mQTTServerPwd, String mQTTServerUser, String mQTTServerIp, String ftpServerPwd, String ftpServerUser, String ftpServerIp, String numSerie) { 

     this(new Long(-1), street, num, town, code, descriptionRoom, btPwd, gprsPwd, gprsLogin, gprsApn, gprsPin, motherboardId, logLevel, frameSendPeriod, 
       frameCapturePeriod, mQTTServerPwd, mQTTServerUser, mQTTServerIp, ftpServerPwd, ftpServerUser, ftpServerIp, numSerie, 
       new ArrayList<InfoCamera>()); 
    } 

    public ConfigurationConcentrator(Long idConcentrator, String street, String num, String town, String code, String descriptionRoom, String btPwd, 
      String gprsPwd, String gprsLogin, String gprsApn, String gprsPin, String motherboardId, int logLevel, int frameSendPeriod, int frameCapturePeriod, 
      String mQTTServerPwd, String mQTTServerUser, String mQTTServerIp, String ftpServerPwd, String ftpServerUser, String ftpServerIp, String numSerie, 
      List<InfoCamera> infoCameras) { 

     this.descriptionRoom = descriptionRoom; 
     this.btPwd = btPwd; 
     this.gprsPwd = gprsPwd; 
     this.gprsLogin = gprsLogin; 
     this.gprsApn = gprsApn; 
     this.gprsPin = gprsPin; 
     this.motherboardId = motherboardId; 
     this.logLevel = logLevel; 
     this.frameSendPeriod = frameSendPeriod; 
     this.frameCapturePeriod = frameCapturePeriod; 
     this.mQTTServerPwd = mQTTServerPwd; 
     this.mQTTServerUser = mQTTServerUser; 
     this.mQTTServerIp = mQTTServerIp; 
     this.ftpServerPwd = ftpServerPwd; 
     this.ftpServerUser = ftpServerUser; 
     this.ftpServerIp = ftpServerIp; 
     this.numSerie = numSerie; 

     this.street = street; 
     this.num = num; 
     this.code = code; 
     this.town = town; 

     this.descriptionRoom = descriptionRoom; 
     this.infoCameras = infoCameras; 
     this.idConcentrator = idConcentrator; 
    } 

    public void addIdCamera(InfoCamera idCamera) { 
     this.infoCameras.add(idCamera); 
    } 

    public String getStreet() { 
     return street; 
    } 

    public String getNum() { 
     return num; 
    } 

    public String getCode() { 
     return code; 
    } 

    public String getTown() { 
     return town; 
    } 

    public String getDescriptionRoom() { 
     return descriptionRoom; 
    } 

    public String getBtPwd() { 
     return btPwd; 
    } 

    public String getGprsPwd() { 
     return gprsPwd; 
    } 

    public String getGprsLogin() { 
     return gprsLogin; 
    } 

    public String getGprsApn() { 
     return gprsApn; 
    } 

    public String getGprsPin() { 
     return gprsPin; 
    } 

    public String getMotherboardId() { 
     return motherboardId; 
    } 

    public int getLogLevel() { 
     return logLevel; 
    } 

    public int getFrameSendPeriod() { 
     return frameSendPeriod; 
    } 

    public int getFrameCapturePeriod() { 
     return frameCapturePeriod; 
    } 

    public String getmQTTServerPwd() { 
     return mQTTServerPwd; 
    } 

    public String getmQTTServerUser() { 
     return mQTTServerUser; 
    } 

    public String getmQTTServerIp() { 
     return mQTTServerIp; 
    } 

    public String getFtpServerPwd() { 
     return ftpServerPwd; 
    } 

    public String getFtpServerUser() { 
     return ftpServerUser; 
    } 

    public String getFtpServerIp() { 
     return ftpServerIp; 
    } 

    public String getNumSerie() { 
     return numSerie; 
    } 

    public Long getIdConcentrator() { 
     return idConcentrator; 
    } 

    public List<InfoCamera> getInfoCameras() { 
     return infoCameras; 
    } 

    public void setStreet(String street) { 
     this.street = street; 
    } 

    public void setNum(String num) { 
     this.num = num; 
    } 

    public void setCode(String code) { 
     this.code = code; 
    } 

    public void setTown(String town) { 
     this.town = town; 
    } 

    public void setDescriptionRoom(String descriptionRoom) { 
     this.descriptionRoom = descriptionRoom; 
    } 

    public void setBtPwd(String btPwd) { 
     this.btPwd = btPwd; 
    } 

    public void setGprsPwd(String gprsPwd) { 
     this.gprsPwd = gprsPwd; 
    } 

    public void setGprsLogin(String gprsLogin) { 
     this.gprsLogin = gprsLogin; 
    } 

    public void setGprsApn(String gprsApn) { 
     this.gprsApn = gprsApn; 
    } 

    public void setGprsPin(String gprsPin) { 
     this.gprsPin = gprsPin; 
    } 

    public void setMotherboardId(String motherboardId) { 
     this.motherboardId = motherboardId; 
    } 

    public void setLogLevel(int logLevel) { 
     this.logLevel = logLevel; 
    } 

    public void setFrameSendPeriod(int frameSendPeriod) { 
     this.frameSendPeriod = frameSendPeriod; 
    } 

    public void setFrameCapturePeriod(int frameCapturePeriod) { 
     this.frameCapturePeriod = frameCapturePeriod; 
    } 

    public void setmQTTServerPwd(String mQTTServerPwd) { 
     this.mQTTServerPwd = mQTTServerPwd; 
    } 

    public void setmQTTServerUser(String mQTTServerUser) { 
     this.mQTTServerUser = mQTTServerUser; 
    } 

    public void setmQTTServerIp(String mQTTServerIp) { 
     this.mQTTServerIp = mQTTServerIp; 
    } 

    public void setFtpServerPwd(String ftpServerPwd) { 
     this.ftpServerPwd = ftpServerPwd; 
    } 

    public void setFtpServerUser(String ftpServerUser) { 
     this.ftpServerUser = ftpServerUser; 
    } 

    public void setFtpServerIp(String ftpServerIp) { 
     this.ftpServerIp = ftpServerIp; 
    } 

    public void setNumSerie(String numSerie) { 
     this.numSerie = numSerie; 
    } 

    public void setIdConcentrator(Long idConcentrator) { 
     this.idConcentrator = idConcentrator; 
    } 

    public void setInfoCameras(List<InfoCamera> infoCameras) { 
     this.infoCameras = infoCameras; 
    } 

} 

구성 카메라

package gasnatural.models; 

public class ConfigurationCamera { 
    private String numSerie = null; 
    private String numCamera = null; 
    private String cups = null; 

    private String status = null; 

    public ConfigurationCamera() { 

    } 

    public ConfigurationCamera(String numSerie, String numCamera, String cups, String status) { 
     this.numSerie = numSerie; 
     this.numCamera = numCamera; 
     this.cups = cups; 
     this.status = status; 
    } 

    public String getStatus() { 
     return status; 
    } 

    public String getNumSerie() { 
     return numSerie; 
    } 

    public String getNumCamera() { 
     return numCamera; 
    } 

    public String getCups() { 
     return cups; 
    } 

    public void setNumSerie(String numSerie) { 
     this.numSerie = numSerie; 
    } 

    public void setNumCamera(String numCamera) { 
     this.numCamera = numCamera; 
    } 

    public void setCups(String cups) { 
     this.cups = cups; 
    } 

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

} 

UPDATE4

난 매핑 객체 클래스를 단순화하고, I는 동일한 오류를 수신한다.

public class DTOConfigurationConcentrator { 
    private String street; 
    private String num; 

    public DTOConfigurationConcentrator() { 
     super(); 
    } 

    public String getStreet() { 
     return street; 
    } 

    public void setStreet(String street) { 
     this.street = street; 
    } 

    public String getNum() { 
     return num; 
    } 

    public void setNum(String num) { 
     this.num = num; 
    } 

} 

IT는 JSON 직렬화를위한 도구와 관련된 무언가를 보인다하지만 난이를 확인할 수있는 방법을 정확히 알지 못한다.

+0

이 변경 동의 ... @EnableWebMvc 내 구성 클래스를 태그하는 것을 잊었다 : */* 수락합니다 : 응용 프로그램/JSON – kuhajeyan

+0

은 내가 시험에 사용하는 curl 명령에 헤더를 추가 , 같은 오류가 나타납니다 ... –

+0

http://stackoverflow.com/questions/19444855/spring-rest-post-json-requestbody-content-type-not-supported 비슷한 문제가 있는지 확인할 수 있습니까? – Nagaraddi

답변

0

나는 당신의 요청 헤더에

관련 문제