Code Monkey home page Code Monkey logo

soumyadip007 / spring-rest-jackson-json-data-binding Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 64 KB

Representational State Transfer is a software architectural style that defines a set of constraints to be used for creating Web services. Web services that conform to the REST architectural style, called Rest API, provide interoperability between computer systems on the Internet.We are implementing it with Sping framework with the help of jackson.

License: Eclipse Public License 2.0

Java 100.00%
spring spring-rest restful-api restful-webservices json jackson jackson-json-processor jackson-databind

spring-rest-jackson-json-data-binding's Introduction

Spring-Rest-Jackson-Json-Data-Binding

Representational State Transfer is a software architectural style that defines a set of constraints to be used for creating Web services. Web services that conform to the REST architectural style, called Rest API, provide interoperability between computer systems on the Internet.We are implementing it with Sping framework with the help of jackson.

Microservices

spring-rest Build Status

Introduction

This project shows different ways to test Spring Boot Application.

Libraries used:

  1. spring boot web

  2. spring boot test

See dependency tree.

Testing Notes

There are multiple ways to test spring applications. See improvements in Spring Boot 1.4.

Integration Tests

  • will start the server and load the complete spring context and all the beans.
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
public class MyTest {

    // will have whatever port tomcat starts on
    @Value("${local.server.port}")
    private int port;

    // ...

}
  • can use rest-assured to create BDD styled tests
@Before
public void setup() {
    logger.info("setting RestAssured port: " + port);
    RestAssured.baseURI = "http://localhost:" + port;
}

@Test
public void getAllBooksShouldReturnListOfAllBooksWithRestAssured() throws Exception {

    final String expectedTitle = "The Lightning Thief";

    Response response = given().
                        when().
                            get(baseUrl + getAllBooksURL).
                         then().
                            statusCode(HttpServletResponse.SC_OK).
                            contentType("application/json").extract().response();

    String res = response.asString();
    jsonPath(res, hasSize(4));
}
  • can use RestTestTemplate in Integration tests
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HomeRestControllerRestTemplateTest {

...

    @Autowired
	private TestRestTemplate restTemplate;

...

    @Test
	public void getAllBooksShouldReturnListOfAllBooks() throws Exception {
		final String expectedTitle = "The Lightning Thief";
		//another way to test
		ResponseEntity<Book[]> responseEntity = restTemplate.getForEntity(baseUrl + getAllBooksURL
		                                                                    , Book[].class);
		Book[] books = responseEntity.getBody();
		List<Book> booksList = Arrays.asList(books);
		assertThat(booksList.isEmpty(), is(false));
		assertThat(booksList.size(), is(4));
		assertThat(booksList.get(0).getTitle(), is(expectedTitle));
	}

Unit/Slice Tests

  • Use this to test individual parts of the spring application. @WebMVCTest to test just the controller/service.
  • mockmvc provides methods to fake the http calls. There is no server running in the background. You can check the logs and confirm tomcast is never started.
@RunWith(SpringRunner.class)
@WebMvcTest(HomeRestController.class)
public class HomeRestControllerMockMVCTest {
    ...

    @Autowired
    	private MockMvc mockMvc;

    @Test
    	public void getAllBooksShouldExist() throws Exception{
    		mockMvc.perform(get(baseUrl+getAllBooksURL).accept(MediaType.APPLICATION_JSON))
    		.andExpect(status().isOk());
    	}
}

Notes:

  • Read a resource using appcontext:
private ApplicationContext appContext;
Resource resource = appContext.getResource("classpath:data.json");
  • Use ObjectMapper to convert File/url into json object
ObjectMapper mapper = new ObjectMapper();
Book[] b = mapper.readValue(resource.getFile(), Book[].class);

Security Issue

spring-rest-jackson-json-data-binding's People

Contributors

soumyadip007 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.