springboot 통합 cxf-jaxrs 에서 json 변환 문 제 를 해결 합 니 다.
6216 단어 springbootcxf-jaxrsjson
나 는 프로젝트 를 boot 로 재 구성 할 때 cxf 의 사용 에 대해 문제 가 생 겼 는데 주로 실체 류 와 json 전환 에 있다.
밤 에 관련 된 답 을 본 후에 jaxb 는 기본적으로 xml 형식 을 지원 하 는 것 을 알 게 되 었 고 대상 이 json 으로 전환 하 는 것 은 추가 적 인 변환기 가 필요 하 다 는 것 을 알 게 되 었 습 니 다.그리고 stackoverflow 에서 해결 방법 을 찾 은 것 은 bean 을 설명 하고 Json Provider 를 주입 하 는 것 입 니 다.그러나 나 는 이것 이 서버 에서 대상 을 json 으로 전환 하 는 문 제 를 해결 할 수 있다 는 것 을 알 게 되 었 습 니 다.
클 라 이언 트 는 이상 을 보고 합 니 다.
No message body reader has been found for class ......, ContentType: application/json
다음 cxf 웹 클 라 이언 트 클래스 의 원본 코드 에서 발견:
create()방법 은 재 부팅 방법 이 많 습 니 다.그 중 하 나 는 provider 를 지정 하여 형식 을 바 꿀 수 있 습 니 다.마지막 으로 이 재 부팅 방법 을 통 해 클 라 이언 트 json 형식 전환 문 제 를 해결 하 였 습 니 다.
마지막 해결 방안:
cxf 를 단독으로 사용 하 는 토대 에서 변경 하 는 것 은 주로 두 가지 측면 이 있다.
1.서버:시작 클래스 에 bean 을 설명 하고 Jackson JaxbJSonProvider 를 주입 합 니 다.
2.클 라 이언 트:웹 클 라 이언 트 에서 create()방법 을 호출 할 때 json 으로 전환 하 는 provider 를 지정 합 니 다.
다음은 간단 한 demo 입 니 다.
1.웹 서비스 서버(생산자)
1.maven 의존
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--cxf-jaxrs-starter-->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
<version>3.2.0</version>
</dependency>
<!--jaxrs json -->
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.8.5</version>
</dependency>
2.application.yml 프로필cxf 경로 와 패키지 스 캔 설정
server:
port: 9001
cxf:
path: /services
servlet.init:
service-list-path: /info
jaxrs:
component-scan: true
3.boot 응용 시작 클래스 설정시작 클래스 에 bean 을 설명 하고 Jackson JaxbJSonProvider 대상 을 자동 으로 주입 합 니 다.그러면 cxf 는 대상 을 json 으로 바 꿀 때 이 대상 을 자동 으로 사용 합 니 다.
@SpringBootApplication
public class CxfServerApplication {
public static void main(String[] args) {
SpringApplication.run(CxfServerApplication.class, args);
}
// json
@Bean
public JacksonJaxbJsonProvider jacksonJaxbJsonProvider() {
return new JacksonJaxbJsonProvider();
}
}
4.고객 서비스 인터페이스cxf 경로 에 대한 설명 은 다른 cxf 자 료 를 참조 하 십시오.
@Path("/customerService")
public interface CustomerService {
/**
* : id
*/
@Path("/findById")
@GET
@Produces({"application/xml", "application/json"})
Customer findById(@QueryParam("id")Integer id);
}
5.고객 서비스 실현 유형간단 한 실현 클래스 입 니 다.추가 주 해 를 추가 할 필요 가 없습니다.dao 를 데이터베이스 에서 데 이 터 를 조회 하여 되 돌려 줍 니 다(dao 층 코드 가 붙 지 않 았 으 며 스스로 실현 할 수 있 습 니 다).
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService {
@Autowired
private CustomerDao customerDao;
@Override
public Customer findById(Integer id) {
// dao,
return customerDao.findById(id);
}
}
2.웹 서비스 클 라 이언 트(소비자)1.maven 의존
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--cxf-jaxrs-starter-->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxrs</artifactId>
<version>3.2.0</version>
</dependency>
<!--jaxrs json -->
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.8.5</version>
</dependency>
2.전 json 도구 설정웹 클 라 이언 트 의 create()방법 은 List
@Component
public class JsonProvider extends ArrayList<JacksonJaxbJsonProvider> {
// , JacksonJaxbJsonProvider
public JsonProvider(){
this.add(new JacksonJaxbJsonProvider());
}
}
3.웹 클 라 이언 트 를 사용 하여 웹 서 비 스 를 호출 합 니 다.Controller 에 위 에서 만 든 사용자 정의 JSonProvider 를 주입 하고 WebClient 에서 create()방법 을 호출 할 때 방법 매개 변수 로 주입 하여 json 변환 기 를 수 동 으로 지정 하 는 목적 을 달성 합 니 다.
@Controller
public class CustomerController {
// json
@Autowired
private List<JacksonJaxbJsonProvider> jsonProvider;
@RequestMapping("/customer_findById")
@ResponseBody
public List<Customer> findById(Integer id) {
// webservice
Customer customer = WebClient
.create("http://localhost:9001/services/customerService/findById?id="+id, jsonProvider)
.accept(MediaType.APPLICATION_JSON).get(Customer.class);
return customer;
}
}
셋째,기타 주의1.xml/json 형식 으로 변환 하여 전송 해 야 하 는 실체 클래스 는 클래스 이름 에 주 해 를 추가 해 야 합 니 다.
@XmlRootElement(name = "xxx")
2.위의 demo 에서 사용 하 는 cxf-spring-boot-starter-jaxrs 버 전 은 3.2.0 입 니 다.3.2.1 이후 버 전에 서 는 View Resolver 를 수 동 으로 설정 해 야 합 니 다.
그렇지 않 으 면 잘못 보고 할 것 이다.
@ConditionalOnProperty(spring.mvc.locale) did not find property 'locale' (OnPropertyCondition)
이상 은 개인 적 인 경험 이 므 로 여러분 에 게 참고 가 되 기 를 바 랍 니 다.여러분 들 도 저 희 를 많이 응원 해 주시 기 바 랍 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin Springboot -- 파트 14 사용 사례 REST로 전환하여 POST로 JSON으로 전환前回 前回 前回 記事 の は は で で で で で で を 使っ 使っ 使っ て て て て て リクエスト を を 受け取り 、 reqeustbody で 、 その リクエスト の ボディ ボディ を を 受け取り 、 関数 内部 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.