Giter Site home page Giter Site logo

spring-in-action-6-samples's Introduction

Spring in Action, 6th Edition Sample Code

This repo will hold the sample code from Spring in Action, 6th edition.

What are all of the weird comments with "tag::" and "end::" about???

(Note: In the most recent commit, I've removed these comments. But they may come back, so I'll leave this mention of them here.)

The source code will have several weird comments, many starting with "tag::" and some with "end::". There may also be large chunks of code commented out.

The "tag::" and "end::" comments are there for the sake of building the book itself. The code in between those comments are pulled into the manuscript so that (as much as possible) code you see in the book comes straight from code that was built and tested.

Sometimes, though, code evolves throughout the course of a chapter. It's impractical to have multiple copies of the same project just for a few lines that are different. In those cases, the variant code is commented out, but still has the "tag::" and "end::" markers around it. The danger of doing that is that it's entirely possible that the commented out variant isn't correct--after all, it hasn't been compiled or run through a test suite. If you find something out of sorts, please let me know and I'll try to fix it.

Shouldn't there be a test for ... ?

I'll readily admit that there could be more tests around certain parts of the code. When I find time, I will add these tests. In the meantime, please feel free to open a pull request with any suggested tests you think should be merged in.

spring-in-action-6-samples's People

Contributors

habuma avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

spring-in-action-6-samples's Issues

Define a table name for the class User

Hi,

when you use the class /taco-cloud-sfc/src/main/java)/tacos/User.java with a h2 db, this could run into an issue with the h2-Usertable "User" like this:

Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax Fehler in SQL Befehl "drop table if exists [*]user CASCADE "; erwartet "identifier"
Syntax error in SQL statement "drop table if exists [*]user CASCADE "; expected "identifier"; SQL statement:
drop table if exists user CASCADE  [42001-210]

Spring cannot create the data-table.

I solved this issue with an annotation for the table:

...
@Table(name="TACO_USER")
public class User implements UserDetails {
...

This solves the issue with the h2 database and the apps starts fine.

Chapter 12: tacocloud-mongo has MySQL database URL defined in application.yml

When viewing chapter 12 taco cloud code (mongodb version) you can find a datasource URL defined in application.yml, the URL points to MySQL database, whereas it should point to mongo database.

---
spring:
  profiles: prod

  datasource:
    url: jdbc:mysql://localhost/tacocloud
    username: tacouser
    password: tacopassword

https://github.com/habuma/spring-in-action-6-samples/blob/main/ch12_13/tacocloud-mongodb/tacocloud/src/main/resources/application.yml

Define Primary Key for Ingredient

In Chapter 3 code, the primary key has to be added else it fails
create table if not exists Ingredient (
id varchar(4) not null primary key,
name varchar(25) not null,
type varchar(10) not null
);

Caused by: org.springframework.jdbc.datasource.init.ScriptStatementFailedException:
Failed to execute SQL script statement #6 of URL [file:/C:/Users/mrigank.ballabh/source/repos/spring-in-action-6-samples/ch03/tacos-jdbctemplate/target/classes/schema.sql]: alter table Ingredient_Ref add foreign key (ingredient) references Ingredient(id); nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Constraint "PRIMARY KEY | UNIQUE (ID)" not found;

HttpSecurity http.authorizeRequests() is depracated

@bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
return http
.authorizeRequests() - deprecated
.mvcMatchers("/design", "/orders").hasRole("USER")
.anyRequest().permitAll()
}

looks like nowadays it should be:

@bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
return http
.authorizeHttpRequests()
.requestMatchers("/design", "/orders").hasRole("USER")
.requestMatchers("/", "/**").permitAll()
.and()
.build();
}

chapter 4 Cassandra DB: no data stored in table tacos

After submitting an order via UI, I checked data in Cassandra via cqlsh. Taco information is stored in table orders in json but no data stored in table tacos. It's different from what described in the book.

cqlsh:taco_cloud> select id, tacos from orders;

 id                                   | tacos
--------------------------------------+-------------------------------------------------------------------------------------------------------------------
 2f09eca0-6468-11ee-b2c3-5d2ec40b06dd | [{ingredients: [{name: 'Corn Tortilla', type: 'WRAP'}, {name: 'Cheddar', type: 'CHEESE'}], name: 'lily''s taco'}]

(1 rows)

cqlsh:taco_cloud> select * from tacos;

 id | createdat | ingredients | name
----+-----------+-------------+------

(0 rows)

Issue on Chapter in DesignTacoController with Spring DataJDBC

When running the project after making changes for spring data jdbc getting error:

java.lang.IllegalStateException: Cannot set property id because no setter, no wither and it's not part of the persistence constructor private com.example.tacos.domain.Ingredient()

at line ingredientRepository.findAll().forEach(i -> ingredients.add(i));

CVV validation incorrect

For the domain TacoOrder, as CVV needs to be 3 digits, the current validation presented does not check for the minimum digits required, rather only the maximum digits allowed.

Suggest to use the following regex pattern matching for constraint:

@Pattern(regexp = "^[0-9]{3}$", message = "Must be 3 digits")
private String ccCVV;

Fix browser tests when run in GitHub Actions

Some tests are failing in GitHub Actions due to failure to startup Chrome. These same tests pass fine locally. Need to sort this out or skip them when run in GitHub Actions.

Dependency issues in pom.xml CVE-2022-41854 and CVE-2022-1471

When I use idea to open the pom.xml at the beginning of the book, there is a section that keeps reporting errors. The following is the code segment that reports the error:

     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-thymeleaf</artifactId>
         <version>3.0.1</version>
    </dependency>

Detailed error content:

Provides transitive vulnerable dependency org.yaml:snakeyaml:1.33 CVE-2022-41854 6.5 Out-of-bounds Write vulnerability with medium severity found CVE-2022-1471 9.8 Deserialization of Untrusted Data vulnerability with high severity found Results Powered by Check marx
I found the cause and solution based on the following:
https://stackoverflow.com/questions/74781733/vulnerable-dependency-mavenorg-yamlsnakeyaml

Reason:

The SnakeYAML (1.30) version that Springboot relies on was found to be vulnerable in 2022, and the author had not discovered this vulnerability when writing this book.

The solution is to exclude the indirect dependency of SnakeYAML from the project, and only need to modify part of pom.xml:

     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-thymeleaf</artifactId>
         <version>3.0.1</version>
         <exclusions>
             <exclusion>
                 <groupId>org.yaml</groupId>
                 <artifactId>snakeyaml</artifactId>
             </exclusion>
         </exclusions>
     </dependency>

data.sql Not executed

add jpa:hibernate:ddl-auto=none in application.properties
this sql will be executed

Questions with IngredientRef domain

A domain IngredientRef is mentioned and created somewhere in chap 1 or 2. But by the end of chapter 3 (starting from Spring Data JPA) the domain of IngredientRef is already not mentioned.

What is its significance? It is quite confusing.

taco_order_tacos table has no entry

As it is the final code of the chapter 5, has one slight problem that it creates no entry in the taco_order_tacos table, and hence the link between the taco_order and taco is lost.

This is perhaps happening because the Modelattribute in DesignController always passes a New TacoOrder

@ModelAttribute(name = "order", value = "order")
  public TacoOrder order() {
    return new TacoOrder();
  }

Spring not able to connect with cassandra running on docker

Caused by: java.lang.IllegalStateException: Since you provided explicit contact points, the local DC must be explicitly set (see basic.load-balancing-policy.local-datacenter in the config, or set it programmatically with SessionBuilder.withLocalDatacenter). Current contact points are: Node(endPoint=/127.0.0.1:9042, hostId=09137d7d-bd7e-46b2-a3d3-fe350c30b5a9, hashCode=69f9f10a)=datacenter1. Current DCs in this cluster are: datacenter1

Application.yml

spring:
thymeleaf:
cache: true
data:
cassandra:
keyspace-name: taco_cloud
schema-action: CREATE_IF_NOT_EXISTS
local-datacenter: datacenter1
contact-points: localhost
port: 9042

root@15541c9a703e:/# nodetool status
Datacenter: datacenter1

cqlsh> desc keyspaces;

system system_distributed system_traces system_virtual_schema
system_auth system_schema system_views taco_cloud

Github repo url: https://github.com/abcool/Training/tree/master/Spring%20in%20Action/Taco%20Cloud/Chapter-4

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.