简体   繁体   中英

How can I return JPA data from a rest resource to a webpage using Spring Boot?

I'm using this tutorial from the Spring Boot Guides. How would I return the all of the the nutrients from the DB to a JSP or ThymeLeaf page. For now, I want to have the front-end and back-end in on project.

How can I configure the project as it so that the rest responses appear on the web pages I plan to add? Do I need to add some xml configuration? Is that still necessary in Spring?

Entity:

@Entity
@Table(name = "NUTRIENT_DEFINITION")


public class NutritionDefinition implements Serializable {


@Id
@NotNull()
@Size(max = 3)
@Column(name = "USDA_NUTRIENT_IDENTIFIER")
private String usdaNutrientIdentifier;

@NotNull
@Size(max = 7)
@Column(name = "UNITS")
private String  units;

@Size(max = 20)
@Column(name = "INFOODS_TAG")
private String infoodsTag;

@NotNull
@Size(max = 60)
@Column(name = "NUTRIENT_NAME")
private String nutrientName;

@NotNull
@Size(max = 1)
@Column(name = "NUMBER_OF_DECIMAL_PLACES")
private String numberOfDecimalPlaces;

@NotNull
@Size(max = 6)
@Column(name = "SORT_ORDER")
private String sortOrder;


protected NutritionDefinition(){


}

public NutritionDefinition(String usda_nutrient_identifier,String units, String infoods_tag,
                           String nutrient_name, String number_of_decimal_places, String sort_order){

    this.usdaNutrientIdentifier = usda_nutrient_identifier;
    this.units = units;
    this.infoodsTag = infoods_tag;
    this.nutrientName = nutrient_name;
    this.numberOfDecimalPlaces = number_of_decimal_places;
    this.sortOrder = sort_order;
}


public String getUsdaNutrientIdentifier() {
    return usdaNutrientIdentifier;
}

public void setUsdaNutrientIdentifier(String usdaNutrientIdentifier) {
    this.usdaNutrientIdentifier = usdaNutrientIdentifier;
}

public String getUnits() {
    return units;
}

public void setUnits(String units) {
    this.units = units;
}

public String getInfoodsTag() {
    return infoodsTag;
}

public void setInfoodsTag(String infoodsTag) {
    this.infoodsTag = infoodsTag;
}

public String getNutrientName() {
    return nutrientName;
}

public void setNutrientName(String nutrientName) {
    this.nutrientName = nutrientName;
}

public String getNumberOfDecimalPlaces() {
    return numberOfDecimalPlaces;
}

public void setNumberOfDecimalPlaces(String numberOfDecimalPlaces) {
    this.numberOfDecimalPlaces = numberOfDecimalPlaces;
}

public String getSortOrder() {
    return sortOrder;
}

public void setSortOrder(String sortOrder) {
    this.sortOrder = sortOrder;
}
}

Rest class:

@RepositoryRestResource(collectionResourceRel = "NutritionDefinition", path = "NutritionDefinition")
public interface NutrientRepository extends PagingAndSortingRepository<NutritionDefinition,String>{

    List<NutritionDefinition> findBynutrientName(@Param("nutrientName")String name);


}

Edit: I've seen code examples where an object called "Model" is a passed as a parameter to the rest repository but, I can't figure out how to weave that into my example project.

The ReposityRestResource is explicit on returning a RESTful response. If you are wanting to display the results as HTML instead of JSON then you want to use a replaying engine like Thymeleaf. The one edge case to this is if you want the web page to make AJAX calls to the backing service to pull in the data.

If you are going to return the HTML with Thymeleaf then yes you would want to add the results to the page Model so that Thymeleaf can render it. There is a getting started guide for that: https://spring.io/guides/gs/serving-web-content/ . The one addition you would want on that guide is to inject the repository into the controller and call the find method.

For the AJAX solution you can use JavaScript libraries like jQuery to make the call to the service. I am not as familiar with that flow. Best to add ask another question targeted at that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM