Skip to content

Commit 9568df1

Browse files
navuluriBhaskara Navuluri
andauthored
BAEL-5298 (eugenp#11965)
* BAEL-5298 * Renamed the tests * Changed the Parent to parent-boot-2 Co-authored-by: Bhaskara Navuluri <[email protected]>
1 parent 3f6a7de commit 9568df1

10 files changed

Lines changed: 376 additions & 42 deletions

File tree

feign/pom.xml

Lines changed: 88 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xmlns="http://maven.apache.org/POM/4.0.0"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
53
<modelVersion>4.0.0</modelVersion>
64
<groupId>com.baeldung.feign</groupId>
75
<artifactId>feign</artifactId>
86
<name>feign</name>
97

108
<parent>
119
<groupId>com.baeldung</groupId>
12-
<artifactId>parent-modules</artifactId>
13-
<version>1.0.0-SNAPSHOT</version>
10+
<artifactId>parent-boot-2</artifactId>
11+
<version>0.0.1-SNAPSHOT</version>
12+
<relativePath>../parent-boot-2</relativePath>
1413
</parent>
1514

15+
<properties>
16+
<java.version>1.8</java.version>
17+
<feign.version>11.8</feign.version>
18+
<wsdl4j.version>1.6.3</wsdl4j.version>
19+
</properties>
20+
1621
<dependencies>
1722
<dependency>
1823
<groupId>io.github.openfeign</groupId>
@@ -35,10 +40,85 @@
3540
<version>${lombok.version}</version>
3641
<scope>provided</scope>
3742
</dependency>
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-web</artifactId>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.springframework.boot</groupId>
49+
<artifactId>spring-boot-starter-web-services</artifactId>
50+
</dependency>
51+
<dependency>
52+
<groupId>wsdl4j</groupId>
53+
<artifactId>wsdl4j</artifactId>
54+
<version>${wsdl4j.version}</version>
55+
</dependency>
56+
<dependency>
57+
<groupId>io.github.openfeign</groupId>
58+
<artifactId>feign-hc5</artifactId>
59+
<version>${feign.version}</version>
60+
</dependency>
61+
<dependency>
62+
<groupId>io.github.openfeign</groupId>
63+
<artifactId>feign-soap</artifactId>
64+
<version>${feign.version}</version>
65+
</dependency>
66+
<dependency>
67+
<groupId>org.springframework.boot</groupId>
68+
<artifactId>spring-boot-starter-test</artifactId>
69+
<scope>test</scope>
70+
</dependency>
3871
</dependencies>
72+
<build>
73+
<plugins>
74+
<plugin>
75+
<groupId>org.springframework.boot</groupId>
76+
<artifactId>spring-boot-maven-plugin</artifactId>
77+
</plugin>
78+
<plugin>
79+
<groupId>org.codehaus.mojo</groupId>
80+
<artifactId>jaxb2-maven-plugin</artifactId>
81+
<version>2.5.0</version>
82+
<executions>
83+
<execution>
84+
<id>xjc</id>
85+
<goals>
86+
<goal>xjc</goal>
87+
</goals>
88+
</execution>
89+
</executions>
90+
<configuration>
91+
<packageName>com.baeldung.feign.soap</packageName>
92+
<sources>
93+
<source>src/main/resources/users.xsd</source>
94+
</sources>
3995

40-
<properties>
41-
<feign.version>10.11</feign.version>
42-
</properties>
96+
</configuration>
97+
</plugin>
98+
99+
<plugin>
100+
<groupId>org.jvnet.jaxb2.maven2</groupId>
101+
<artifactId>maven-jaxb2-plugin</artifactId>
102+
<version>0.14.0</version>
103+
<executions>
104+
<execution>
105+
<id>feign-soap-stub-generation</id>
106+
<goals>
107+
<goal>generate</goal>
108+
</goals>
109+
<configuration>
110+
<schemaDirectory>target/generated-sources/jaxb</schemaDirectory>
111+
<schemaIncludes>
112+
<include>*.xsd</include>
113+
</schemaIncludes>
114+
115+
<generatePackage>com.baeldung.feign.soap</generatePackage>
116+
<generateDirectory>target/generated-sources/jaxb</generateDirectory>
117+
</configuration>
118+
</execution>
119+
</executions>
120+
</plugin>
121+
</plugins>
43122

123+
</build>
44124
</project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.feign.soap;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
8+
public class FeignSoapApplication {
9+
10+
public static void main(String[] args) {
11+
SpringApplication.run(FeignSoapApplication.class, args);
12+
}
13+
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.feign.soap;
2+
3+
import feign.Headers;
4+
import feign.RequestLine;
5+
6+
public interface SoapClient {
7+
@RequestLine("POST")
8+
@Headers({"SOAPAction: createUser", "Content-Type: text/xml;charset=UTF-8", "Accept: text/xml"})
9+
String createUserWithPlainText(String soapBody);
10+
11+
@RequestLine("POST")
12+
@Headers({"Content-Type: text/xml;charset=UTF-8"})
13+
CreateUserResponse createUserWithSoap(CreateUserRequest soapBody);
14+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.baeldung.feign.soap;
2+
3+
import org.springframework.ws.server.endpoint.annotation.Endpoint;
4+
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
5+
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
6+
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;
7+
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
@Endpoint
12+
public class UsersEndpoint {
13+
14+
private static final Map<String, User> userMap = new HashMap<>();
15+
16+
@PayloadRoot(namespace = "http://www.baeldung.com/springbootsoap/feignclient", localPart = "getUserRequest")
17+
@ResponsePayload
18+
public GetUserResponse getUser(@RequestPayload GetUserRequest request) {
19+
GetUserResponse response = new GetUserResponse();
20+
response.setUser(userMap.get(request.getId()));
21+
return response;
22+
}
23+
24+
@PayloadRoot(namespace = "http://www.baeldung.com/springbootsoap/feignclient", localPart = "createUserRequest")
25+
@ResponsePayload
26+
public CreateUserResponse createUser(@RequestPayload CreateUserRequest request) {
27+
CreateUserResponse response = new CreateUserResponse();
28+
if (request.getUser().getId().equalsIgnoreCase("500"))
29+
throw new RuntimeException("This is a reserved user id");
30+
userMap.put(request.getUser().id, request.getUser());
31+
32+
response.setMessage("Success! Created the user with id - " + request.getUser().getId());
33+
return response;
34+
}
35+
36+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.feign.soap;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.boot.web.servlet.ServletRegistrationBean;
5+
import org.springframework.context.ApplicationContext;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.core.io.ClassPathResource;
9+
import org.springframework.ws.config.annotation.EnableWs;
10+
import org.springframework.ws.config.annotation.WsConfigurerAdapter;
11+
import org.springframework.ws.transport.http.MessageDispatcherServlet;
12+
import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition;
13+
import org.springframework.xml.xsd.SimpleXsdSchema;
14+
import org.springframework.xml.xsd.XsdSchema;
15+
16+
@EnableWs
17+
@Configuration
18+
public class WebServicesConfiguration extends WsConfigurerAdapter {
19+
20+
@Value("${ws.api.path:/ws/api/v1/*}")
21+
private String webserviceApiPath;
22+
@Value("${ws.port.type.name:UsersPort}")
23+
private String webservicePortTypeName;
24+
@Value("${ws.target.namespace:http://www.baeldung.com/springbootsoap/feignclient}")
25+
private String webserviceTargetNamespace;
26+
@Value("${ws.location.uri:http://localhost:18080/ws/api/v1/}")
27+
private String locationUri;
28+
29+
@Bean
30+
public ServletRegistrationBean<MessageDispatcherServlet> messageDispatcherServlet(ApplicationContext applicationContext) {
31+
MessageDispatcherServlet servlet = new MessageDispatcherServlet();
32+
servlet.setApplicationContext(applicationContext);
33+
servlet.setTransformWsdlLocations(true);
34+
return new ServletRegistrationBean<>(servlet, webserviceApiPath);
35+
}
36+
37+
@Bean(name = "users")
38+
public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema usersSchema) {
39+
DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
40+
wsdl11Definition.setPortTypeName(webservicePortTypeName);
41+
wsdl11Definition.setTargetNamespace(webserviceTargetNamespace);
42+
wsdl11Definition.setLocationUri(locationUri);
43+
wsdl11Definition.setSchema(usersSchema);
44+
return wsdl11Definition;
45+
}
46+
47+
@Bean
48+
public XsdSchema userSchema() {
49+
return new SimpleXsdSchema(new ClassPathResource("users.xsd"));
50+
}
51+
52+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
server.port=18080
2+
# Custom properties begin here
3+
ws.api.path=/ws/users/*
4+
ws.port.type.name=UsersPort
5+
ws.target.namespace=http://www.baeldung.com/springbootsoap/feignclient
6+
ws.location.uri=http://localhost:${server.port}/ws/users/
7+
debug=false

feign/src/main/resources/users.xsd

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.baeldung.com/springbootsoap/feignclient"
3+
targetNamespace="http://www.baeldung.com/springbootsoap/feignclient" elementFormDefault="qualified">
4+
5+
<xs:element name="getUserRequest">
6+
<xs:complexType>
7+
<xs:sequence>
8+
<xs:element name="id" type="xs:string"/>
9+
</xs:sequence>
10+
</xs:complexType>
11+
</xs:element>
12+
<xs:element name="getUserResponse">
13+
<xs:complexType>
14+
<xs:sequence>
15+
<xs:element name="user" type="tns:user"/>
16+
</xs:sequence>
17+
</xs:complexType>
18+
</xs:element>
19+
20+
<xs:element name="createUserRequest">
21+
<xs:complexType>
22+
<xs:sequence>
23+
<xs:element name="user" type="tns:user"/>
24+
</xs:sequence>
25+
</xs:complexType>
26+
</xs:element>
27+
<xs:element name="createUserResponse">
28+
<xs:complexType>
29+
<xs:sequence>
30+
<xs:element name="message" type="xs:string"/>
31+
</xs:sequence>
32+
</xs:complexType>
33+
</xs:element>
34+
<!-- Define the complex object Product -->
35+
<xs:complexType name="user">
36+
<xs:sequence>
37+
<xs:element name="id" type="xs:string"/>
38+
<xs:element name="name" type="xs:string"/>
39+
<xs:element name="email" type="xs:string"/>
40+
</xs:sequence>
41+
</xs:complexType>
42+
43+
44+
</xs:schema>

feign/src/test/java/com/baeldung/feign/clients/BookClientLiveTest.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,58 +4,53 @@
44
import com.baeldung.feign.models.Book;
55
import com.baeldung.feign.models.BookResource;
66
import lombok.extern.slf4j.Slf4j;
7-
import org.junit.Before;
8-
import org.junit.Test;
7+
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
910

1011
import java.util.List;
1112
import java.util.UUID;
1213
import java.util.stream.Collectors;
1314

1415
import static org.hamcrest.CoreMatchers.containsString;
1516
import static org.hamcrest.CoreMatchers.is;
16-
import static org.junit.Assert.assertThat;
17-
import static org.junit.Assert.assertTrue;
17+
import static org.hamcrest.MatcherAssert.assertThat;
18+
import static org.junit.jupiter.api.Assertions.assertTrue;
1819

1920
/**
2021
* Consumes https://github.com/Baeldung/spring-hypermedia-api
2122
*/
2223
@Slf4j
23-
public class BookClientLiveTest {
24+
class BookClientLiveTest {
2425
private BookClient bookClient;
2526

26-
@Before
27-
public void setup() {
27+
@BeforeEach
28+
void setup() {
2829
BookControllerFeignClientBuilder feignClientBuilder = new BookControllerFeignClientBuilder();
2930
bookClient = feignClientBuilder.getBookClient();
3031
}
3132

3233
@Test
33-
public void givenBookClient_shouldRunSuccessfully() throws Exception {
34-
List<Book> books = bookClient.findAll()
35-
.stream()
36-
.map(BookResource::getBook)
37-
.collect(Collectors.toList());
34+
void givenBookClient_shouldRunSuccessfully() throws Exception {
35+
List<Book> books = bookClient.findAll().stream().map(BookResource::getBook).collect(Collectors.toList());
3836
assertTrue(books.size() > 2);
3937
log.info("{}", books);
4038
}
4139

4240
@Test
43-
public void givenBookClient_shouldFindOneBook() throws Exception {
44-
Book book = bookClient.findByIsbn("0151072558")
45-
.getBook();
41+
void givenBookClient_shouldFindOneBook() throws Exception {
42+
Book book = bookClient.findByIsbn("0151072558").getBook();
4643
assertThat(book.getAuthor(), containsString("Orwell"));
4744
log.info("{}", book);
4845
}
4946

5047
@Test
51-
public void givenBookClient_shouldPostBook() throws Exception {
52-
String isbn = UUID.randomUUID()
53-
.toString();
48+
void givenBookClient_shouldPostBook() throws Exception {
49+
String isbn = UUID.randomUUID().toString();
5450
Book book = new Book(isbn, "Me", "It's me!", null, null);
5551
bookClient.create(book);
5652

57-
book = bookClient.findByIsbn(isbn)
58-
.getBook();
53+
book = bookClient.findByIsbn(isbn).getBook();
5954
assertThat(book.getAuthor(), is("Me"));
6055
log.info("{}", book);
6156
}

0 commit comments

Comments
 (0)