보통은 org.springframework.web.HttpMediaTypeNotSupportedException 예외는
Rest api에서 허용할 수 있는 MediaType이 아니면 에러가 난다.
@RequestMapping(path="/api/1",
consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
ResultActions result =
mockMvc.perform(post("/api/1/city")
.contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
.content(requestJson))
.andDo(print());
그러나, 특별한 경우로
SpringBoot의 UI 모델 또는 DTO에 lombok, Jackson의 JsonCreator가 잘못 결합될 때. 이상한 에러가 난다.
그래서 몇시간동안 삽질, 주화 입마에 빠질 수 있다.
org.springframework.web.HttpMediaTypeNotSupportedException
문제가 되는 코드는 다음과 같다.
@RestController
@RequestMapping("/api/test")
public class CityController {
@PostMapping
public Product post(@RequestBody City city) {
.....
}
}
@Data
public class City {
@NonNull
private Long id;
@NonNull
private String name;
@NonNull
private Integer population;
@JsonCreator
public City(Long id, String name, Integer population) {
this.id = id;
this.name = name;
this.population = population;
}
}
또는 아래와 같이 AllArgsConstructor에 JsonCreator를 사용하면 안된다.
@Data
@Builder
@AllArgsConstructor(onConstructor = @__(@JsonCreator))
public class City {
@NonNull
private Long id;
@NonNull
private String name;
@NonNull
private Integer population;
}
HttpMediaTypeNotSupportedException가 발생한다.
따라서 아래와 같이 모델을 변경하니 잘 동작한다.
@Data
@AllArgsConstructor
@NoArgsConstructor
public class City {
@NonNull
private Long id;
@NonNull
private String name;
@NonNull
private Integer population;
}
이미 보고는 되어 있는데.. 해결 방안은 딱히 없으니. 알아서 잘 피해야 할 것 같다.
https://github.com/FasterXML/jackson-databind/issues/1239
https://github.com/spring-projects/spring-boot/issues/12568
Posted by 김용환 '김용환'
에러
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class com.example.model.City]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.example.model.City` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (PushbackInputStream); line: 2, column: 3]
생성자에 Jackson의 @JsonCreator를 붙여 해결하고 싶겠지만. 결국 문제가 발생할 것이다.
@Data
public class City {
@NonNull
private Long id;
@NonNull
private String name;
@NonNull
private Integer population;
@JsonCreator
public City(Long id, String name, Integer population) {
this.id = id;
this.name = name;
this.population = population;
}
}
아래 org.springframework.web.HttpMediaTypeNotSupportedException 이 발생하게 된다...
https://knight76.tistory.com/entry/orgspringframeworkwebHttpMediaTypeNotSupportedException-%ED%95%B4%EA%B2%B0%ED%95%98%EA%B8%B0
최대한 Lombok을 잘 사용하는 것이 좋다.
Posted by 김용환 '김용환'
sprin test를 사용하다가 로그에 request의 body(특정 json)가 안나오는 문제가 있다.
MockHttpServletRequest:
HTTP Method = POST
Request URI = /api/1/city/
Parameters = {}
Headers = [Accept:"application/json"]
Body = <no character encoding set>
Session Attrs = {}
그래서 코드에 characterEncoding을 추가했더니 문제가 발생하지 않았다.
ResultActions result =
mockMvc.perform(post("/api/1/city/", 10L)
.accept(MediaType.APPLICATION_JSON)
.characterEncoding("utf-8")
.content(requestJson))
.andDo(print());
다음과 같이 request의 body 정보가 출력되었다.
MockHttpServletRequest:
HTTP Method = POST
Request URI = /api/1/city/
Parameters = {}
Headers = [Accept:"application/json"]
Body = {
"id" : 10,
"name" : "Bratislava",
"population" : 432000
}
Session Attrs = {}
Posted by 김용환 '김용환'
gradle, asciidoctor를 사용할 때
maven plugin 예제를 참고하다 잘 안되는 경우가 있다.
(디폴트로) maven의 adoc 파일을 생성하는 위치는 src/main/asciidoc 인 반면,
gradle 의 경우는 src/docs/asciidoc 이다.
gradle 사용자는 주의해야 한다.
Posted by 김용환 '김용환'
Proxy를 사용해야 하는 환경에서
grails 2.x 컴파일을 하는데, 제대로 inhouse library를 다운받지 못한 현상이 발생했다.
마치 nonProxy 설정에 문제가 있다는 느낌..
grails add-proxy client --host=${PROXY_HOST} --port=${PROXY_PORT} --noproxy="${JAVA_NO_PROXY}"
grails set-proxy client
grails compile
grails dependency-report를 실행해보면, inhouse lib을 못가져온다.
원인은 setproxy 하면서 생성된 .grails/ProxySettings.groovy 파일을 삭제하고
grails compile 을 실행하니 제대로 동작한다.
Posted by 김용환 '김용환'
예전에 2000년 쯤에 "구XX 커피"라는 회사에 몇 개월 알바를 한 적이 있다.
이때 오리온 동천동 물류 창고(현재 동천역)에 납품한 적이 있었는데. 엄청난 규모의 물류 창고 크기에 압도된 적이 있다.실제 재고량에 압도되었던 그 때의 기억이 난다.
최근에 물류 쪽 얘기를 들어보면서 모르는 단어에 대해 링크를 걸어본다.
SCM과 3PL의 관계
https://m.blog.naver.com/jackshin01/220969531915
https://m.blog.naver.com/jackshin01/220982419044
DAS, PAS, DPS
https://m.blog.naver.com/jackshin01/221039015878
냉동 창고
http://www.kharn.kr/news/article.html?no=7900
라스트 밀과 풀필먼트
https://froma.co.kr/446
Posted by 김용환 '김용환'
댓글을 달아 주세요